mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
change community and spaces in user manage
This commit is contained in:
@ -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<String, dynamic> 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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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<UsersEvent, UsersState> {
|
||||
int numberSpaces = 0;
|
||||
int numberRole = 0;
|
||||
|
||||
void isCompleteSpacesFun(CheckSpacesStepStatus event, Emitter<UsersState> emit) {
|
||||
void isCompleteSpacesFun(
|
||||
CheckSpacesStepStatus event, Emitter<UsersState> emit) {
|
||||
emit(UsersLoadingState());
|
||||
var spaceBloc = NavigationService.navigatorKey.currentContext!.read<SpaceTreeBloc>();
|
||||
var spaceBloc =
|
||||
NavigationService.navigatorKey.currentContext!.read<SpaceTreeBloc>();
|
||||
|
||||
isCompleteSpaces = spaceBloc.state.selectedCommunities.isNotEmpty;
|
||||
emit(ChangeStatusSteps());
|
||||
@ -76,16 +79,19 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
|
||||
emit(ChangeStatusSteps());
|
||||
}
|
||||
|
||||
Future<List<SpaceModel>> _fetchSpacesForCommunity(String communityUuid) async {
|
||||
Future<List<SpaceModel>> _fetchSpacesForCommunity(
|
||||
String communityUuid) async {
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
return await CommunitySpaceManagementApi().getSpaceHierarchy(communityUuid, projectUuid);
|
||||
return await CommunitySpaceManagementApi()
|
||||
.getSpaceHierarchy(communityUuid, projectUuid);
|
||||
}
|
||||
|
||||
List<TreeNode> updatedCommunities = [];
|
||||
List<TreeNode> spacesNodes = [];
|
||||
List<String> communityIds = [];
|
||||
_onLoadCommunityAndSpaces(LoadCommunityAndSpacesEvent event, Emitter<UsersState> emit) async {
|
||||
_onLoadCommunityAndSpaces(
|
||||
LoadCommunityAndSpacesEvent event, Emitter<UsersState> emit) async {
|
||||
try {
|
||||
emit(UsersLoadingState());
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
@ -95,7 +101,8 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
|
||||
communityIds = communities.map((community) => community.uuid).toList();
|
||||
updatedCommunities = await Future.wait(
|
||||
communities.map((community) async {
|
||||
List<SpaceModel> spaces = await _fetchSpacesForCommunity(community.uuid);
|
||||
List<SpaceModel> spaces =
|
||||
await _fetchSpacesForCommunity(community.uuid);
|
||||
spacesNodes = _buildTreeNodes(spaces);
|
||||
return TreeNode(
|
||||
uuid: community.uuid,
|
||||
@ -122,7 +129,8 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
|
||||
// Build tree nodes from your data model.
|
||||
List<TreeNode> _buildTreeNodes(List<SpaceModel> spaces) {
|
||||
return spaces.map((space) {
|
||||
List<TreeNode> childNodes = space.children.isNotEmpty ? _buildTreeNodes(space.children) : [];
|
||||
List<TreeNode> childNodes =
|
||||
space.children.isNotEmpty ? _buildTreeNodes(space.children) : [];
|
||||
return TreeNode(
|
||||
uuid: space.uuid!,
|
||||
title: space.name,
|
||||
@ -183,7 +191,8 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
|
||||
bool _searchAndHighlightNodes(List<TreeNode> 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<UsersEvent, UsersState> {
|
||||
List<TreeNode> _filterNodes(List<TreeNode> nodes, String searchTerm) {
|
||||
List<TreeNode> filteredNodes = [];
|
||||
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);
|
||||
if (isMatch || filteredChildren.isNotEmpty) {
|
||||
node.isHighlighted = isMatch;
|
||||
@ -307,8 +317,8 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
|
||||
_getPermissions(PermissionEvent event, Emitter<UsersState> 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<UsersEvent, UsersState> {
|
||||
bool _searchRolePermission(List<PermissionOption> 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<UsersEvent, UsersState> {
|
||||
// List<String> selectedIds =
|
||||
// getSelectedIds(updatedCommunities).where((id) => !communityIds.contains(id)).toList();
|
||||
|
||||
List<String> selectedSpacesId = [];
|
||||
var spaceBloc = NavigationService.navigatorKey.currentContext!.read<SpaceTreeBloc>();
|
||||
for (var community in spaceBloc.state.selectedCommunities) {
|
||||
selectedSpacesId.addAll(spaceBloc.state.selectedCommunityAndSpaces[community] ?? []);
|
||||
}
|
||||
List<String> selectedSpacesId = getSelectedSpacesIds();
|
||||
// List<String> selectedIds = getSelectedIds(updatedCommunities);
|
||||
|
||||
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 {
|
||||
try {
|
||||
emit(UsersLoadingState());
|
||||
List<String> 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<UsersEvent, UsersState> {
|
||||
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<UsersEvent, UsersState> {
|
||||
|
||||
String checkEmailValid = '';
|
||||
|
||||
Future<void> checkEmail(CheckEmailEvent event, Emitter<UsersState> emit) async {
|
||||
Future<void> checkEmail(
|
||||
CheckEmailEvent event, Emitter<UsersState> emit) async {
|
||||
emit(UsersLoadingState());
|
||||
String? res = await UserPermissionApi().checkEmail(
|
||||
emailController.text,
|
||||
@ -460,7 +477,8 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
|
||||
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<UsersEvent, UsersState> {
|
||||
emit(UsersLoadingState());
|
||||
|
||||
try {
|
||||
var spaceBloc =
|
||||
NavigationService.navigatorKey.currentContext!.read<SpaceTreeBloc>();
|
||||
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<UsersEvent, UsersState> {
|
||||
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<UsersEvent, UsersState> {
|
||||
} 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) {
|
||||
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<UsersEvent, UsersState> {
|
||||
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<TreeNode> nodes, TreeNode target) {
|
||||
|
Reference in New Issue
Block a user