forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditableUserPassword.js
More file actions
192 lines (185 loc) · 5.62 KB
/
EditableUserPassword.js
File metadata and controls
192 lines (185 loc) · 5.62 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
import React, { useRef } from 'react'
import {
Box,
Button,
Flex,
Heading,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
Stack,
Text,
useDisclosure,
useToast,
} from '@chakra-ui/react'
import { EditIcon } from '@chakra-ui/icons'
import { Formik } from 'formik'
import { t, Trans } from '@lingui/macro'
import { useMutation } from '@apollo/client'
import { PasswordField } from '../components/fields/PasswordField'
import { PasswordConfirmation } from '../components/fields/PasswordConfirmation'
import { createValidationSchema } from '../utilities/fieldRequirements'
import { UPDATE_USER_PASSWORD } from '../graphql/mutations'
import { LockIcon } from '../theme/Icons'
export function EditableUserPassword({ ...props }) {
const { isOpen, onOpen, onClose } = useDisclosure()
const toast = useToast()
const initialFocusRef = useRef()
const [updateUserPassword, { error: _updateUserPasswordError }] = useMutation(
UPDATE_USER_PASSWORD,
{
onError: ({ message }) => {
toast({
title: t`An error occurred while updating your password.`,
description: message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ updateUserPassword }) {
if (
updateUserPassword.result.__typename ===
'UpdateUserPasswordResultType'
) {
toast({
title: t`Changed User Password`,
description: t`You have successfully updated your password.`,
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
onClose()
} else if (
updateUserPassword.result.__typename === 'UpdateUserPasswordError'
) {
toast({
title: t`Unable to update your password, please try again.`,
description: updateUserPassword.result.description,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else {
toast({
title: t`Incorrect send method received.`,
description: t`Incorrect updateUserPassword.result typename.`,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
console.log('Incorrect updateUserPassword.result typename.')
}
},
},
)
return (
<Box {...props}>
<Heading as="h3" size="md" mb="1">
<Trans>Password:</Trans>
</Heading>
<Flex
align="center"
borderWidth="1px"
borderColor="gray.500"
rounded="md"
p="1"
>
<LockIcon mr="2" ml="1" boxSize="1.3rem" aria-hidden="true" />
<Text fontSize="xs">∗∗∗∗∗∗∗∗∗∗∗</Text>
<Button
aria-label="Edit User Password"
variant="primary"
ml="auto"
onClick={onOpen}
fontSize="sm"
px="3"
>
<EditIcon color="white" mr="2" boxSize="1rem" />
<Trans>Edit</Trans>
</Button>
</Flex>
<Modal
isOpen={isOpen}
onClose={onClose}
initialFocusRef={initialFocusRef}
motionPreset="slideInBottom"
>
<ModalOverlay />
<ModalContent pb={4}>
<Formik
validateOnBlur={false}
initialValues={{
password: '',
confirmPassword: '',
currentPassword: '',
}}
initialTouched={{
currentPassword: true,
}}
validationSchema={createValidationSchema([
'password',
'confirmPassword',
'currentPassword',
])}
onSubmit={async (values) => {
// Submit update detail mutation
await updateUserPassword({
variables: {
updatedPassword: values.password,
updatedPasswordConfirm: values.confirmPassword,
currentPassword: values.currentPassword,
},
})
}}
>
{({ handleSubmit, isSubmitting }) => (
<form id="form" onSubmit={handleSubmit}>
<ModalHeader>
<Trans>Change Password</Trans>
</ModalHeader>
<ModalCloseButton />
<ModalBody>
<Stack spacing={4} p="6" align="center">
<PasswordField
name="currentPassword"
label={t`Current Password:`}
width="100%"
ref={initialFocusRef}
/>
<Text textAlign="center">
<Trans>Enter and confirm your new password below:</Trans>
</Text>
<PasswordConfirmation
width="100%"
passwordLabel={t`New Password:`}
confirmPasswordLabel={t`Confirm New Password:`}
/>
</Stack>
</ModalBody>
<ModalFooter>
<Button
variant="primary"
isLoading={isSubmitting}
type="submit"
mr="4"
>
<Trans>Confirm</Trans>
</Button>
</ModalFooter>
</form>
)}
</Formik>
</ModalContent>
</Modal>
</Box>
)
}