diff --git a/lib/pages/spaces_management/bloc/space_management_bloc.dart b/lib/pages/spaces_management/bloc/space_management_bloc.dart index f178dd3a..89a04fc2 100644 --- a/lib/pages/spaces_management/bloc/space_management_bloc.dart +++ b/lib/pages/spaces_management/bloc/space_management_bloc.dart @@ -7,20 +7,24 @@ import 'package:syncrow_web/pages/spaces_management/bloc/space_management_state. import 'package:syncrow_web/services/product_api.dart'; import 'package:syncrow_web/services/space_mana_api.dart'; -class SpaceManagementBloc extends Bloc { +class SpaceManagementBloc + extends Bloc { final CommunitySpaceManagementApi _api; final ProductApi _productApi; List? _cachedProducts; - SpaceManagementBloc(this._api, this._productApi) : super(SpaceManagementInitial()) { + SpaceManagementBloc(this._api, this._productApi) + : super(SpaceManagementInitial()) { on(_onLoadCommunityAndSpaces); on(_onUpdateSpacePosition); on(_onCreateCommunity); - on(_onSaveSpaces); - on(_onFetchProducts); + on(_onSelectCommunity); on(_onCommunityDelete); on(_onUpdateCommunity); + on(_onSaveSpaces); + on(_onFetchProducts); + on(_onSelectSpace); } void _onUpdateCommunity( @@ -30,22 +34,23 @@ class SpaceManagementBloc extends Bloc.from(previousState.communities); - for(var community in updatedCommunities){ - if(community.uuid == event.communityUuid){ + final updatedCommunities = + List.from(previousState.communities); + for (var community in updatedCommunities) { + if (community.uuid == event.communityUuid) { community.name = event.name; break; } } emit(SpaceManagementLoaded( - communities: updatedCommunities, - products: previousState.products, - selectedCommunity: previousState.selectedCommunity, - )); - + communities: updatedCommunities, + products: previousState.products, + selectedCommunity: previousState.selectedCommunity, + )); } } else { emit(const SpaceManagementError('Failed to update the community.')); @@ -88,7 +93,8 @@ class SpaceManagementBloc extends Bloc updatedCommunities = await Future.wait( communities.map((community) async { - List spaces = await _api.getSpaceHierarchy(community.uuid); + List spaces = + await _api.getSpaceHierarchy(community.uuid); return CommunityModel( uuid: community.uuid, createdAt: community.createdAt, @@ -101,7 +107,8 @@ class SpaceManagementBloc extends Bloc.from(previousState.communities) - ..add(newCommunity); + final updatedCommunities = + List.from(previousState.communities) + ..add(newCommunity); emit(SpaceManagementLoaded( communities: updatedCommunities, products: _cachedProducts ?? [], @@ -158,6 +167,53 @@ class SpaceManagementBloc extends Bloc emit, + ) async { + _handleCommunitySpaceStateUpdate( + emit: emit, + selectedCommunity: event.selectedCommunity, + selectedSpace: null, + ); + } + + void _onSelectSpace( + SelectSpaceEvent event, + Emitter emit, + ) { + _handleCommunitySpaceStateUpdate( + emit: emit, + selectedCommunity: event.selectedCommunity, + selectedSpace: event.selectedSpace, + ); + } + + void _handleCommunitySpaceStateUpdate({ + required Emitter emit, + CommunityModel? selectedCommunity, + SpaceModel? selectedSpace, + }) { + final previousState = state; + emit(SpaceManagementLoading()); + + try { + if (previousState is SpaceManagementLoaded) { + final communities = List.from( + (previousState as dynamic).communities, + ); + emit(SpaceManagementLoaded( + communities: communities, + products: _cachedProducts ?? [], + selectedCommunity: selectedCommunity, + selectedSpace: selectedSpace, + )); + } + } catch (e) { + emit(SpaceManagementError('Error updating state: $e')); + } + } + void _onSaveSpaces( SaveSpacesEvent event, Emitter emit, @@ -166,7 +222,8 @@ class SpaceManagementBloc extends Bloc communities; final List products; - CommunityModel? selectedCommunity; // Include products in the state + CommunityModel? selectedCommunity; + SpaceModel? selectedSpace; SpaceManagementLoaded( - {required this.communities, required this.products, this.selectedCommunity}); + {required this.communities, + required this.products, + this.selectedCommunity, + this.selectedSpace}); } class SpaceCreationSuccess extends SpaceManagementState {