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 358bdd48..6dc847eb 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 @@ -18,9 +18,6 @@ import 'package:syncrow_web/services/product_api.dart'; import 'package:syncrow_web/services/space_mana_api.dart'; import 'package:syncrow_web/services/space_model_mang_api.dart'; import 'package:syncrow_web/utils/constants/action_enum.dart'; -import 'package:syncrow_web/utils/constants/strings_manager.dart'; -import 'package:syncrow_web/utils/constants/temp_const.dart'; -import 'package:syncrow_web/utils/helpers/shared_preferences_helper.dart'; class SpaceManagementBloc extends Bloc { @@ -231,12 +228,20 @@ class SpaceManagementBloc ) async { _logEvent('LoadCommunityAndSpacesEvent'); - var prevState = state; + // If already loaded, use cached data + if (state is SpaceManagementLoaded) { + emit(state); + return; + } + emit(SpaceManagementLoading()); + try { final projectUuid = await ProjectManager.getProjectUUID() ?? ''; + // Load products only once _onloadProducts(); + List communities = await _api.fetchCommunities(projectUuid); @@ -250,17 +255,20 @@ class SpaceManagementBloc updatedAt: community.updatedAt, name: community.name, description: community.description, - spaces: spaces, // New spaces list + spaces: spaces, region: community.region, ); }).toList(), ); - final prevSpaceModels = await fetchSpaceModels(prevState); + // Fetch space models only once + final prevSpaceModels = await fetchSpaceModels(state); + emit(SpaceManagementLoaded( - communities: updatedCommunities, - products: _cachedProducts ?? [], - spaceModels: prevSpaceModels)); + communities: updatedCommunities, + products: _cachedProducts ?? [], + spaceModels: prevSpaceModels, + )); } catch (e) { emit(SpaceManagementError('Error loading communities and spaces: $e')); }