From 5d440a2b9eb03af55fe3c83b22ee36cb7a5557a5 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Wed, 9 Oct 2024 11:48:25 +0400 Subject: [PATCH] Fixed creating community --- .../bloc/space_management_bloc.dart | 21 ++++++++++++------ .../view/dialogs/create_community_dialog.dart | 9 ++++++-- .../widgets/sidebar_widget.dart | 22 +++++++++++++------ 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/lib/pages/spaces_management/bloc/space_management_bloc.dart b/lib/pages/spaces_management/bloc/space_management_bloc.dart index f3ad73dc..00498e29 100644 --- a/lib/pages/spaces_management/bloc/space_management_bloc.dart +++ b/lib/pages/spaces_management/bloc/space_management_bloc.dart @@ -14,6 +14,7 @@ class SpaceManagementBloc on(_onLoadCommunityAndSpaces); on(_onCreateSpace); on(_onUpdateSpacePosition); + on(_onCreateCommunity); } void _onLoadCommunityAndSpaces( @@ -68,22 +69,28 @@ class SpaceManagementBloc // Handle space position update logic } - void _onCreateCommunity( + void _onCreateCommunity( CreateCommunityEvent event, Emitter emit, ) async { + final previousState = state; + emit(SpaceManagementLoading()); + try { - CommunityModel? community = await _api.createCommunity( + + CommunityModel? newCommunity = await _api.createCommunity( event.name, event.description, event.regionId, ); - if (community != null) { - List updatedCommunities = List.from((state as SpaceManagementLoaded).communities) - ..add(community); // Add the newly created community to the list - - emit(SpaceManagementLoaded(communities: updatedCommunities)); + if (newCommunity != null) { + if (previousState is SpaceManagementLoaded) { + final updatedCommunities = + List.from(previousState.communities) + ..add(newCommunity); + emit(SpaceManagementLoaded(communities: updatedCommunities)); + } } else { emit(const SpaceManagementError('Error creating community')); } diff --git a/lib/pages/spaces_management/view/dialogs/create_community_dialog.dart b/lib/pages/spaces_management/view/dialogs/create_community_dialog.dart index 2eb8107e..c7a96889 100644 --- a/lib/pages/spaces_management/view/dialogs/create_community_dialog.dart +++ b/lib/pages/spaces_management/view/dialogs/create_community_dialog.dart @@ -1,7 +1,8 @@ import 'package:flutter/material.dart'; class CreateCommunityDialog extends StatefulWidget { - final Function(String) onCreateCommunity; + final Function(String name, String description, String regionId) + onCreateCommunity; const CreateCommunityDialog({super.key, required this.onCreateCommunity}); @@ -109,7 +110,11 @@ class CreateCommunityDialogState extends State { child: ElevatedButton( onPressed: () { if (enteredName.isNotEmpty) { - widget.onCreateCommunity(enteredName); + widget.onCreateCommunity( + enteredName, + "", + "42dc377a-1a39-4df9-b85a-b3817af88525", + ); Navigator.of(context).pop(); } }, diff --git a/lib/pages/spaces_management/widgets/sidebar_widget.dart b/lib/pages/spaces_management/widgets/sidebar_widget.dart index 9314b10c..6ce2c4a7 100644 --- a/lib/pages/spaces_management/widgets/sidebar_widget.dart +++ b/lib/pages/spaces_management/widgets/sidebar_widget.dart @@ -1,6 +1,9 @@ import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_svg/svg.dart'; import 'package:syncrow_web/common/search_bar.dart'; +import 'package:syncrow_web/pages/spaces_management/bloc/space_management_bloc.dart'; +import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.dart'; import 'package:syncrow_web/pages/spaces_management/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; import 'package:syncrow_web/pages/spaces_management/widgets/community_tile.dart'; @@ -30,14 +33,19 @@ class _SidebarWidgetState extends State { super.initState(); } - void _showCreateCommunityDialog() { + void _showCreateCommunityDialog(BuildContext parentContext) { showDialog( - context: context, + context: parentContext, builder: (context) => CreateCommunityDialog( - onCreateCommunity: (String communityName) { - setState(() { - // You can update the community list here when a new community is added - }); + onCreateCommunity: + (String communityName, String description, String regionId) { + parentContext.read().add( + CreateCommunityEvent( + name: communityName, + description: description, + regionId: regionId, + ), + ); }, ), ); @@ -122,7 +130,7 @@ class _SidebarWidgetState extends State { style: Theme.of(context).textTheme.titleMedium, ), GestureDetector( - onTap: _showCreateCommunityDialog, + onTap: () => _showCreateCommunityDialog(context), child: Container( width: 30, height: 30,