mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
add events for select space and community
This commit is contained in:
@ -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,12 +34,14 @@ 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;
|
||||
}
|
||||
@ -45,7 +51,6 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
|
||||
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,11 +146,13 @@ 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)
|
||||
final updatedCommunities =
|
||||
List<CommunityModel>.from(previousState.communities)
|
||||
..add(newCommunity);
|
||||
emit(SpaceManagementLoaded(
|
||||
communities: updatedCommunities,
|
||||
@ -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) {
|
||||
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user