1
1
import { useActor , useMachine } from "@xstate/react"
2
+ import { User } from "api/typesGenerated"
2
3
import { DeleteDialog } from "components/Dialogs/DeleteDialog/DeleteDialog"
3
4
import { getPaginationContext } from "components/PaginationWidget/utils"
4
5
import { usePermissions } from "hooks/usePermissions"
@@ -22,6 +23,9 @@ export const Language = {
22
23
activateDialogMessagePrefix : "Do you want to activate the user" ,
23
24
}
24
25
26
+ const getSelectedUser = ( id : string , users ?: User [ ] ) =>
27
+ users ?. find ( ( u ) => u . id === id )
28
+
25
29
export const UsersPage : FC < { children ?: ReactNode } > = ( ) => {
26
30
const xServices = useContext ( XServiceContext )
27
31
const navigate = useNavigate ( )
@@ -40,18 +44,14 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
40
44
const {
41
45
users,
42
46
getUsersError,
43
- userIdToDelete ,
44
- userIdToSuspend ,
45
- userIdToActivate ,
47
+ usernameToDelete ,
48
+ usernameToSuspend ,
49
+ usernameToActivate ,
46
50
userIdToResetPassword,
47
51
newUserPassword,
48
52
paginationRef,
49
53
} = usersState . context
50
54
51
- const userToBeSuspended = users ?. find ( ( u ) => u . id === userIdToSuspend )
52
- const userToBeDeleted = users ?. find ( ( u ) => u . id === userIdToDelete )
53
- const userToBeActivated = users ?. find ( ( u ) => u . id === userIdToActivate )
54
- const userToResetPassword = users ?. find ( ( u ) => u . id === userIdToResetPassword )
55
55
const { updateUsers : canEditUsers } = usePermissions ( )
56
56
const [ rolesState , rolesSend ] = useActor ( xServices . siteRolesXService )
57
57
const { roles } = rolesState . context
@@ -88,13 +88,25 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
88
88
)
89
89
} }
90
90
onDeleteUser = { ( user ) => {
91
- usersSend ( { type : "DELETE_USER" , userId : user . id } )
91
+ usersSend ( {
92
+ type : "DELETE_USER" ,
93
+ userId : user . id ,
94
+ username : user . username ,
95
+ } )
92
96
} }
93
97
onSuspendUser = { ( user ) => {
94
- usersSend ( { type : "SUSPEND_USER" , userId : user . id } )
98
+ usersSend ( {
99
+ type : "SUSPEND_USER" ,
100
+ userId : user . id ,
101
+ username : user . username ,
102
+ } )
95
103
} }
96
104
onActivateUser = { ( user ) => {
97
- usersSend ( { type : "ACTIVATE_USER" , userId : user . id } )
105
+ usersSend ( {
106
+ type : "ACTIVATE_USER" ,
107
+ userId : user . id ,
108
+ username : user . username ,
109
+ } )
98
110
} }
99
111
onResetUserPassword = { ( user ) => {
100
112
usersSend ( { type : "RESET_USER_PASSWORD" , userId : user . id } )
@@ -117,25 +129,29 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
117
129
paginationRef = { paginationRef }
118
130
/>
119
131
120
- { userToBeDeleted && (
121
- < DeleteDialog
122
- isOpen = { usersState . matches ( "confirmUserDeletion" ) }
123
- confirmLoading = { usersState . matches ( "deletingUser" ) }
124
- name = { userToBeDeleted . username }
125
- entity = "user"
126
- onConfirm = { ( ) => {
127
- usersSend ( "CONFIRM_USER_DELETE" )
128
- } }
129
- onCancel = { ( ) => {
130
- usersSend ( "CANCEL_USER_DELETE" )
131
- } }
132
- />
133
- ) }
132
+ < DeleteDialog
133
+ isOpen = {
134
+ usersState . matches ( "confirmUserDeletion" ) ||
135
+ usersState . matches ( "deletingUser" )
136
+ }
137
+ confirmLoading = { usersState . matches ( "deletingUser" ) }
138
+ name = { usernameToDelete ?? "" }
139
+ entity = "user"
140
+ onConfirm = { ( ) => {
141
+ usersSend ( "CONFIRM_USER_DELETE" )
142
+ } }
143
+ onCancel = { ( ) => {
144
+ usersSend ( "CANCEL_USER_DELETE" )
145
+ } }
146
+ />
134
147
135
148
< ConfirmDialog
136
149
type = "delete"
137
150
hideCancel = { false }
138
- open = { usersState . matches ( "confirmUserSuspension" ) }
151
+ open = {
152
+ usersState . matches ( "confirmUserSuspension" ) ||
153
+ usersState . matches ( "suspendingUser" )
154
+ }
139
155
confirmLoading = { usersState . matches ( "suspendingUser" ) }
140
156
title = { Language . suspendDialogTitle }
141
157
confirmText = { Language . suspendDialogAction }
@@ -147,16 +163,20 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
147
163
} }
148
164
description = {
149
165
< >
150
- { Language . suspendDialogMessagePrefix } { " " }
151
- < strong > { userToBeSuspended ?. username } </ strong > ?
166
+ { Language . suspendDialogMessagePrefix }
167
+ { usernameToSuspend && " " }
168
+ < strong > { usernameToSuspend ?? "" } </ strong > ?
152
169
</ >
153
170
}
154
171
/>
155
172
156
173
< ConfirmDialog
157
174
type = "success"
158
175
hideCancel = { false }
159
- open = { usersState . matches ( "confirmUserActivation" ) }
176
+ open = {
177
+ usersState . matches ( "confirmUserActivation" ) ||
178
+ usersState . matches ( "activatingUser" )
179
+ }
160
180
confirmLoading = { usersState . matches ( "activatingUser" ) }
161
181
title = { Language . activateDialogTitle }
162
182
confirmText = { Language . activateDialogAction }
@@ -168,24 +188,30 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
168
188
} }
169
189
description = {
170
190
< >
171
- { Language . activateDialogMessagePrefix } { " " }
172
- < strong > { userToBeActivated ?. username } </ strong > ?
191
+ { Language . activateDialogMessagePrefix }
192
+ { usernameToActivate && " " }
193
+ < strong > { usernameToActivate ?? "" } </ strong > ?
173
194
</ >
174
195
}
175
196
/>
176
197
177
- < ResetPasswordDialog
178
- loading = { usersState . matches ( "resettingUserPassword" ) }
179
- user = { userToResetPassword }
180
- newPassword = { newUserPassword }
181
- open = { usersState . matches ( "confirmUserPasswordReset" ) }
182
- onClose = { ( ) => {
183
- usersSend ( "CANCEL_USER_PASSWORD_RESET" )
184
- } }
185
- onConfirm = { ( ) => {
186
- usersSend ( "CONFIRM_USER_PASSWORD_RESET" )
187
- } }
188
- />
198
+ { userIdToResetPassword && (
199
+ < ResetPasswordDialog
200
+ open = {
201
+ usersState . matches ( "confirmUserPasswordReset" ) ||
202
+ usersState . matches ( "resettingUserPassword" )
203
+ }
204
+ loading = { usersState . matches ( "resettingUserPassword" ) }
205
+ user = { getSelectedUser ( userIdToResetPassword , users ) }
206
+ newPassword = { newUserPassword }
207
+ onClose = { ( ) => {
208
+ usersSend ( "CANCEL_USER_PASSWORD_RESET" )
209
+ } }
210
+ onConfirm = { ( ) => {
211
+ usersSend ( "CONFIRM_USER_PASSWORD_RESET" )
212
+ } }
213
+ />
214
+ ) }
189
215
</ >
190
216
)
191
217
}
0 commit comments