diff --git a/lib/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart b/lib/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart index f8ed1b86..554fdafe 100644 --- a/lib/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart +++ b/lib/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart @@ -52,10 +52,14 @@ class SpaceManagementBloc break; } } + + var prevSpaceModels = await fetchSpaceModels(previousState); + emit(SpaceManagementLoaded( communities: updatedCommunities, products: previousState.products, selectedCommunity: previousState.selectedCommunity, + spaceModels: prevSpaceModels, )); } } else { @@ -66,6 +70,23 @@ class SpaceManagementBloc } } + Future> fetchSpaceModels( + SpaceManagementState previousState) async { + List prevSpaceModels = []; + + if (previousState is SpaceManagementLoaded || previousState is BlankState) { + prevSpaceModels = List.from( + (previousState as dynamic).spaceModels ?? [], + ); + } + + if (prevSpaceModels.isEmpty) { + prevSpaceModels = await _spaceModelApi.listSpaceModels(page: 1); + } + + return prevSpaceModels; + } + void _onloadProducts() async { if (_cachedProducts == null) { final products = await _productApi.fetchProducts(); @@ -89,19 +110,24 @@ class SpaceManagementBloc return await _api.getSpaceHierarchy(communityUuid); } - void _onNewCommunity( + Future _onNewCommunity( NewCommunityEvent event, Emitter emit, - ) { + ) async { try { + final previousState = state; + if (event.communities.isEmpty) { emit(const SpaceManagementError('No communities provided.')); return; } + var prevSpaceModels = await fetchSpaceModels(previousState); + emit(BlankState( communities: event.communities, products: _cachedProducts ?? [], + spaceModels: prevSpaceModels, )); } catch (error) { emit(SpaceManagementError('Error loading communities: $error')); @@ -112,6 +138,7 @@ class SpaceManagementBloc BlankStateEvent event, Emitter emit) async { try { final previousState = state; + var prevSpaceModels = await fetchSpaceModels(previousState); if (previousState is SpaceManagementLoaded || previousState is BlankState) { @@ -119,6 +146,7 @@ class SpaceManagementBloc emit(BlankState( communities: List.from(prevCommunities), products: _cachedProducts ?? [], + spaceModels: prevSpaceModels, )); return; } @@ -139,6 +167,7 @@ class SpaceManagementBloc })); emit(BlankState( + spaceModels: prevSpaceModels, communities: updatedCommunities, products: _cachedProducts ?? [], )); @@ -217,6 +246,7 @@ class SpaceManagementBloc try { CommunityModel? newCommunity = await _api.createCommunity(event.name, event.description); + var prevSpaceModels = await fetchSpaceModels(previousState); if (newCommunity != null) { if (previousState is SpaceManagementLoaded || @@ -226,6 +256,7 @@ class SpaceManagementBloc ); final updatedCommunities = prevCommunities..add(newCommunity); emit(SpaceManagementLoaded( + spaceModels: prevSpaceModels, communities: updatedCommunities, products: _cachedProducts ?? [], selectedCommunity: newCommunity,