forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditableUserEmail.js
More file actions
150 lines (140 loc) · 4.63 KB
/
EditableUserEmail.js
File metadata and controls
150 lines (140 loc) · 4.63 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
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 { string } from 'prop-types'
import { createValidationSchema } from '../utilities/fieldRequirements'
import { EmailField } from '../components/fields/EmailField'
import { UPDATE_USER_PROFILE } from '../graphql/mutations'
import { EmailIcon } from '../theme/Icons'
export function EditableUserEmail({ detailValue, ...props }) {
const { isOpen, onOpen, onClose } = useDisclosure()
const toast = useToast()
const initialFocusRef = useRef()
const [updateUserProfile, { error: _updateUserProfileError }] = useMutation(UPDATE_USER_PROFILE, {
onError: ({ message }) => {
toast({
title: t`An error occurred while updating your email address.`,
description: message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ updateUserProfile }) {
if (updateUserProfile.result.__typename === 'UpdateUserProfileResult') {
toast({
title: t`Email Verification Sent`,
description: t`A verification email has been sent to your new email address. Please verify your email address to complete the change.`,
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
onClose()
} else if (updateUserProfile.result.__typename === 'UpdateUserProfileError') {
toast({
title: t`Unable to update your email address, 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="1">
<Trans>Email:</Trans>
</Heading>
<Flex align="center" borderWidth="1px" borderColor="gray.500" rounded="md" p="1">
<EmailIcon mr="2" ml="1" boxSize="icons.lg" aria-hidden="true" />
<Text>{detailValue}</Text>
<Button aria-label="Edit User Email" 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={{
email: '',
}}
initialTouched={{
email: true,
}}
validationSchema={createValidationSchema(['email'])}
onSubmit={async (values) => {
// Submit update detail mutation
await updateUserProfile({
variables: {
userName: values.email,
},
})
}}
>
{({ handleSubmit, isSubmitting }) => (
<form id="form" onSubmit={handleSubmit}>
<ModalHeader>
<Trans>Edit Email</Trans>
</ModalHeader>
<ModalCloseButton />
<ModalBody>
<Stack spacing="4" p="6">
<Heading as="h3" size="sm">
<Trans>Current Email:</Trans>
</Heading>
<Text>{detailValue}</Text>
<EmailField name="email" label={t`New Email Address:`} ref={initialFocusRef} />
</Stack>
</ModalBody>
<ModalFooter>
<Button variant="primary" isLoading={isSubmitting} type="submit" mr="4">
<Trans>Confirm</Trans>
</Button>
</ModalFooter>
</form>
)}
</Formik>
</ModalContent>
</Modal>
</Box>
)
}
EditableUserEmail.propTypes = {
detailValue: string,
}