reduced no of calls on load

This commit is contained in:
hannathkadher
2025-02-28 19:37:50 +04:00
parent b84513c09d
commit 7607e5f80d

View File

@ -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<SpaceManagementEvent, SpaceManagementState> {
@ -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<CommunityModel> 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'));
}