load space models

This commit is contained in:
hannathkadher
2025-01-12 09:25:29 +04:00
parent cfc1b544b7
commit 6591ef1664

View File

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