Enhance EditCommunityDialog: Refactor to accept parent context and streamline community update handling. Introduced a new method _onUpdateCommunitySuccess for improved readability and maintainability. Updated SpaceManagementCommunityDialogHelper to pass the parent context for better state management in the community update flow.

This commit is contained in:
Faris Armoush
2025-07-06 12:52:35 +03:00
parent 826dea8054
commit 73de1e6ff9
3 changed files with 34 additions and 14 deletions

View File

@ -15,7 +15,10 @@ abstract final class SpaceManagementCommunityDialogHelper {
) { ) {
showDialog<void>( showDialog<void>(
context: context, context: context,
builder: (_) => EditCommunityDialog(community: community), builder: (_) => EditCommunityDialog(
community: community,
parentContext: context,
),
); );
} }

View File

@ -120,11 +120,12 @@ class CommunitiesBloc extends Bloc<CommunitiesEvent, CommunitiesState> {
CommunitiesUpdateCommunity event, CommunitiesUpdateCommunity event,
Emitter<CommunitiesState> emit, Emitter<CommunitiesState> emit,
) { ) {
final updatedCommunities = state.communities
.map((e) => e.uuid == event.community.uuid ? event.community : e)
.toList();
emit( emit(
state.copyWith( state.copyWith(
communities: state.communities communities: updatedCommunities,
.map((e) => e.uuid == event.community.uuid ? event.community : e)
.toList(),
), ),
); );
} }

View File

@ -4,14 +4,20 @@ import 'package:syncrow_web/pages/space_management_v2/main_module/shared/helpers
import 'package:syncrow_web/pages/space_management_v2/main_module/shared/widgets/community_dialog.dart'; import 'package:syncrow_web/pages/space_management_v2/main_module/shared/widgets/community_dialog.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart'; import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/bloc/communities_bloc.dart'; import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/bloc/communities_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/communities_tree_selection_bloc/communities_tree_selection_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/update_community/data/services/remote_update_community_service.dart'; import 'package:syncrow_web/pages/space_management_v2/modules/update_community/data/services/remote_update_community_service.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/update_community/presentation/bloc/update_community_bloc.dart'; import 'package:syncrow_web/pages/space_management_v2/modules/update_community/presentation/bloc/update_community_bloc.dart';
import 'package:syncrow_web/services/api/http_service.dart'; import 'package:syncrow_web/services/api/http_service.dart';
class EditCommunityDialog extends StatelessWidget { class EditCommunityDialog extends StatelessWidget {
const EditCommunityDialog({required this.community, super.key}); const EditCommunityDialog({
required this.community,
required this.parentContext,
super.key,
});
final CommunityModel community; final CommunityModel community;
final BuildContext parentContext;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -26,15 +32,7 @@ class EditCommunityDialog extends StatelessWidget {
SpaceManagementCommunityDialogHelper.showLoadingDialog(context); SpaceManagementCommunityDialogHelper.showLoadingDialog(context);
break; break;
case UpdateCommunitySuccess(:final community): case UpdateCommunitySuccess(:final community):
Navigator.of(context).pop(); _onUpdateCommunitySuccess(context, community);
Navigator.of(context).pop();
SpaceManagementCommunityDialogHelper.showSuccessSnackBar(
context,
'${community.name} community updated successfully',
);
context.read<CommunitiesBloc>().add(
CommunitiesUpdateCommunity(community),
);
break; break;
case UpdateCommunityFailure(): case UpdateCommunityFailure():
Navigator.of(context).pop(); Navigator.of(context).pop();
@ -52,4 +50,22 @@ class EditCommunityDialog extends StatelessWidget {
), ),
); );
} }
void _onUpdateCommunitySuccess(
BuildContext context,
CommunityModel community,
) {
Navigator.of(context).pop();
Navigator.of(context).pop();
SpaceManagementCommunityDialogHelper.showSuccessSnackBar(
context,
'${community.name} community updated successfully',
);
parentContext.read<CommunitiesBloc>().add(
CommunitiesUpdateCommunity(community),
);
parentContext.read<CommunitiesTreeSelectionBloc>().add(
SelectCommunityEvent(community: community),
);
}
} }