update space bloc on community and space change

This commit is contained in:
hannathkadher
2025-03-05 12:28:00 +04:00
parent 51cfa8d5ae
commit 6e90f81760
6 changed files with 114 additions and 39 deletions

View File

@ -1,2 +1,2 @@
ENV_NAME=development ENV_NAME=development
BASE_URL=https://syncrow-dev.azurewebsites.net BASE_URL=http://localhost:4001

View File

@ -17,6 +17,32 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
on<ClearAllData>(_clearAllData); on<ClearAllData>(_clearAllData);
on<ClearCachedData>(_clearCachedData); on<ClearCachedData>(_clearCachedData);
on<OnCommunityAdded>(_onCommunityAdded); on<OnCommunityAdded>(_onCommunityAdded);
on<OnCommunityUpdated>(_onCommunityUpdate);
}
void _onCommunityUpdate(
OnCommunityUpdated event,
Emitter<SpaceTreeState> emit,
) async {
emit(SpaceTreeLoadingState());
try {
final updatedCommunity = event.updatedCommunity;
final updatedCommunities =
List<CommunityModel>.from(state.communityList);
final index = updatedCommunities
.indexWhere((community) => community.uuid == updatedCommunity.uuid);
if (index != -1) {
updatedCommunities[index] = updatedCommunity;
emit(state.copyWith(communitiesList: updatedCommunities));
} else {
emit(SpaceTreeErrorState('Community not found in the list.'));
}
} catch (e) {
emit(SpaceTreeErrorState('Error updating community: $e'));
}
} }
_fetchSpaces(InitialEvent event, Emitter<SpaceTreeState> emit) async { _fetchSpaces(InitialEvent event, Emitter<SpaceTreeState> emit) async {
@ -29,8 +55,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
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 CommunitySpaceManagementApi()
await CommunitySpaceManagementApi().getSpaceHierarchy(community.uuid, projectUuid); .getSpaceHierarchy(community.uuid, projectUuid);
return CommunityModel( return CommunityModel(
uuid: community.uuid, uuid: community.uuid,
@ -45,23 +71,27 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
); );
emit(state.copyWith( emit(state.copyWith(
communitiesList: updatedCommunities, expandedCommunity: [], expandedSpaces: [])); communitiesList: updatedCommunities,
expandedCommunity: [],
expandedSpaces: []));
} catch (e) { } catch (e) {
emit(SpaceTreeErrorState('Error loading communities and spaces: $e')); emit(SpaceTreeErrorState('Error loading communities and spaces: $e'));
} }
} }
void _onCommunityAdded(OnCommunityAdded event, Emitter<SpaceTreeState> emit) async { void _onCommunityAdded(
final updatedCommunities = List<CommunityModel>.from(state.communityList); OnCommunityAdded event, Emitter<SpaceTreeState> emit) async {
updatedCommunities.add(event.newCommunity); final updatedCommunities = List<CommunityModel>.from(state.communityList);
updatedCommunities.add(event.newCommunity);
emit(state.copyWith(communitiesList: updatedCommunities)); emit(state.copyWith(communitiesList: updatedCommunities));
} }
_onCommunityExpanded(
_onCommunityExpanded(OnCommunityExpanded event, Emitter<SpaceTreeState> emit) async { OnCommunityExpanded event, Emitter<SpaceTreeState> emit) async {
try { try {
List<String> updatedExpandedCommunityList = List.from(state.expandedCommunities); List<String> updatedExpandedCommunityList =
List.from(state.expandedCommunities);
if (updatedExpandedCommunityList.contains(event.communityId)) { if (updatedExpandedCommunityList.contains(event.communityId)) {
updatedExpandedCommunityList.remove(event.communityId); updatedExpandedCommunityList.remove(event.communityId);
@ -93,14 +123,19 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
} }
} }
_onCommunitySelected(OnCommunitySelected event, Emitter<SpaceTreeState> emit) async { _onCommunitySelected(
OnCommunitySelected event, Emitter<SpaceTreeState> emit) async {
try { try {
List<String> updatedSelectedCommunities = List<String> updatedSelectedCommunities =
List.from(state.selectedCommunities.toSet().toList()); List.from(state.selectedCommunities.toSet().toList());
List<String> updatedSelectedSpaces = List.from(state.selectedSpaces.toSet().toList()); List<String> updatedSelectedSpaces =
List<String> updatedSoldChecks = List.from(state.soldCheck.toSet().toList()); List.from(state.selectedSpaces.toSet().toList());
Map<String, List<String>> communityAndSpaces = Map.from(state.selectedCommunityAndSpaces); List<String> updatedSoldChecks =
List<String> selectedSpacesInCommunity = communityAndSpaces[event.communityId] ?? []; List.from(state.soldCheck.toSet().toList());
Map<String, List<String>> communityAndSpaces =
Map.from(state.selectedCommunityAndSpaces);
List<String> selectedSpacesInCommunity =
communityAndSpaces[event.communityId] ?? [];
List<String> childrenIds = _getAllChildIds(event.children); List<String> childrenIds = _getAllChildIds(event.children);
@ -133,11 +168,15 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
try { try {
List<String> updatedSelectedCommunities = List<String> updatedSelectedCommunities =
List.from(state.selectedCommunities.toSet().toList()); List.from(state.selectedCommunities.toSet().toList());
List<String> updatedSelectedSpaces = List.from(state.selectedSpaces.toSet().toList()); List<String> updatedSelectedSpaces =
List<String> updatedSoldChecks = List.from(state.soldCheck.toSet().toList()); List.from(state.selectedSpaces.toSet().toList());
Map<String, List<String>> communityAndSpaces = Map.from(state.selectedCommunityAndSpaces); List<String> updatedSoldChecks =
List.from(state.soldCheck.toSet().toList());
Map<String, List<String>> communityAndSpaces =
Map.from(state.selectedCommunityAndSpaces);
List<String> selectedSpacesInCommunity = communityAndSpaces[event.communityModel.uuid] ?? []; List<String> selectedSpacesInCommunity =
communityAndSpaces[event.communityModel.uuid] ?? [];
List<String> childrenIds = _getAllChildIds(event.children); List<String> childrenIds = _getAllChildIds(event.children);
bool isChildSelected = false; bool isChildSelected = false;
@ -160,9 +199,11 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
selectedSpacesInCommunity.addAll(childrenIds); selectedSpacesInCommunity.addAll(childrenIds);
} }
List<String> spaces = _getThePathToChild(event.communityModel.uuid, event.spaceId); List<String> spaces =
_getThePathToChild(event.communityModel.uuid, event.spaceId);
for (String space in spaces) { for (String space in spaces) {
if (!updatedSelectedSpaces.contains(space) && !updatedSoldChecks.contains(space)) { if (!updatedSelectedSpaces.contains(space) &&
!updatedSoldChecks.contains(space)) {
updatedSoldChecks.add(space); updatedSoldChecks.add(space);
} }
} }
@ -185,7 +226,9 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
updatedSoldChecks.remove(event.spaceId); updatedSoldChecks.remove(event.spaceId);
List<String> parents = List<String> parents =
_getThePathToChild(event.communityModel.uuid, event.spaceId).toSet().toList(); _getThePathToChild(event.communityModel.uuid, event.spaceId)
.toSet()
.toList();
if (updatedSelectedSpaces.isEmpty) { if (updatedSelectedSpaces.isEmpty) {
updatedSoldChecks.removeWhere(parents.contains); updatedSoldChecks.removeWhere(parents.contains);
@ -193,7 +236,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
} else { } else {
// Check if any parent has selected children // Check if any parent has selected children
for (String space in parents) { for (String space in parents) {
if (!_noChildrenSelected(event.communityModel, space, updatedSelectedSpaces, parents)) { if (!_noChildrenSelected(
event.communityModel, space, updatedSelectedSpaces, parents)) {
updatedSoldChecks.remove(space); updatedSoldChecks.remove(space);
} }
} }
@ -218,8 +262,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
} }
} }
_noChildrenSelected( _noChildrenSelected(CommunityModel community, String spaceId,
CommunityModel community, String spaceId, List<String> selectedSpaces, List<String> parents) { List<String> selectedSpaces, List<String> parents) {
if (selectedSpaces.contains(spaceId)) { if (selectedSpaces.contains(spaceId)) {
return true; return true;
} }
@ -246,10 +290,11 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
// Filter communities and expand only those that match the query // Filter communities and expand only those that match the query
filteredCommunity = communities.where((community) { filteredCommunity = communities.where((community) {
final containsQueryInCommunity = final containsQueryInCommunity = community.name
community.name.toLowerCase().contains(event.searchQuery.toLowerCase()); .toLowerCase()
final containsQueryInSpaces = .contains(event.searchQuery.toLowerCase());
community.spaces.any((space) => _containsQuery(space, event.searchQuery.toLowerCase())); final containsQueryInSpaces = community.spaces.any(
(space) => _containsQuery(space, event.searchQuery.toLowerCase()));
return containsQueryInCommunity || containsQueryInSpaces; return containsQueryInCommunity || containsQueryInSpaces;
}).toList(); }).toList();
@ -302,8 +347,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
// Helper function to determine if any space or its children match the search query // Helper function to determine if any space or its children match the search query
bool _containsQuery(SpaceModel space, String query) { bool _containsQuery(SpaceModel space, String query) {
final matchesSpace = space.name.toLowerCase().contains(query); final matchesSpace = space.name.toLowerCase().contains(query);
final matchesChildren = final matchesChildren = space.children.any((child) =>
space.children.any((child) => _containsQuery(child, query)); // Recursive check for children _containsQuery(child, query)); // Recursive check for children
return matchesSpace || matchesChildren; return matchesSpace || matchesChildren;
} }
@ -326,8 +371,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
return children; return children;
} }
bool _anySpacesSelectedInCommunity( bool _anySpacesSelectedInCommunity(CommunityModel community,
CommunityModel community, List<String> selectedSpaces, List<String> partialCheckedList) { List<String> selectedSpaces, List<String> partialCheckedList) {
bool result = false; bool result = false;
List<String> ids = _getAllChildIds(community.spaces); List<String> ids = _getAllChildIds(community.spaces);
for (var id in ids) { for (var id in ids) {
@ -356,7 +401,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
return ids; return ids;
} }
List<String> _getAllParentsIds(SpaceModel child, String spaceId, List<String> listIds) { List<String> _getAllParentsIds(
SpaceModel child, String spaceId, List<String> listIds) {
List<String> ids = listIds; List<String> ids = listIds;
ids.add(child.uuid ?? ''); ids.add(child.uuid ?? '');

View File

@ -77,6 +77,15 @@ class OnCommunityAdded extends SpaceTreeEvent {
List<Object> get props => [newCommunity]; List<Object> get props => [newCommunity];
} }
class OnCommunityUpdated extends SpaceTreeEvent {
final CommunityModel updatedCommunity;
const OnCommunityUpdated(this.updatedCommunity);
@override
List<Object> get props => [updatedCommunity];
}
class ClearAllData extends SpaceTreeEvent {} class ClearAllData extends SpaceTreeEvent {}
class ClearCachedData extends SpaceTreeEvent {} class ClearCachedData extends SpaceTreeEvent {}

View File

@ -139,7 +139,6 @@ class SpaceManagementBloc
UpdateCommunityEvent event, UpdateCommunityEvent event,
Emitter<SpaceManagementState> emit, Emitter<SpaceManagementState> emit,
) async { ) async {
final previousState = state; final previousState = state;
try { try {
final projectUuid = await ProjectManager.getProjectUUID() ?? ''; final projectUuid = await ProjectManager.getProjectUUID() ?? '';
@ -154,6 +153,8 @@ class SpaceManagementBloc
for (var community in updatedCommunities) { for (var community in updatedCommunities) {
if (community.uuid == event.communityUuid) { if (community.uuid == event.communityUuid) {
community.name = event.name; community.name = event.name;
_spaceTreeBloc.add(OnCommunityAdded(community));
break; break;
} }
} }
@ -456,8 +457,6 @@ class SpaceManagementBloc
event.communityUuid, event.communityUuid,
emit, emit,
); );
} else {
// add(LoadCommunityAndSpacesEvent());
} }
} catch (e) { } catch (e) {
emit(SpaceManagementError('Error saving spaces: $e')); emit(SpaceManagementError('Error saving spaces: $e'));
@ -481,6 +480,8 @@ class SpaceManagementBloc
for (var community in communities) { for (var community in communities) {
if (community.uuid == communityUuid) { if (community.uuid == communityUuid) {
community.spaces = allSpaces; community.spaces = allSpaces;
_spaceTreeBloc.add(OnCommunityUpdated(community));
emit(SpaceManagementLoaded( emit(SpaceManagementLoaded(
communities: communities, communities: communities,
products: _cachedProducts ?? [], products: _cachedProducts ?? [],

View File

@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/common/buttons/cancel_button.dart'; import 'package:syncrow_web/pages/common/buttons/cancel_button.dart';
import 'package:syncrow_web/pages/common/buttons/default_button.dart'; import 'package:syncrow_web/pages/common/buttons/default_button.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_event.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/bloc/create_space_model_bloc.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/bloc/create_space_model_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/bloc/create_space_model_event.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/bloc/create_space_model_event.dart';
@ -196,6 +198,12 @@ class CreateSpaceModelDialog extends StatelessWidget {
.add(CreateSpaceModel( .add(CreateSpaceModel(
newSpaceModel: newSpaceModel:
newModel)); newModel));
pageContext!
.read<
SpaceManagementBloc>()
.add(
UpdateSpaceModelCache(
newModel));
} }
Navigator.of(context) Navigator.of(context)
.pop(); // Close the dialog .pop(); // Close the dialog
@ -241,6 +249,11 @@ class CreateSpaceModelDialog extends StatelessWidget {
spaceModelUuid: spaceModelUuid:
newModel.uuid ?? newModel.uuid ??
'')); ''));
pageContext!
.read<
SpaceManagementBloc>()
.add(UpdateSpaceModelCache(
newModel));
} }
Navigator.of(context) Navigator.of(context)
.pop(); .pop();

View File

@ -4,6 +4,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart'; import 'package:flutter_svg/svg.dart';
import 'package:syncrow_web/pages/common/buttons/cancel_button.dart'; import 'package:syncrow_web/pages/common/buttons/cancel_button.dart';
import 'package:syncrow_web/pages/common/buttons/default_button.dart'; import 'package:syncrow_web/pages/common/buttons/default_button.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_event.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/bloc/space_model_bloc.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/bloc/space_model_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/bloc/space_model_event.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/bloc/space_model_event.dart';
@ -182,6 +184,10 @@ class SpaceModelCardWidget extends StatelessWidget {
pageContext!.read<SpaceModelBloc>().add( pageContext!.read<SpaceModelBloc>().add(
DeleteSpaceModel(spaceModelUuid: model.uuid ?? ''), DeleteSpaceModel(spaceModelUuid: model.uuid ?? ''),
); );
pageContext!.read<SpaceManagementBloc>().add(
DeleteSpaceModelFromCache(model.uuid ?? ''),
);
} }
}, },
); );