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
417 lines (404 loc) · 14.5 KB
/
Copy pathAdminDomainModal.js
File metadata and controls
417 lines (404 loc) · 14.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
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
406
407
408
409
410
411
412
413
414
415
416
417
import React, { useRef } from 'react'
import { t, Trans } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import {
Badge,
Box,
Button,
Divider,
Flex,
FormControl,
FormErrorMessage,
Grid,
IconButton,
Input,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
SimpleGrid,
Stack,
Switch,
Tag,
TagCloseButton,
TagLabel,
TagRightIcon,
Text,
Tooltip,
useToast,
} from '@chakra-ui/react'
import { AddIcon, MinusIcon, QuestionOutlineIcon, SmallAddIcon } from '@chakra-ui/icons'
import { array, bool, func, number, object, string } from 'prop-types'
import { Field, FieldArray, Formik } from 'formik'
import { useMutation } from '@apollo/client'
import { DomainField } from '../components/fields/DomainField'
import { CREATE_DOMAIN, UPDATE_DOMAIN } from '../graphql/mutations'
import { ABTestVariant, ABTestingWrapper } from '../app/ABTestWrapper'
export function AdminDomainModal({ isOpen, onClose, validationSchema, orgId, ...props }) {
const {
editingDomainId,
editingDomainUrl,
selectorInputList,
tagInputList,
orgSlug,
archived,
hidden,
permission,
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') {
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',
})
onClose()
} 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: ['PaginatedOrgDomains', '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') {
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',
})
onClose()
} 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.')
}
},
})
const tagOptions = [
{ en: 'NEW', fr: 'NOUVEAU' },
{ en: 'PROD', fr: 'PROD' },
{ en: 'STAGING', fr: 'DEV' },
{ en: 'TEST', fr: 'TEST' },
{ en: 'WEB', fr: 'WEB' },
{ en: 'INACTIVE', fr: 'INACTIF' },
]
const addableTags = (values, helper) => {
const stringValues = values?.map((label) => {
return label[i18n.locale]
})
const difference = tagOptions.filter((label) => !stringValues?.includes(label[i18n.locale]))
return difference?.map((label, idx) => {
return (
<Tag
key={idx}
id={`add-tag-${label[i18n.locale]}`}
as="button"
_hover={{ bg: 'gray.200' }}
borderRadius="full"
onClick={(e) => {
e.preventDefault()
helper.push(label)
}}
>
<TagLabel>{label[i18n.locale]}</TagLabel>
<TagRightIcon as={AddIcon} color="gray.500" ml="auto" />
</Tag>
)
})
}
return (
<Modal isOpen={isOpen} onClose={onClose} initialFocusRef={initialFocusRef} motionPreset="slideInBottom">
<ModalOverlay />
<ModalContent pb={4}>
<Formik
initialValues={{
domainUrl: editingDomainUrl,
selectors: selectorInputList,
// convert initial tags to input type
tags: tagInputList?.map((label) => {
return tagOptions.filter((option) => {
return option[i18n.locale] == label
})[0]
}),
archiveDomain: archived,
hideDomain: hidden,
}}
initialTouched={{
domainUrl: true,
}}
validationSchema={validationSchema}
onSubmit={async (values) => {
// Submit update detail mutation
if (mutation === 'update') {
await updateDomain({
variables: {
domainId: editingDomainId,
orgId: orgId,
domain: values.domainUrl,
selectors: values.selectors,
tags: values.tags,
archived: values.archiveDomain,
hidden: values.hideDomain,
},
})
} else if (mutation === 'create') {
await createDomain({
variables: {
orgId: orgId,
domain: values.domainUrl,
selectors: values.selectors,
tags: values.tags,
archived: values.archiveDomain,
hidden: values.hideDomain,
},
})
}
}}
>
{({ handleSubmit, handleChange, isSubmitting, values, errors, touched }) => (
<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}>
<DomainField name="domainUrl" label={t`New Domain URL:`} placeholder={t`New Domain URL`} />
<FieldArray
name="selectors"
render={(arrayHelpers) => (
<Box>
<Text fontWeight="bold">
<Trans>DKIM Selectors:</Trans>
</Text>
{values.selectors.map((_selector, index) => (
<FormControl
key={index}
isInvalid={
errors.selectors &&
errors.selectors[index] &&
touched.selectors &&
touched.selectors[index]
}
>
<Grid gridTemplateColumns="auto 1fr" gap="0.5em" alignItems="center" mb="0.5em">
<IconButton
variant="danger"
icon={<MinusIcon size="icons.xs" />}
data-testid="remove-dkim-selector"
type="button"
p="3"
onClick={() => arrayHelpers.remove(index)}
aria-label="remove-dkim-selector"
/>
<Field id={`selectors.${index}`} name={`selectors.${index}`} h="1.5rem">
{({ field }) => (
<Input
{...field}
id={`selectors.${index}`}
name={`selectors.${index}`}
placeholder={i18n._(t`DKIM Selector`)}
ref={initialFocusRef}
/>
)}
</Field>
<FormErrorMessage gridColumn="2 / 3" mt={0}>
{errors && errors.selectors && errors.selectors[index]}
</FormErrorMessage>
</Grid>
</FormControl>
))}
<IconButton
variant="primary"
icon={<SmallAddIcon size="icons.md" />}
data-testid="add-dkim-selector"
type="button"
px="2"
onClick={() => arrayHelpers.push('')}
aria-label="add-dkim-selector"
/>
</Box>
)}
/>
<FieldArray
name="tags"
render={(arrayHelpers) => (
<Box>
<Text fontWeight="bold">Tags:</Text>
<SimpleGrid columns={3} spacing={2}>
{values.tags?.map((label, idx) => {
return (
<Tag key={idx} borderRadius="full">
<TagLabel>{label[i18n.locale]}</TagLabel>
<TagCloseButton
ml="auto"
onClick={() => arrayHelpers.remove(idx)}
aria-label={`remove-tag-${label[i18n.locale]}`}
/>
</Tag>
)
})}
</SimpleGrid>
<Divider borderBottomColor="gray.900" />
<SimpleGrid columns={3} spacing={2}>
{addableTags(values.tags, arrayHelpers)}
</SimpleGrid>
</Box>
)}
/>
<ABTestingWrapper insiderVariantName="B">
<ABTestVariant name="B">
<Flex align="center">
<Tooltip label={t`Prevent this domain from being counted in your organization's summaries.`}>
<QuestionOutlineIcon tabIndex={0} />
</Tooltip>
<label>
<Switch
isFocusable={true}
name="hideDomain"
mx="2"
defaultChecked={values.hideDomain}
onChange={handleChange}
/>
</label>
<Badge variant="outline" color="gray.900" p="1.5">
<Trans>Hide domain</Trans>
</Badge>
</Flex>
{permission === 'SUPER_ADMIN' && (
<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={values.archiveDomain}
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>
)}
<Text>
<Trans>Please allow up to 24 hours for summaries to reflect any changes.</Trans>
</Text>
</ABTestVariant>
</ABTestingWrapper>
</Stack>
</ModalBody>
<ModalFooter>
<Button variant="primary" isLoading={isSubmitting} type="submit" mr="4">
<Trans>Confirm</Trans>
</Button>
</ModalFooter>
</form>
)}
</Formik>
</ModalContent>
</Modal>
)
}
AdminDomainModal.propTypes = {
isOpen: bool,
onClose: func,
validationSchema: object,
orgId: string,
editingDomainId: string,
editingDomainUrl: string,
selectorInputList: array,
tagInputList: array,
archived: bool,
hidden: bool,
permission: string,
orgSlug: string,
mutation: string,
orgCount: number,
}