add events for select space and community

This commit is contained in:
hannathkadher
2024-11-28 19:45:24 +04:00
parent 68ae992963
commit d02d680520
2 changed files with 82 additions and 21 deletions

View File

@ -7,20 +7,24 @@ import 'package:syncrow_web/pages/spaces_management/bloc/space_management_state.
import 'package:syncrow_web/services/product_api.dart';
import 'package:syncrow_web/services/space_mana_api.dart';
class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementState> {
class SpaceManagementBloc
extends Bloc<SpaceManagementEvent, SpaceManagementState> {
final CommunitySpaceManagementApi _api;
final ProductApi _productApi;
List<ProductModel>? _cachedProducts;
SpaceManagementBloc(this._api, this._productApi) : super(SpaceManagementInitial()) {
SpaceManagementBloc(this._api, this._productApi)
: super(SpaceManagementInitial()) {
on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces);
on<UpdateSpacePositionEvent>(_onUpdateSpacePosition);
on<CreateCommunityEvent>(_onCreateCommunity);
on<SaveSpacesEvent>(_onSaveSpaces);
on<FetchProductsEvent>(_onFetchProducts);
on<SelectCommunityEvent>(_onSelectCommunity);
on<DeleteCommunityEvent>(_onCommunityDelete);
on<UpdateCommunityEvent>(_onUpdateCommunity);
on<SaveSpacesEvent>(_onSaveSpaces);
on<FetchProductsEvent>(_onFetchProducts);
on<SelectSpaceEvent>(_onSelectSpace);
}
void _onUpdateCommunity(
@ -30,22 +34,23 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
final previousState = state;
try {
emit(SpaceManagementLoading());
final success = await _api.updateCommunity(event.communityUuid, event.name);
final success =
await _api.updateCommunity(event.communityUuid, event.name);
if (success) {
if (previousState is SpaceManagementLoaded) {
final updatedCommunities = List<CommunityModel>.from(previousState.communities);
for(var community in updatedCommunities){
if(community.uuid == event.communityUuid){
final updatedCommunities =
List<CommunityModel>.from(previousState.communities);
for (var community in updatedCommunities) {
if (community.uuid == event.communityUuid) {
community.name = event.name;
break;
}
}
emit(SpaceManagementLoaded(
communities: updatedCommunities,
products: previousState.products,
selectedCommunity: previousState.selectedCommunity,
));
communities: updatedCommunities,
products: previousState.products,
selectedCommunity: previousState.selectedCommunity,
));
}
} else {
emit(const SpaceManagementError('Failed to update the community.'));
@ -88,7 +93,8 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async {
List<SpaceModel> spaces = await _api.getSpaceHierarchy(community.uuid);
List<SpaceModel> spaces =
await _api.getSpaceHierarchy(community.uuid);
return CommunityModel(
uuid: community.uuid,
createdAt: community.createdAt,
@ -101,7 +107,8 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
}).toList(),
);
emit(SpaceManagementLoaded(communities: updatedCommunities, products: _cachedProducts ?? []));
emit(SpaceManagementLoaded(
communities: updatedCommunities, products: _cachedProducts ?? []));
} catch (e) {
emit(SpaceManagementError('Error loading communities and spaces: $e'));
}
@ -139,12 +146,14 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
emit(SpaceManagementLoading());
try {
CommunityModel? newCommunity = await _api.createCommunity(event.name, event.description);
CommunityModel? newCommunity =
await _api.createCommunity(event.name, event.description);
if (newCommunity != null) {
if (previousState is SpaceManagementLoaded) {
final updatedCommunities = List<CommunityModel>.from(previousState.communities)
..add(newCommunity);
final updatedCommunities =
List<CommunityModel>.from(previousState.communities)
..add(newCommunity);
emit(SpaceManagementLoaded(
communities: updatedCommunities,
products: _cachedProducts ?? [],
@ -158,6 +167,53 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
}
}
void _onSelectCommunity(
SelectCommunityEvent event,
Emitter<SpaceManagementState> emit,
) async {
_handleCommunitySpaceStateUpdate(
emit: emit,
selectedCommunity: event.selectedCommunity,
selectedSpace: null,
);
}
void _onSelectSpace(
SelectSpaceEvent event,
Emitter<SpaceManagementState> emit,
) {
_handleCommunitySpaceStateUpdate(
emit: emit,
selectedCommunity: event.selectedCommunity,
selectedSpace: event.selectedSpace,
);
}
void _handleCommunitySpaceStateUpdate({
required Emitter<SpaceManagementState> emit,
CommunityModel? selectedCommunity,
SpaceModel? selectedSpace,
}) {
final previousState = state;
emit(SpaceManagementLoading());
try {
if (previousState is SpaceManagementLoaded) {
final communities = List<CommunityModel>.from(
(previousState as dynamic).communities,
);
emit(SpaceManagementLoaded(
communities: communities,
products: _cachedProducts ?? [],
selectedCommunity: selectedCommunity,
selectedSpace: selectedSpace,
));
}
} catch (e) {
emit(SpaceManagementError('Error updating state: $e'));
}
}
void _onSaveSpaces(
SaveSpacesEvent event,
Emitter<SpaceManagementState> emit,
@ -166,7 +222,8 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
emit(SpaceManagementLoading());
try {
final updatedSpaces = await saveSpacesHierarchically(event.spaces, event.communityUuid);
final updatedSpaces =
await saveSpacesHierarchically(event.spaces, event.communityUuid);
emit(SpaceCreationSuccess(spaces: updatedSpaces));
add(LoadCommunityAndSpacesEvent());
} catch (e) {

View File

@ -17,10 +17,14 @@ class SpaceManagementLoading extends SpaceManagementState {}
class SpaceManagementLoaded extends SpaceManagementState {
final List<CommunityModel> communities;
final List<ProductModel> products;
CommunityModel? selectedCommunity; // Include products in the state
CommunityModel? selectedCommunity;
SpaceModel? selectedSpace;
SpaceManagementLoaded(
{required this.communities, required this.products, this.selectedCommunity});
{required this.communities,
required this.products,
this.selectedCommunity,
this.selectedSpace});
}
class SpaceCreationSuccess extends SpaceManagementState {