5
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
6
*/
7
7
8
- import ProfileModificationForm , {
8
+ import UserModificationForm , {
9
9
USER_NAME ,
10
10
USER_PROFILE_NAME ,
11
+ USER_SELECTED_GROUPS ,
11
12
UserModificationFormType ,
12
13
UserModificationSchema ,
13
14
} from './user-modification-form' ;
14
15
import { yupResolver } from '@hookform/resolvers/yup' ;
15
16
import { useForm } from 'react-hook-form' ;
16
17
import { FunctionComponent , useCallback , useEffect , useMemo , useState } from 'react' ;
17
18
import { CustomMuiDialog , FetchStatus , useSnackMessage } from '@gridsuite/commons-ui' ;
18
- import { UpdateUserInfos , UserAdminSrv , UserInfos , UserProfile } from '../../../services' ;
19
+ import { GroupInfos , UpdateUserInfos , UserAdminSrv , UserInfos , UserProfile } from '../../../services' ;
19
20
20
21
interface UserModificationDialogProps {
21
22
userInfos : UserInfos | undefined ;
@@ -34,36 +35,64 @@ const UserModificationDialog: FunctionComponent<UserModificationDialogProps> = (
34
35
const formMethods = useForm ( {
35
36
resolver : yupResolver ( UserModificationSchema ) ,
36
37
} ) ;
37
- const { reset } = formMethods ;
38
+ const { reset, setValue } = formMethods ;
38
39
const [ profileOptions , setProfileOptions ] = useState < string [ ] > ( [ ] ) ;
40
+ const [ groupOptions , setGroupOptions ] = useState < string [ ] > ( [ ] ) ;
41
+ const [ selectedGroups , setSelectedGroups ] = useState < string [ ] > ( [ ] ) ;
39
42
const [ dataFetchStatus , setDataFetchStatus ] = useState < string > ( FetchStatus . IDLE ) ;
40
43
41
44
useEffect ( ( ) => {
42
- // fetch available profiles
43
45
if ( userInfos && open ) {
46
+ setSelectedGroups ( userInfos . groups ) ;
47
+ // fetch profile & groups
44
48
setDataFetchStatus ( FetchStatus . FETCHING ) ;
45
- UserAdminSrv . fetchProfilesWithoutValidityCheck ( )
49
+ let fetcherPromises : Promise < unknown > [ ] = [ ] ;
50
+ const profilePromise = UserAdminSrv . fetchProfilesWithoutValidityCheck ( ) ;
51
+ const groupPromise = UserAdminSrv . fetchGroups ( ) ;
52
+ fetcherPromises . push ( profilePromise ) ;
53
+ fetcherPromises . push ( groupPromise ) ;
54
+
55
+ profilePromise
46
56
. then ( ( allProfiles : UserProfile [ ] ) => {
47
- setDataFetchStatus ( FetchStatus . FETCH_SUCCESS ) ;
48
57
setProfileOptions (
49
58
allProfiles . map ( ( p ) => p . name ) . sort ( ( a : string , b : string ) => a . localeCompare ( b ) )
50
59
) ;
51
60
} )
52
61
. catch ( ( error ) => {
53
- setDataFetchStatus ( FetchStatus . FETCH_ERROR ) ;
54
62
snackError ( {
55
63
messageTxt : error . message ,
56
64
headerId : 'users.table.error.profiles' ,
57
65
} ) ;
58
66
} ) ;
67
+
68
+ groupPromise
69
+ . then ( ( allGroups : GroupInfos [ ] ) => {
70
+ setGroupOptions ( allGroups . map ( ( g ) => g . name ) . sort ( ( a : string , b : string ) => a . localeCompare ( b ) ) ) ;
71
+ } )
72
+ . catch ( ( error ) => {
73
+ snackError ( {
74
+ messageTxt : error . message ,
75
+ headerId : 'users.table.error.groups' ,
76
+ } ) ;
77
+ } ) ;
78
+
79
+ Promise . all ( fetcherPromises )
80
+ . then ( ( ) => {
81
+ setDataFetchStatus ( FetchStatus . FETCH_SUCCESS ) ;
82
+ } )
83
+ . catch ( ( ) => {
84
+ setDataFetchStatus ( FetchStatus . FETCH_ERROR ) ;
85
+ } ) ;
59
86
}
60
87
} , [ open , snackError , userInfos ] ) ;
61
88
62
89
useEffect ( ( ) => {
63
90
if ( userInfos && open ) {
91
+ const sortedGroups = Array . from ( userInfos . groups ) . sort ( ( a , b ) => a . localeCompare ( b ) ) ;
64
92
reset ( {
65
93
[ USER_NAME ] : userInfos . sub ,
66
94
[ USER_PROFILE_NAME ] : userInfos . profileName ,
95
+ [ USER_SELECTED_GROUPS ] : JSON . stringify ( sortedGroups ) , // only used to dirty the form
67
96
} ) ;
68
97
}
69
98
} , [ userInfos , open , reset ] ) ;
@@ -73,14 +102,27 @@ const UserModificationDialog: FunctionComponent<UserModificationDialogProps> = (
73
102
onClose ( ) ;
74
103
} , [ onClose ] ) ;
75
104
105
+ const onSelectionChanged = useCallback (
106
+ ( selectedItems : string [ ] ) => {
107
+ if ( userInfos ) {
108
+ setSelectedGroups ( selectedItems ) ;
109
+ selectedItems . sort ( ( a , b ) => a . localeCompare ( b ) ) ;
110
+ setValue ( USER_SELECTED_GROUPS , JSON . stringify ( selectedItems ) , {
111
+ shouldDirty : true ,
112
+ } ) ;
113
+ }
114
+ } ,
115
+ [ setValue , userInfos ]
116
+ ) ;
117
+
76
118
const onSubmit = useCallback (
77
119
( userFormData : UserModificationFormType ) => {
78
120
if ( userInfos ) {
79
121
const newData : UpdateUserInfos = {
80
122
sub : userInfos . sub , // sub cannot be changed, it is a PK in database
81
123
isAdmin : userInfos . isAdmin , // cannot be changed for now
82
124
profileName : userFormData . profileName ?? undefined ,
83
- groups : [ ] ,
125
+ groups : selectedGroups ,
84
126
} ;
85
127
UserAdminSrv . udpateUser ( newData )
86
128
. catch ( ( error ) =>
@@ -94,7 +136,7 @@ const UserModificationDialog: FunctionComponent<UserModificationDialogProps> = (
94
136
} ) ;
95
137
}
96
138
} ,
97
- [ onUpdate , snackError , userInfos ]
139
+ [ onUpdate , selectedGroups , snackError , userInfos ]
98
140
) ;
99
141
100
142
const isDataReady = useMemo ( ( ) => dataFetchStatus === FetchStatus . FETCH_SUCCESS , [ dataFetchStatus ] ) ;
@@ -110,8 +152,16 @@ const UserModificationDialog: FunctionComponent<UserModificationDialogProps> = (
110
152
titleId = { 'users.form.modification.title' }
111
153
removeOptional = { true }
112
154
isDataFetching = { isDataFetching }
155
+ unscrollableFullHeight
113
156
>
114
- { isDataReady && < ProfileModificationForm profileOptions = { profileOptions } /> }
157
+ { isDataReady && (
158
+ < UserModificationForm
159
+ profileOptions = { profileOptions }
160
+ groupOptions = { groupOptions }
161
+ selectedGroups = { selectedGroups }
162
+ onSelectionChanged = { onSelectionChanged }
163
+ />
164
+ ) }
115
165
</ CustomMuiDialog >
116
166
) ;
117
167
} ;
0 commit comments