forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditableUserTFAMethod.js
More file actions
191 lines (186 loc) · 5.41 KB
/
EditableUserTFAMethod.js
File metadata and controls
191 lines (186 loc) · 5.41 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
import React from 'react'
import { bool, string } from 'prop-types'
import {
Badge,
Box,
Button,
Flex,
Heading,
Select,
Stack,
useToast,
} from '@chakra-ui/react'
import { t, Trans } from '@lingui/macro'
import { Field, Formik } from 'formik'
import { useMutation } from '@apollo/client'
import { UPDATE_USER_PROFILE } from '../graphql/mutations'
import { useUserVar } from '../utilities/userState'
import {
EmailIcon,
PhoneIcon,
RadioCheckedIcon,
RadioUncheckedIcon,
} from '../theme/Icons'
export function EditableUserTFAMethod({
currentTFAMethod,
isUserAdmin,
emailValidated,
phoneValidated,
...props
}) {
const toast = useToast()
const { login, currentUser } = useUserVar()
const [updateUserProfile, { error: _updateUserProfileError }] = useMutation(
UPDATE_USER_PROFILE,
{
onError: ({ message }) => {
toast({
title: t`An error occurred while updating your TFA send method.`,
description: message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ updateUserProfile }) {
if (updateUserProfile.result.__typename === 'UpdateUserProfileResult') {
toast({
title: t`Changed TFA Send Method`,
description: t`You have successfully updated your TFA send method.`,
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
login({
...currentUser,
tfaSendMethod: updateUserProfile.result.user.tfaSendMethod,
})
} else if (
updateUserProfile.result.__typename === 'UpdateUserProfileError'
) {
toast({
title: t`Unable to update to your TFA send method, please try again.`,
description: updateUserProfile.result.description,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else {
toast({
title: t`Incorrect send method received.`,
description: t`Incorrect updateUserProfile.result typename.`,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
console.log('Incorrect updateUserProfile.result typename.')
}
},
},
)
return (
<Box {...props}>
<Heading as="h3" size="md" mb="3">
<Trans>Two-Factor Authentication:</Trans>
</Heading>
<Stack isInline mb="4">
<Box p="1">
{emailValidated ? (
<RadioCheckedIcon
boxSize="icons.lg"
mr="2"
aria-label="Email is validated"
/>
) : (
<RadioUncheckedIcon
boxSize="icons.lg"
mr="2"
aria-label="Email is not validated"
/>
)}
<Badge variant="outline" color="gray.900" p="1">
<EmailIcon mr="2" ml="1" boxSize="icons.lg" aria-hidden="true" />
<Trans>Email Validated</Trans>
</Badge>
</Box>
<Box p="1">
{phoneValidated ? (
<RadioCheckedIcon
boxSize="icons.lg"
mr="2"
aria-label="Phone is validated"
/>
) : (
<RadioUncheckedIcon
boxSize="icons.lg"
mr="2"
aria-label="Phone is not validated"
/>
)}
<Badge variant="outline" color="gray.900" p="1.5">
<PhoneIcon mr="2" ml="1" boxSize="1.1rem" aria-hidden="true" />
<Trans>Phone Validated</Trans>
</Badge>
</Box>
</Stack>
<Formik
validateOnBlur={false}
initialValues={{ tfaMethod: currentTFAMethod }}
onSubmit={async (values) => {
// Submit update detail mutation
await updateUserProfile({
variables: {
tfaSendMethod: values.tfaMethod,
},
})
}}
>
{({ handleSubmit, isSubmitting, getFieldProps }) => (
<form id="tfaForm" onSubmit={handleSubmit}>
<Flex
align="center"
borderWidth="1px"
borderColor="gray.500"
rounded="md"
p="1"
>
<Field
aria-label="TFA Method Select"
data-testid="tfa-method-select"
id="tfaMethod"
component={Select}
{...getFieldProps('tfaMethod')}
w="57%"
>
{(!isUserAdmin || currentTFAMethod === 'NONE') && (
<option value="NONE">{t`None`}</option>
)}
{emailValidated && <option value="EMAIL">{t`Email`}</option>}
{phoneValidated && <option value="PHONE">{t`Phone`}</option>}
</Field>
<Button
aria-label="Save TFA Method"
variant="primary"
type="submitBtn"
isLoading={isSubmitting}
ml="auto"
>
<Trans>Save</Trans>
</Button>
</Flex>
</form>
)}
</Formik>
</Box>
)
}
EditableUserTFAMethod.propTypes = {
currentTFAMethod: string,
isUserAdmin: bool,
emailValidated: bool,
phoneValidated: bool,
}