mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
space load on space save
This commit is contained in:
@ -60,41 +60,42 @@ class SpaceManagementBloc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _onloadProducts() async {
|
||||||
|
if (_cachedProducts == null) {
|
||||||
|
final products = await _productApi.fetchProducts();
|
||||||
|
_cachedProducts = products;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void _onFetchProducts(
|
void _onFetchProducts(
|
||||||
FetchProductsEvent event,
|
FetchProductsEvent event,
|
||||||
Emitter<SpaceManagementState> emit,
|
Emitter<SpaceManagementState> emit,
|
||||||
) async {
|
) async {
|
||||||
if (_cachedProducts != null) {
|
|
||||||
// Products are already cached, no need to fetch again
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final products = await _productApi.fetchProducts();
|
_onloadProducts();
|
||||||
_cachedProducts = products; // Cache the products locally
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(SpaceManagementError('Error fetching products: $e'));
|
emit(SpaceManagementError('Error fetching products: $e'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<List<SpaceModel>> _fetchSpacesForCommunity(
|
||||||
|
String communityUuid) async {
|
||||||
|
return await _api.getSpaceHierarchy(communityUuid);
|
||||||
|
}
|
||||||
|
|
||||||
void _onLoadCommunityAndSpaces(
|
void _onLoadCommunityAndSpaces(
|
||||||
LoadCommunityAndSpacesEvent event,
|
LoadCommunityAndSpacesEvent event,
|
||||||
Emitter<SpaceManagementState> emit,
|
Emitter<SpaceManagementState> emit,
|
||||||
) async {
|
) async {
|
||||||
emit(SpaceManagementLoading());
|
emit(SpaceManagementLoading());
|
||||||
try {
|
try {
|
||||||
if (_cachedProducts == null) {
|
_onloadProducts();
|
||||||
final products = await _productApi.fetchProducts();
|
|
||||||
_cachedProducts = products;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch all communities
|
|
||||||
List<CommunityModel> communities = await _api.fetchCommunities();
|
List<CommunityModel> communities = await _api.fetchCommunities();
|
||||||
|
|
||||||
List<CommunityModel> updatedCommunities = await Future.wait(
|
List<CommunityModel> updatedCommunities = await Future.wait(
|
||||||
communities.map((community) async {
|
communities.map((community) async {
|
||||||
List<SpaceModel> spaces =
|
List<SpaceModel> spaces =
|
||||||
await _api.getSpaceHierarchy(community.uuid);
|
await _fetchSpacesForCommunity(community.uuid);
|
||||||
return CommunityModel(
|
return CommunityModel(
|
||||||
uuid: community.uuid,
|
uuid: community.uuid,
|
||||||
createdAt: community.createdAt,
|
createdAt: community.createdAt,
|
||||||
@ -225,16 +226,52 @@ class SpaceManagementBloc
|
|||||||
try {
|
try {
|
||||||
final updatedSpaces =
|
final updatedSpaces =
|
||||||
await saveSpacesHierarchically(event.spaces, event.communityUuid);
|
await saveSpacesHierarchically(event.spaces, event.communityUuid);
|
||||||
|
|
||||||
|
final allSpaces = await _fetchSpacesForCommunity(event.communityUuid);
|
||||||
|
|
||||||
emit(SpaceCreationSuccess(spaces: updatedSpaces));
|
emit(SpaceCreationSuccess(spaces: updatedSpaces));
|
||||||
add(LoadCommunityAndSpacesEvent());
|
|
||||||
|
if (previousState is SpaceManagementLoaded) {
|
||||||
|
_updateLoadedState(
|
||||||
|
previousState,
|
||||||
|
allSpaces,
|
||||||
|
event.communityUuid,
|
||||||
|
emit,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
add(LoadCommunityAndSpacesEvent());
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(SpaceManagementError('Error saving spaces: $e'));
|
emit(SpaceManagementError('Error saving spaces: $e'));
|
||||||
|
|
||||||
if (previousState is SpaceManagementLoaded) {
|
if (previousState is SpaceManagementLoaded) {
|
||||||
emit(previousState);
|
emit(previousState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _updateLoadedState(
|
||||||
|
SpaceManagementLoaded previousState,
|
||||||
|
List<SpaceModel> allSpaces,
|
||||||
|
String communityUuid,
|
||||||
|
Emitter<SpaceManagementState> emit,
|
||||||
|
) {
|
||||||
|
final communities = List<CommunityModel>.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<List<SpaceModel>> saveSpacesHierarchically(
|
Future<List<SpaceModel>> saveSpacesHierarchically(
|
||||||
List<SpaceModel> spaces, String communityUuid) async {
|
List<SpaceModel> spaces, String communityUuid) async {
|
||||||
final orderedSpaces = flattenHierarchy(spaces);
|
final orderedSpaces = flattenHierarchy(spaces);
|
||||||
@ -245,7 +282,6 @@ class SpaceManagementBloc
|
|||||||
|
|
||||||
for (var parent in parentsToDelete) {
|
for (var parent in parentsToDelete) {
|
||||||
try {
|
try {
|
||||||
// Ensure parent.uuid is not null before calling the API
|
|
||||||
if (parent.uuid != null) {
|
if (parent.uuid != null) {
|
||||||
await _api.deleteSpace(communityUuid, parent.uuid!);
|
await _api.deleteSpace(communityUuid, parent.uuid!);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user