diff --git a/lib/pages/roles_and_permission/model/edit_user_model.dart b/lib/pages/roles_and_permission/model/edit_user_model.dart index 4075ac12..81430fa3 100644 --- a/lib/pages/roles_and_permission/model/edit_user_model.dart +++ b/lib/pages/roles_and_permission/model/edit_user_model.dart @@ -219,6 +219,9 @@ class UserSpaceModel { final double x; final double y; final String icon; + final String communityUuid; + + //communityUuid UserSpaceModel({ required this.uuid, @@ -231,22 +234,23 @@ class UserSpaceModel { required this.x, required this.y, required this.icon, + required this.communityUuid, }); /// Create a [UserSpaceModel] from JSON data factory UserSpaceModel.fromJson(Map json) { return UserSpaceModel( - uuid: json['uuid'] as String, - createdAt: json['createdAt'] as String, - updatedAt: json['updatedAt'] as String, - spaceTuyaUuid: json['spaceTuyaUuid'] as String?, - spaceName: json['spaceName'] as String, - invitationCode: json['invitationCode'] as String?, - disabled: json['disabled'] as bool, - x: (json['x'] as num).toDouble(), - y: (json['y'] as num).toDouble(), - icon: json['icon'] as String, - ); + uuid: json['uuid'] as String, + createdAt: json['createdAt'] as String, + updatedAt: json['updatedAt'] as String, + spaceTuyaUuid: json['spaceTuyaUuid'] as String?, + spaceName: json['spaceName'] as String, + invitationCode: json['invitationCode'] as String?, + disabled: json['disabled'] as bool, + x: (json['x'] as num).toDouble(), + y: (json['y'] as num).toDouble(), + icon: json['icon'] as String, + communityUuid: json['communityUuid'] as String); } /// Convert the [UserSpaceModel] to JSON @@ -262,6 +266,7 @@ class UserSpaceModel { 'x': x, 'y': y, 'icon': icon, + 'communityUuid': communityUuid }; } } diff --git a/lib/pages/roles_and_permission/users_page/add_user_dialog/bloc/users_bloc.dart b/lib/pages/roles_and_permission/users_page/add_user_dialog/bloc/users_bloc.dart index 2fed09da..54187152 100644 --- a/lib/pages/roles_and_permission/users_page/add_user_dialog/bloc/users_bloc.dart +++ b/lib/pages/roles_and_permission/users_page/add_user_dialog/bloc/users_bloc.dart @@ -10,6 +10,7 @@ import 'package:syncrow_web/pages/roles_and_permission/users_page/add_user_dialo import 'package:syncrow_web/pages/roles_and_permission/users_page/add_user_dialog/model/permission_option_model.dart'; import 'package:syncrow_web/pages/roles_and_permission/users_page/add_user_dialog/model/tree_node_model.dart'; import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart'; +import 'package:syncrow_web/pages/space_tree/bloc/space_tree_event.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart'; import 'package:syncrow_web/services/space_mana_api.dart'; @@ -62,9 +63,11 @@ class UsersBloc extends Bloc { int numberSpaces = 0; int numberRole = 0; - void isCompleteSpacesFun(CheckSpacesStepStatus event, Emitter emit) { + void isCompleteSpacesFun( + CheckSpacesStepStatus event, Emitter emit) { emit(UsersLoadingState()); - var spaceBloc = NavigationService.navigatorKey.currentContext!.read(); + var spaceBloc = + NavigationService.navigatorKey.currentContext!.read(); isCompleteSpaces = spaceBloc.state.selectedCommunities.isNotEmpty; emit(ChangeStatusSteps()); @@ -76,16 +79,19 @@ class UsersBloc extends Bloc { emit(ChangeStatusSteps()); } - Future> _fetchSpacesForCommunity(String communityUuid) async { + Future> _fetchSpacesForCommunity( + String communityUuid) async { final projectUuid = await ProjectManager.getProjectUUID() ?? ''; - return await CommunitySpaceManagementApi().getSpaceHierarchy(communityUuid, projectUuid); + return await CommunitySpaceManagementApi() + .getSpaceHierarchy(communityUuid, projectUuid); } List updatedCommunities = []; List spacesNodes = []; List communityIds = []; - _onLoadCommunityAndSpaces(LoadCommunityAndSpacesEvent event, Emitter emit) async { + _onLoadCommunityAndSpaces( + LoadCommunityAndSpacesEvent event, Emitter emit) async { try { emit(UsersLoadingState()); final projectUuid = await ProjectManager.getProjectUUID() ?? ''; @@ -95,7 +101,8 @@ class UsersBloc extends Bloc { communityIds = communities.map((community) => community.uuid).toList(); updatedCommunities = await Future.wait( communities.map((community) async { - List spaces = await _fetchSpacesForCommunity(community.uuid); + List spaces = + await _fetchSpacesForCommunity(community.uuid); spacesNodes = _buildTreeNodes(spaces); return TreeNode( uuid: community.uuid, @@ -122,7 +129,8 @@ class UsersBloc extends Bloc { // Build tree nodes from your data model. List _buildTreeNodes(List spaces) { return spaces.map((space) { - List childNodes = space.children.isNotEmpty ? _buildTreeNodes(space.children) : []; + List childNodes = + space.children.isNotEmpty ? _buildTreeNodes(space.children) : []; return TreeNode( uuid: space.uuid!, title: space.name, @@ -183,7 +191,8 @@ class UsersBloc extends Bloc { bool _searchAndHighlightNodes(List nodes, String searchTerm) { bool anyMatch = false; for (var node in nodes) { - bool isMatch = node.title.toLowerCase().contains(searchTerm.toLowerCase()); + bool isMatch = + node.title.toLowerCase().contains(searchTerm.toLowerCase()); bool childMatch = _searchAndHighlightNodes(node.children, searchTerm); node.isHighlighted = isMatch || childMatch; @@ -195,7 +204,8 @@ class UsersBloc extends Bloc { List _filterNodes(List nodes, String searchTerm) { List filteredNodes = []; for (var node in nodes) { - bool isMatch = node.title.toLowerCase().contains(searchTerm.toLowerCase()); + bool isMatch = + node.title.toLowerCase().contains(searchTerm.toLowerCase()); List filteredChildren = _filterNodes(node.children, searchTerm); if (isMatch || filteredChildren.isNotEmpty) { node.isHighlighted = isMatch; @@ -307,8 +317,8 @@ class UsersBloc extends Bloc { _getPermissions(PermissionEvent event, Emitter emit) async { try { emit(UsersLoadingState()); - permissions = await UserPermissionApi() - .fetchPermission(event.roleUuid == "" ? roles.first.uuid : event.roleUuid); + permissions = await UserPermissionApi().fetchPermission( + event.roleUuid == "" ? roles.first.uuid : event.roleUuid); roleSelected = event.roleUuid!; emit(RolePermissionInitial()); } catch (e) { @@ -319,7 +329,8 @@ class UsersBloc extends Bloc { bool _searchRolePermission(List nodes, String searchTerm) { bool anyMatch = false; for (var node in nodes) { - bool isMatch = node.title.toLowerCase().contains(searchTerm.toLowerCase()); + bool isMatch = + node.title.toLowerCase().contains(searchTerm.toLowerCase()); bool childMatch = _searchRolePermission(node.subOptions, searchTerm); node.isHighlighted = isMatch || childMatch; anyMatch = anyMatch || node.isHighlighted; @@ -335,11 +346,7 @@ class UsersBloc extends Bloc { // List selectedIds = // getSelectedIds(updatedCommunities).where((id) => !communityIds.contains(id)).toList(); - List selectedSpacesId = []; - var spaceBloc = NavigationService.navigatorKey.currentContext!.read(); - for (var community in spaceBloc.state.selectedCommunities) { - selectedSpacesId.addAll(spaceBloc.state.selectedCommunityAndSpaces[community] ?? []); - } + List selectedSpacesId = getSelectedSpacesIds(); // List selectedIds = getSelectedIds(updatedCommunities); bool res = await UserPermissionApi().sendInviteUser( @@ -379,11 +386,20 @@ class UsersBloc extends Bloc { } } + List getSelectedSpacesIds() { + List selectedSpacesId = []; + var spaceBloc = + NavigationService.navigatorKey.currentContext!.read(); + for (var community in spaceBloc.state.selectedCommunities) { + selectedSpacesId + .addAll(spaceBloc.state.selectedCommunityAndSpaces[community] ?? []); + } + return selectedSpacesId; + } + _editInviteUser(EditInviteUsers event, Emitter emit) async { try { emit(UsersLoadingState()); - List selectedIds = - getSelectedIds(updatedCommunities).where((id) => !communityIds.contains(id)).toList(); final projectUuid = await ProjectManager.getProjectUUID() ?? ''; bool res = await UserPermissionApi().editInviteUser( @@ -393,7 +409,7 @@ class UsersBloc extends Bloc { lastName: lastNameController.text, phoneNumber: phoneController.text, roleUuid: roleSelected, - spaceUuids: selectedIds, + spaceUuids: getSelectedSpacesIds(), projectUuid: projectUuid); if (res == true) { showCustomDialog( @@ -436,7 +452,8 @@ class UsersBloc extends Bloc { String checkEmailValid = ''; - Future checkEmail(CheckEmailEvent event, Emitter emit) async { + Future checkEmail( + CheckEmailEvent event, Emitter emit) async { emit(UsersLoadingState()); String? res = await UserPermissionApi().checkEmail( emailController.text, @@ -460,7 +477,8 @@ class UsersBloc extends Bloc { isEmailValid && isEmailServerValid; } else { - isCompleteBasics = firstNameController.text.isNotEmpty && lastNameController.text.isNotEmpty; + isCompleteBasics = firstNameController.text.isNotEmpty && + lastNameController.text.isNotEmpty; } emit(ChangeStatusSteps()); emit(ValidateBasics()); @@ -497,10 +515,13 @@ class UsersBloc extends Bloc { emit(UsersLoadingState()); try { + var spaceBloc = + NavigationService.navigatorKey.currentContext!.read(); final projectUuid = await ProjectManager.getProjectUUID() ?? ''; if (event.uuid?.isNotEmpty ?? false) { - final res = await UserPermissionApi().fetchUserById(event.uuid, projectUuid); + final res = + await UserPermissionApi().fetchUserById(event.uuid, projectUuid); if (res != null) { // Populate the text controllers @@ -510,16 +531,24 @@ class UsersBloc extends Bloc { phoneController.text = res.phoneNumber ?? ''; jobTitleController.text = res.jobTitle ?? ''; res.roleType; - if (updatedCommunities.isNotEmpty) { - // Create a list of UUIDs to mark - final uuidsToMark = res.spaces.map((space) => space.uuid).toList(); - // Print all IDs and mark nodes in updatedCommunities - debugPrint('Printing and marking nodes in updatedCommunities:'); - _printAndMarkNodes(updatedCommunities, uuidsToMark); - } + res.spaces.map((space) { + selectedIds.add(space.uuid); + CommunityModel community = spaceBloc.state.communityList + .firstWhere((item) => item.uuid == space.communityUuid); + spaceBloc.add(OnSpaceSelected(community, space.uuid, [])); + }).toList(); + + // if (updatedCommunities.isNotEmpty) { + // // Create a list of UUIDs to mark + // final uuidsToMark = res.spaces.map((space) => space.uuid).toList(); + // // Print all IDs and mark nodes in updatedCommunities + // debugPrint('Printing and marking nodes in updatedCommunities:'); + // _printAndMarkNodes(updatedCommunities, uuidsToMark); + // } final roleId = roles .firstWhere((element) => - element.type == res.roleType.toString().toLowerCase().replaceAll("_", " ")) + element.type == + res.roleType.toString().toLowerCase().replaceAll("_", " ")) .uuid; debugPrint('Role ID: $roleId'); roleSelected = roleId; @@ -530,14 +559,16 @@ class UsersBloc extends Bloc { } catch (_) {} } - void _printAndMarkNodes(List nodes, List uuidsToMark, [int level = 0]) { + void _printAndMarkNodes(List nodes, List uuidsToMark, + [int level = 0]) { for (final node in nodes) { if (uuidsToMark.contains(node.uuid)) { node.isChecked = true; debugPrint( '${' ' * level}MATCH FOUND: Node ID: ${node.uuid}, Title: ${node.title} is marked as checked.'); } else { - debugPrint('${' ' * level}Node ID: ${node.uuid}, Title: ${node.title}'); + debugPrint( + '${' ' * level}Node ID: ${node.uuid}, Title: ${node.title}'); } if (node.children.isNotEmpty) { _printAndMarkNodes(node.children, uuidsToMark, level + 1); @@ -590,7 +621,8 @@ class UsersBloc extends Bloc { bool _areAllChildrenChecked(TreeNode node) { return node.children.isNotEmpty && node.children.every((child) => - child.isChecked && (child.children.isEmpty || _areAllChildrenChecked(child))); + child.isChecked && + (child.children.isEmpty || _areAllChildrenChecked(child))); } TreeNode? _findParent(List nodes, TreeNode target) {