refactoring
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/// BLoC pour la gestion des organisations (Clean Architecture)
|
||||
library organizations_bloc;
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import '../data/models/organization_model.dart';
|
||||
@@ -99,6 +100,7 @@ class OrganizationsBloc extends Bloc<OrganizationsEvent, OrganizationsState> {
|
||||
useMesOnly: event.useMesOnly,
|
||||
));
|
||||
} catch (e) {
|
||||
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
||||
emit(OrganizationsError(
|
||||
'Erreur lors du chargement des organisations',
|
||||
details: e.toString(),
|
||||
@@ -144,6 +146,7 @@ class OrganizationsBloc extends Bloc<OrganizationsEvent, OrganizationsState> {
|
||||
currentPage: nextPage,
|
||||
));
|
||||
} catch (e) {
|
||||
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
||||
emit(OrganizationsError(
|
||||
'Erreur lors du chargement de plus d\'organisations',
|
||||
details: e.toString(),
|
||||
@@ -204,6 +207,7 @@ class OrganizationsBloc extends Bloc<OrganizationsEvent, OrganizationsState> {
|
||||
));
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
||||
emit(OrganizationsError(
|
||||
'Erreur lors de la recherche',
|
||||
details: e.toString(),
|
||||
@@ -240,6 +244,7 @@ class OrganizationsBloc extends Bloc<OrganizationsEvent, OrganizationsState> {
|
||||
statusFilter: event.statut,
|
||||
));
|
||||
} catch (e) {
|
||||
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
||||
emit(OrganizationsError(
|
||||
'Erreur lors de la recherche avancée',
|
||||
details: e.toString(),
|
||||
@@ -262,6 +267,7 @@ class OrganizationsBloc extends Bloc<OrganizationsEvent, OrganizationsState> {
|
||||
emit(OrganizationError('Organisation non trouvée', organizationId: event.id));
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
||||
emit(OrganizationError(
|
||||
'Erreur lors du chargement de l\'organisation',
|
||||
organizationId: event.id,
|
||||
@@ -279,12 +285,10 @@ class OrganizationsBloc extends Bloc<OrganizationsEvent, OrganizationsState> {
|
||||
try {
|
||||
final createdOrganization = await _createOrganization(event.organization);
|
||||
emit(OrganizationCreated(createdOrganization));
|
||||
|
||||
// Recharger la liste si elle était déjà chargée
|
||||
if (state is OrganizationsLoaded) {
|
||||
add(const RefreshOrganizations());
|
||||
}
|
||||
// Toujours recharger la liste après création
|
||||
add(const RefreshOrganizations());
|
||||
} catch (e) {
|
||||
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
||||
emit(OrganizationsError(
|
||||
'Erreur lors de la création de l\'organisation',
|
||||
details: e.toString(),
|
||||
@@ -299,6 +303,8 @@ class OrganizationsBloc extends Bloc<OrganizationsEvent, OrganizationsState> {
|
||||
) async {
|
||||
emit(OrganizationUpdating(event.id));
|
||||
|
||||
// Capturer l'état avant tout emit
|
||||
final previousState = state;
|
||||
try {
|
||||
final updatedOrganization = await _updateOrganization(
|
||||
event.id,
|
||||
@@ -306,20 +312,20 @@ class OrganizationsBloc extends Bloc<OrganizationsEvent, OrganizationsState> {
|
||||
);
|
||||
emit(OrganizationUpdated(updatedOrganization));
|
||||
|
||||
// Mettre à jour la liste si elle était déjà chargée
|
||||
final currentState = state;
|
||||
if (currentState is OrganizationsLoaded) {
|
||||
final updatedList = currentState.organizations.map((org) {
|
||||
// Mettre à jour la liste en place
|
||||
if (previousState is OrganizationsLoaded) {
|
||||
final updatedList = previousState.organizations.map((org) {
|
||||
return org.id == event.id ? updatedOrganization : org;
|
||||
}).toList();
|
||||
|
||||
final filteredList = _applyCurrentFilters(updatedList, currentState);
|
||||
emit(currentState.copyWith(
|
||||
final filteredList = _applyCurrentFilters(updatedList, previousState);
|
||||
emit(previousState.copyWith(
|
||||
organizations: updatedList,
|
||||
filteredOrganizations: filteredList,
|
||||
));
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
||||
emit(OrganizationsError(
|
||||
'Erreur lors de la mise à jour de l\'organisation',
|
||||
details: e.toString(),
|
||||
@@ -334,21 +340,23 @@ class OrganizationsBloc extends Bloc<OrganizationsEvent, OrganizationsState> {
|
||||
) async {
|
||||
emit(OrganizationDeleting(event.id));
|
||||
|
||||
// Capturer l'état avant tout emit
|
||||
final previousState = state;
|
||||
try {
|
||||
await _deleteOrganization(event.id);
|
||||
emit(OrganizationDeleted(event.id));
|
||||
|
||||
// Retirer de la liste si elle était déjà chargée
|
||||
final currentState = state;
|
||||
if (currentState is OrganizationsLoaded) {
|
||||
final updatedList = currentState.organizations.where((org) => org.id != event.id).toList();
|
||||
final filteredList = _applyCurrentFilters(updatedList, currentState);
|
||||
emit(currentState.copyWith(
|
||||
// Retirer de la liste en place
|
||||
if (previousState is OrganizationsLoaded) {
|
||||
final updatedList = previousState.organizations.where((org) => org.id != event.id).toList();
|
||||
final filteredList = _applyCurrentFilters(updatedList, previousState);
|
||||
emit(previousState.copyWith(
|
||||
organizations: updatedList,
|
||||
filteredOrganizations: filteredList,
|
||||
));
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
||||
emit(OrganizationsError(
|
||||
'Erreur lors de la suppression de l\'organisation',
|
||||
details: e.toString(),
|
||||
@@ -361,26 +369,26 @@ class OrganizationsBloc extends Bloc<OrganizationsEvent, OrganizationsState> {
|
||||
ActivateOrganization event,
|
||||
Emitter<OrganizationsState> emit,
|
||||
) async {
|
||||
final previousState = state;
|
||||
emit(OrganizationActivating(event.id));
|
||||
|
||||
try {
|
||||
final activatedOrganization = await _repository.activateOrganization(event.id);
|
||||
emit(OrganizationActivated(activatedOrganization));
|
||||
|
||||
// Mettre à jour la liste si elle était déjà chargée
|
||||
final currentState = state;
|
||||
if (currentState is OrganizationsLoaded) {
|
||||
final updatedList = currentState.organizations.map((org) {
|
||||
if (previousState is OrganizationsLoaded) {
|
||||
final updatedList = previousState.organizations.map((org) {
|
||||
return org.id == event.id ? activatedOrganization : org;
|
||||
}).toList();
|
||||
|
||||
final filteredList = _applyCurrentFilters(updatedList, currentState);
|
||||
emit(currentState.copyWith(
|
||||
final filteredList = _applyCurrentFilters(updatedList, previousState);
|
||||
emit(previousState.copyWith(
|
||||
organizations: updatedList,
|
||||
filteredOrganizations: filteredList,
|
||||
));
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
||||
emit(OrganizationsError(
|
||||
'Erreur lors de l\'activation de l\'organisation',
|
||||
details: e.toString(),
|
||||
@@ -393,26 +401,26 @@ class OrganizationsBloc extends Bloc<OrganizationsEvent, OrganizationsState> {
|
||||
SuspendOrganization event,
|
||||
Emitter<OrganizationsState> emit,
|
||||
) async {
|
||||
final previousState = state;
|
||||
emit(OrganizationSuspending(event.id));
|
||||
|
||||
try {
|
||||
final suspendedOrganization = await _repository.suspendOrganization(event.id);
|
||||
emit(OrganizationSuspended(suspendedOrganization));
|
||||
|
||||
// Mettre à jour la liste si elle était déjà chargée
|
||||
final currentState = state;
|
||||
if (currentState is OrganizationsLoaded) {
|
||||
final updatedList = currentState.organizations.map((org) {
|
||||
if (previousState is OrganizationsLoaded) {
|
||||
final updatedList = previousState.organizations.map((org) {
|
||||
return org.id == event.id ? suspendedOrganization : org;
|
||||
}).toList();
|
||||
|
||||
final filteredList = _applyCurrentFilters(updatedList, currentState);
|
||||
emit(currentState.copyWith(
|
||||
final filteredList = _applyCurrentFilters(updatedList, previousState);
|
||||
emit(previousState.copyWith(
|
||||
organizations: updatedList,
|
||||
filteredOrganizations: filteredList,
|
||||
));
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
||||
emit(OrganizationsError(
|
||||
'Erreur lors de la suspension de l\'organisation',
|
||||
details: e.toString(),
|
||||
@@ -508,6 +516,7 @@ class OrganizationsBloc extends Bloc<OrganizationsEvent, OrganizationsState> {
|
||||
final stats = await _repository.getOrganizationsStats();
|
||||
emit(OrganizationsStatsLoaded(stats));
|
||||
} catch (e) {
|
||||
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
||||
emit(const OrganizationsStatsError('Erreur lors du chargement des statistiques'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user