change community and spaces in user manage

This commit is contained in:
mohammad
2025-02-27 12:29:12 +03:00
parent 70cb12236b
commit fd24d6bd27
2 changed files with 82 additions and 45 deletions

View File

@ -219,6 +219,9 @@ class UserSpaceModel {
final double x; final double x;
final double y; final double y;
final String icon; final String icon;
final String communityUuid;
//communityUuid
UserSpaceModel({ UserSpaceModel({
required this.uuid, required this.uuid,
@ -231,22 +234,23 @@ class UserSpaceModel {
required this.x, required this.x,
required this.y, required this.y,
required this.icon, required this.icon,
required this.communityUuid,
}); });
/// Create a [UserSpaceModel] from JSON data /// Create a [UserSpaceModel] from JSON data
factory UserSpaceModel.fromJson(Map<String, dynamic> json) { factory UserSpaceModel.fromJson(Map<String, dynamic> json) {
return UserSpaceModel( return UserSpaceModel(
uuid: json['uuid'] as String, uuid: json['uuid'] as String,
createdAt: json['createdAt'] as String, createdAt: json['createdAt'] as String,
updatedAt: json['updatedAt'] as String, updatedAt: json['updatedAt'] as String,
spaceTuyaUuid: json['spaceTuyaUuid'] as String?, spaceTuyaUuid: json['spaceTuyaUuid'] as String?,
spaceName: json['spaceName'] as String, spaceName: json['spaceName'] as String,
invitationCode: json['invitationCode'] as String?, invitationCode: json['invitationCode'] as String?,
disabled: json['disabled'] as bool, disabled: json['disabled'] as bool,
x: (json['x'] as num).toDouble(), x: (json['x'] as num).toDouble(),
y: (json['y'] as num).toDouble(), y: (json['y'] as num).toDouble(),
icon: json['icon'] as String, icon: json['icon'] as String,
); communityUuid: json['communityUuid'] as String);
} }
/// Convert the [UserSpaceModel] to JSON /// Convert the [UserSpaceModel] to JSON
@ -262,6 +266,7 @@ class UserSpaceModel {
'x': x, 'x': x,
'y': y, 'y': y,
'icon': icon, 'icon': icon,
'communityUuid': communityUuid
}; };
} }
} }

View File

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