diff --git a/lib/pages/spaces_management/bloc/space_management_bloc.dart b/lib/pages/spaces_management/bloc/space_management_bloc.dart index 55194381..fefbc849 100644 --- a/lib/pages/spaces_management/bloc/space_management_bloc.dart +++ b/lib/pages/spaces_management/bloc/space_management_bloc.dart @@ -60,41 +60,42 @@ class SpaceManagementBloc } } + void _onloadProducts() async { + if (_cachedProducts == null) { + final products = await _productApi.fetchProducts(); + _cachedProducts = products; + } + } + void _onFetchProducts( FetchProductsEvent event, Emitter emit, ) async { - if (_cachedProducts != null) { - // Products are already cached, no need to fetch again - return; - } - try { - final products = await _productApi.fetchProducts(); - _cachedProducts = products; // Cache the products locally + _onloadProducts(); } catch (e) { emit(SpaceManagementError('Error fetching products: $e')); } } + Future> _fetchSpacesForCommunity( + String communityUuid) async { + return await _api.getSpaceHierarchy(communityUuid); + } + void _onLoadCommunityAndSpaces( LoadCommunityAndSpacesEvent event, Emitter emit, ) async { emit(SpaceManagementLoading()); try { - if (_cachedProducts == null) { - final products = await _productApi.fetchProducts(); - _cachedProducts = products; - } - - // Fetch all communities + _onloadProducts(); List communities = await _api.fetchCommunities(); List updatedCommunities = await Future.wait( communities.map((community) async { List spaces = - await _api.getSpaceHierarchy(community.uuid); + await _fetchSpacesForCommunity(community.uuid); return CommunityModel( uuid: community.uuid, createdAt: community.createdAt, @@ -225,16 +226,52 @@ class SpaceManagementBloc try { final updatedSpaces = await saveSpacesHierarchically(event.spaces, event.communityUuid); + + final allSpaces = await _fetchSpacesForCommunity(event.communityUuid); + emit(SpaceCreationSuccess(spaces: updatedSpaces)); - add(LoadCommunityAndSpacesEvent()); + + if (previousState is SpaceManagementLoaded) { + _updateLoadedState( + previousState, + allSpaces, + event.communityUuid, + emit, + ); + } else { + add(LoadCommunityAndSpacesEvent()); + } } catch (e) { emit(SpaceManagementError('Error saving spaces: $e')); + if (previousState is SpaceManagementLoaded) { emit(previousState); } } } + void _updateLoadedState( + SpaceManagementLoaded previousState, + List allSpaces, + String communityUuid, + Emitter emit, + ) { + final communities = List.from(previousState.communities); + + for (var community in communities) { + if (community.uuid == communityUuid) { + community.spaces = allSpaces; + emit(SpaceManagementLoaded( + communities: communities, + products: _cachedProducts ?? [], + selectedCommunity: community, + selectedSpace: null, + )); + return; + } + } + } + Future> saveSpacesHierarchically( List spaces, String communityUuid) async { final orderedSpaces = flattenHierarchy(spaces); @@ -245,7 +282,6 @@ class SpaceManagementBloc for (var parent in parentsToDelete) { try { - // Ensure parent.uuid is not null before calling the API if (parent.uuid != null) { await _api.deleteSpace(communityUuid, parent.uuid!); }