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
134 lines (129 loc) · 3.64 KB
/
EditableUserTFAMethod.js
File metadata and controls
134 lines (129 loc) · 3.64 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
import React from 'react'
import { bool, string } from 'prop-types'
import {
Heading,
Stack,
Select,
useToast,
Box,
Icon,
Badge,
} from '@chakra-ui/core'
import WithPseudoBox from './withPseudoBox'
import { t, Trans } from '@lingui/macro'
import { Formik, Field } from 'formik'
import { useMutation } from '@apollo/client'
import { UPDATE_USER_PROFILE } from './graphql/mutations'
import { useUserState } from './UserState'
import { TrackerButton } from './TrackerButton'
function EditableUserTFAMethod({
currentTFAMethod,
emailValidated,
phoneValidated,
}) {
const { currentUser } = useUserState()
const toast = useToast()
const [updateUserProfile, { error: _updateUserProfileError }] = useMutation(
UPDATE_USER_PROFILE,
{
context: {
headers: {
authorization: currentUser.jwt,
},
},
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() {
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',
})
},
},
)
return (
<Stack spacing="4">
<Heading as="h3" size="md">
<Trans>TFA Method:</Trans>
</Heading>
<Stack isInline>
<Box p="1">
<Icon
size="icons.lg"
name={emailValidated ? 'check' : 'close'}
color={emailValidated ? 'green.500' : 'red.500'}
pr={2}
/>
<Badge variant="outline" color="gray.900">
<Trans>Email Validated</Trans>
</Badge>
</Box>
<Box p="1">
<Icon
size="icons.lg"
name={phoneValidated ? 'check' : 'close'}
color={phoneValidated ? 'green.500' : 'red.500'}
pr={2}
/>
<Badge variant="outline" color="gray.900">
<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}>
<Stack isInline align="center" justifyContent="space-between">
<Field
id="tfaMethod"
component={Select}
{...getFieldProps('tfaMethod')}
w={['40%', '57%']}
>
<option value="NONE">{t`None`}</option>
{emailValidated && <option value="EMAIL">{t`Email`}</option>}
{phoneValidated && <option value="PHONE">{t`Phone`}</option>}
</Field>
<TrackerButton
type="submitBtn"
isLoading={isSubmitting}
variant="primary"
>
<Trans>Save TFA Method</Trans>
</TrackerButton>
</Stack>
</form>
)}
</Formik>
</Stack>
)
}
EditableUserTFAMethod.propTypes = {
currentTFAMethod: string,
emailValidated: bool,
phoneValidated: bool,
}
export default WithPseudoBox(EditableUserTFAMethod)