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