forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminDomainModal.js
More file actions
405 lines (392 loc) · 13.1 KB
/
AdminDomainModal.js
File metadata and controls
405 lines (392 loc) · 13.1 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import React, { useRef } from 'react'
import { t, Trans } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import {
Badge,
Box,
Button,
Divider,
Flex,
FormControl,
FormLabel,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
Select,
SimpleGrid,
Stack,
Switch,
Tag,
TagCloseButton,
TagLabel,
Text,
Tooltip,
useToast,
} from '@chakra-ui/react'
import { AddIcon, QuestionOutlineIcon } from '@chakra-ui/icons'
import { array, bool, func, number, object, string } from 'prop-types'
import { FieldArray, Formik } from 'formik'
import { useMutation } from '@apollo/client'
import { DomainField } from '../components/fields/DomainField'
import { CREATE_DOMAIN, UPDATE_DOMAIN } from '../graphql/mutations'
import withSuperAdmin from '../app/withSuperAdmin'
export function AdminDomainModal({ isOpen, onClose, validationSchema, orgId, availableTags, ...props }) {
const { editingDomainId, editingDomainUrl, tagInputList, orgSlug, archived, assetState, mutation, orgCount } = props
const toast = useToast()
const initialFocusRef = useRef()
const { i18n } = useLingui()
const [createDomain] = useMutation(CREATE_DOMAIN, {
refetchQueries: ['PaginatedOrgDomains', 'FindAuditLogs'],
onError(error) {
toast({
title: i18n._(t`An error occurred.`),
description: error.message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ createDomain }) {
if (createDomain.result.__typename === 'Domain') {
onClose()
toast({
title: i18n._(t`Domain added`),
description: i18n._(t`${createDomain.result.domain} was added to ${orgSlug}`),
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else if (createDomain.result.__typename === 'DomainError') {
toast({
title: i18n._(t`Unable to create new domain.`),
description: createDomain.result.description,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else {
toast({
title: i18n._(t`Incorrect send method received.`),
description: i18n._(t`Incorrect createDomain.result typename.`),
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
console.log('Incorrect createDomain.result typename.')
}
},
})
const [updateDomain] = useMutation(UPDATE_DOMAIN, {
refetchQueries: ['FindAuditLogs'],
onError(error) {
toast({
title: i18n._(t`An error occurred.`),
description: error.message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ updateDomain }) {
if (updateDomain.result.__typename === 'Domain') {
onClose()
toast({
title: i18n._(t`Domain updated`),
description: i18n._(
t`${editingDomainUrl} from ${orgSlug} successfully updated to ${updateDomain.result.domain}`,
),
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else if (updateDomain.result.__typename === 'DomainError') {
toast({
title: i18n._(t`Unable to update domain.`),
description: updateDomain.result.description,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else {
toast({
title: i18n._(t`Incorrect send method received.`),
description: i18n._(t`Incorrect updateDomain.result typename.`),
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
console.log('Incorrect updateDomain.result typename.')
}
},
update: (cache, { data }) => {
if (data.updateDomain.result.__typename !== 'Domain') return
const updateDomainId = cache.identify(data.updateDomain.result)
cache.modify({
id: updateDomainId,
fields: {
claimTags() {
return data.updateDomain.result.claimTags
},
},
})
},
})
const addableTags = (values, helper) => {
const stringValues = values?.map(({ tagId }) => {
return tagId
})
const difference = availableTags.filter(({ tagId }) => !stringValues?.includes(tagId))
return difference?.map((tag, idx) => {
return (
<Button
key={idx}
id={`add-tag-${tag.tagId}`}
_hover={{ bg: 'gray.200' }}
borderRadius="full"
onClick={() => {
helper.push(tag)
}}
bg="#f2f2f2"
fontWeight="normal"
size="sm"
>
{tag.label.toUpperCase()}
<AddIcon color="gray.500" ml="auto" />
</Button>
)
})
}
const getInitTags = () => {
let tags = tagInputList?.map((label) => {
return availableTags.filter((option) => option.tagId == label.tagId)[0]
})
if (mutation === 'create' && tags.filter(({ tagId }) => tagId === 'new-nouveau').length === 0) {
const newTag = availableTags.filter(({ tagId }) => tagId === 'new-nouveau')[0]
newTag && tags.push(newTag)
}
return tags
}
return (
<Modal isOpen={isOpen} onClose={onClose} initialFocusRef={initialFocusRef} motionPreset="slideInBottom">
<ModalOverlay />
<ModalContent pb={4}>
<Formik
initialValues={{
domainUrl: editingDomainUrl,
// convert initial tags to input type
tags: getInitTags(),
archiveDomain: archived,
assetState: assetState || 'APPROVED',
}}
initialTouched={{
domainUrl: true,
}}
validationSchema={validationSchema}
onSubmit={async (values) => {
// Submit update detail mutation
if (mutation === 'update') {
await updateDomain({
variables: {
domainId: editingDomainId,
orgId: orgId,
tags: values.tags.map(({ tagId }) => tagId),
archived: values.archiveDomain,
assetState: values.assetState,
ignoreRua: values.ignoreRua,
},
})
} else if (mutation === 'create') {
await createDomain({
variables: {
orgId: orgId,
domain: values.domainUrl.trim(),
tags: values.tags.map(({ tagId }) => tagId),
archived: values.archiveDomain,
assetState: values.assetState,
},
})
}
}}
>
{({ handleSubmit, handleChange, isSubmitting, values }) => (
<form id="form" onSubmit={handleSubmit}>
<ModalHeader>
{mutation === 'update' ? <Trans>Edit Domain Details</Trans> : <Trans>Add Domain Details</Trans>}
</ModalHeader>
<ModalCloseButton />
<ModalBody>
<Stack spacing={4} p={25}>
{mutation === 'create' ? (
<DomainField name="domainUrl" label={t`New Domain URL:`} placeholder={t`New Domain URL`} />
) : (
<Box>
<Text fontWeight="bold">Domain:</Text>
<Text flexWrap="wrap">{editingDomainUrl}</Text>
</Box>
)}
<FieldArray
name="tags"
render={(arrayHelpers) => (
<Box>
<Text fontWeight="bold">Tags:</Text>
<SimpleGrid columns={3} spacing={2}>
{values.tags?.map(({ tagId, label, description }, idx) => {
return (
<Tag key={idx} borderRadius="full" py="2" px="3">
<Tooltip label={description} aria-label={`tag-tooltip-${tagId}`}>
<TagLabel>{label.toUpperCase()}</TagLabel>
</Tooltip>
<TagCloseButton
ml="auto"
onClick={() => arrayHelpers.remove(idx)}
aria-label={`remove-tag-${tagId}`}
/>
</Tag>
)
})}
</SimpleGrid>
<Divider borderBottomColor="gray.900" />
<SimpleGrid columns={3} spacing={2}>
{addableTags(values.tags, arrayHelpers)}
</SimpleGrid>
</Box>
)}
/>
<FormControl>
<FormLabel htmlFor="assetState" fontWeight="bold">
<Tooltip
label={t`Select a state that best describes the asset in relation to your organization.`}
>
<Flex align="center">
<Trans>Asset State</Trans> <QuestionOutlineIcon ml="2" color="gray.500" boxSize="icons.md" />
</Flex>
</Tooltip>
</FormLabel>
<Select
name="assetState"
id="assetState"
borderColor="black"
onChange={handleChange}
defaultValue={assetState}
>
<option value="APPROVED">
<Trans>Approved</Trans>
</option>
<option value="DEPENDENCY">
<Trans>Dependency</Trans>
</option>
<option value="MONITOR_ONLY">
<Trans>Monitor Only</Trans>
</option>
<option value="CANDIDATE">
<Trans>Candidate</Trans>
</option>
<option value="REQUIRES_INVESTIGATION">
<Trans>Requires Investigation</Trans>
</option>
</Select>
</FormControl>
<IgnoreRuaToggle defaultChecked={values.ignoreRua} handleChange={handleChange} />
<ArchiveDomainSwitch
defaultChecked={values.archiveDomain}
handleChange={handleChange}
orgCount={orgCount}
/>
<Text>
<Trans>Please allow up to 24 hours for summaries to reflect any changes.</Trans>
</Text>
</Stack>
</ModalBody>
<ModalFooter>
<Button variant="primary" isLoading={isSubmitting} type="submit" mr="4">
<Trans>Confirm</Trans>
</Button>
</ModalFooter>
</form>
)}
</Formik>
</ModalContent>
</Modal>
)
}
const ArchiveDomainSwitch = withSuperAdmin(({ defaultChecked, handleChange, orgCount }) => {
return (
<Box>
<Flex align="center">
<Tooltip label={t`Prevent this domain from being visible, scanned, and being counted in any summaries.`}>
<QuestionOutlineIcon tabIndex={0} />
</Tooltip>
<label>
<Switch
colorScheme="red"
isFocusable={true}
name="archiveDomain"
mx="2"
defaultChecked={defaultChecked}
onChange={handleChange}
/>
</label>
<Badge variant="outline" color="gray.900" p="1.5">
<Trans>Archive domain</Trans>
</Badge>
</Flex>
<Text fontSize="sm">
{orgCount > 0 ? (
<Trans>Note: This will affect results for {orgCount} organizations</Trans>
) : (
<Trans>Note: This could affect results for multiple organizations</Trans>
)}
</Text>
</Box>
)
})
const IgnoreRuaToggle = withSuperAdmin(({ defaultChecked, handleChange }) => {
return (
<Box>
<Flex align="center">
<label>
<Switch
colorScheme="blue"
isFocusable={true}
name="ignoreRua"
mx="2"
defaultChecked={defaultChecked}
onChange={handleChange}
/>
</label>
<Badge variant="outline" color="gray.900" p="1.5">
<Trans>Ignore RUA</Trans>
</Badge>
</Flex>
</Box>
)
})
AdminDomainModal.propTypes = {
isOpen: bool,
onClose: func,
validationSchema: object,
orgId: string,
editingDomainId: string,
editingDomainUrl: string,
tagInputList: array,
archived: bool,
orgSlug: string,
mutation: string,
orgCount: number,
refetchQueries: array,
myOrg: object,
assetState: string,
availableTags: array,
}