forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserList.js
More file actions
364 lines (344 loc) · 10.5 KB
/
UserList.js
File metadata and controls
364 lines (344 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import React, { useState } from 'react'
import { useLingui } from '@lingui/react'
import {
FormLabel,
Stack,
Icon,
InputGroup,
InputLeftElement,
Input,
Text,
useToast,
Select,
Box,
} from '@chakra-ui/core'
import { Trans, t } from '@lingui/macro'
import { PaginationButtons } from './PaginationButtons'
import { UserCard } from './UserCard'
import { string, shape, boolean } from 'prop-types'
import { useMutation } from '@apollo/client'
import { INVITE_USER_TO_ORG, UPDATE_USER_ROLES } from './graphql/mutations'
import { TrackerButton } from './TrackerButton'
import { useUserState } from './UserState'
import { Field, Formik } from 'formik'
import { fieldRequirements } from './fieldRequirements'
import { object, string as yupString } from 'yup'
export default function UserList({
permission,
userListData,
orgName,
orgSlug,
}) {
let users = []
if (userListData && userListData.edges) {
users = userListData.edges
}
const [userList, setUserList] = useState(users)
const [currentPage, setCurrentPage] = useState(1)
const [usersPerPage] = useState(4)
const toast = useToast()
const { i18n } = useLingui()
const { currentUser } = useUserState()
const [addedUserName, setAddedUserName] = useState()
const addUserValidationSchema = object().shape({
userName: yupString()
.required(i18n._(fieldRequirements.email.required.message))
.email(i18n._(fieldRequirements.email.email.message)),
})
// Get current users
const indexOfLastUser = currentPage * usersPerPage
const indexOfFirstUser = indexOfLastUser - usersPerPage
const currentUsers = userList.slice(indexOfFirstUser, indexOfLastUser)
const [updateUserRoles, { loading, error }] = useMutation(UPDATE_USER_ROLES, {
onError(error) {
toast({
title: error.message,
description: i18n._(t`Unable to change user role, please try again.`),
status: 'error',
duration: 9000,
isClosable: true,
position: 'bottom-left',
})
},
onCompleted() {
toast({
title: i18n._(t`Role updated`),
description: i18n._(t`The user's role has been successfully updated`),
status: 'success',
duration: 9000,
isClosable: true,
position: 'bottom-left',
})
},
})
const [addUser, { loading: addUserLoading }] = useMutation(
INVITE_USER_TO_ORG,
{
context: {
headers: {
authorization: currentUser.jwt,
},
},
onError(error) {
toast({
title: i18n._(t`An error occurred.`),
description: error.message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'bottom-left',
})
},
onCompleted() {
toast({
title: i18n._(t`User invited`),
description: i18n._(t`Email invitation sent to ${addedUserName}`),
status: 'info',
duration: 9000,
isClosable: true,
position: 'bottom-left',
})
},
},
)
if (loading)
return (
<p>
<Trans>Loading...</Trans>
</p>
)
if (error) return <p>{String(error)}</p>
// Change page
const paginate = (pageNumber) => setCurrentPage(pageNumber)
const removeUser = (user) => {
const temp = userList.filter((c) => c.node.id !== user.id)
if (temp) {
setUserList(temp)
if (currentUsers.length <= 1 && userList.length > 1)
setCurrentPage(Math.ceil(userList.length / usersPerPage) - 1)
toast({
title: 'User removed',
description: `${user.displayName} was removed from ${orgName}`,
status: 'info',
duration: 9000,
isClosable: true,
position: 'bottom-left',
})
} else {
toast({
title: 'An error occurred.',
description: `${user.displayName} could not be removed from ${orgName}`,
status: 'error',
duration: 9000,
isClosable: true,
position: 'bottom-left',
})
}
}
const handleClick = (role, userName) => {
updateUserRoles({
variables: {
input: {
orgSlug: orgSlug,
role: role,
userName: userName,
},
},
})
}
const showErrorToast = (error) =>
toast({
title: i18n._(t`An error occurred.`),
description: error,
status: 'error',
duration: 9000,
isClosable: true,
position: 'bottom-left',
})
return (
<Stack mb="6" w="100%">
<Text fontSize="2xl" fontWeight="bold">
<Trans>User List</Trans>
</Text>
<Formik
validationSchema={addUserValidationSchema}
initialValues={{ userName: '', roleSelect: 'USER_READ' }}
initialErrors={{ userName: 'Email cannot be empty' }}
onSubmit={(values) => {
addUser({
variables: {
userName: values.userName,
requestedRole: values.roleSelect,
orgSlug: orgSlug,
preferredLanguage: 'ENGLISH',
},
})
}}
>
{({ handleSubmit, values, errors }) => (
<form id="form" onSubmit={handleSubmit} noValidate>
<Stack
mb="8px"
alignItems="center"
w={permission ? '100%' : ['100%', '50%']}
isInline
>
<InputGroup flexGrow={1}>
<InputLeftElement>
<Icon name="search" color="gray.300" />
</InputLeftElement>
<Input
as={Field}
type="email"
name="userName"
placeholder={i18n._(t`Search for a user`)}
isDisabled={addUserLoading}
/>
</InputGroup>
<Field
as={Select}
flexBasis="7rem"
flexShrink={0}
id="roleSelect"
name="roleSelect"
>
<option value="USER_READ">{i18n._(t`READ`)}</option>
<option value="USER_WRITE">{i18n._(t`WRITE`)}</option>
<option value="ADMIN">{i18n._(t`ADMIN`)}</option>
</Field>
</Stack>
<TrackerButton
w={permission ? '100%' : ['100%', '50%']}
variant="primary"
type="submit"
onClick={() => {
setAddedUserName(values.userName)
if (errors.userName) showErrorToast(errors.userName)
}}
>
<Icon name="add" />
<Trans>Invite User</Trans>
</TrackerButton>
</form>
)}
</Formik>
{userList.length === 0 ? (
<Text fontSize="2xl" fontWeight="bold" textAlign="center">
<Trans>No users in this organization</Trans>
</Text>
) : (
currentUsers.map(({ node }, index) => {
let userRole = node.role
if (permission) {
return (
<Box key={`${node.username}:${index}`}>
{userRole === 'SUPER_ADMIN' ||
(permission === 'ADMIN' && userRole === 'ADMIN') ? (
<Stack key={node.id} isInline align="center">
<UserCard
userName={node.userName}
displayName={node.displayName}
role={userRole}
tfa={null}
/>
</Stack>
) : (
<Box key={`${node.username}:${index}`}>
<Stack isInline align="center">
<TrackerButton
variant="danger"
onClick={() => removeUser(node)}
px="3"
>
<Icon name="minus" />
</TrackerButton>
<UserCard
userName={node.userName}
displayName={node.displayName}
/>
</Stack>
<Stack isInline justifyContent="flex-end" align="center">
<FormLabel htmlFor="role_select" fontWeight="bold">
<Trans>Role:</Trans>
</FormLabel>
<Select
w="35%"
id="role_select"
size="sm"
name="role"
defaultValue={userRole}
onChange={(e) => (userRole = e.target.value)}
>
<option value="USER_READ">{i18n._(t`READ`)}</option>
<option value="USER_WRITE">{i18n._(t`WRITE`)}</option>
<option value="ADMIN">{i18n._(t`ADMIN`)}</option>
</Select>
<TrackerButton
onClick={() => handleClick(userRole, node.userName)}
variant="primary"
fontSize="sm"
px="3"
>
<Trans>Apply</Trans>
</TrackerButton>
</Stack>
</Box>
)}
</Box>
)
}
return (
<UserCard
key={node.id}
userName={node.userName}
tfa={node.tfa}
role={node.role}
displayName={node.displayName}
/>
)
})
)}
{userList.length > 0 && (
<PaginationButtons
perPage={usersPerPage}
total={userList.length}
paginate={paginate}
currentPage={currentPage}
/>
)}
</Stack>
)
}
/* -- Source code for adding organizations, not being used. --
<Box flexShrink="0" ml={{ md: 4 }} mr={{ md: 4 }} minW="25%">
<Box mt={2} color="gray.500">
Orgs:
{// Populate the user-orgs list.
edge.node.user.affiliations.edges.map((edge, i, arr) => {
if (arr.length - 1 === i) {
return (
<Text display="inline" key={edge.node.id + i}>
{edge.node.organization.acronym}
</Text>
)
}
return (
<Text display="inline" key={edge.node.id + i}>
{edge.node.organization.acronym + ' | '}
</Text>
)
})}
</Box>
</Box> */
UserList.propTypes = {
userListData: shape({
id: string,
userName: string,
role: string,
tfa: boolean,
displayName: string,
}),
orgName: string,
orgSlug: string,
permission: string,
}