forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditableUserPhoneNumber.js
More file actions
173 lines (162 loc) · 4.85 KB
/
EditableUserPhoneNumber.js
File metadata and controls
173 lines (162 loc) · 4.85 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
import React, { useRef } from 'react'
import { string } from 'prop-types'
import {
Icon,
Heading,
Stack,
Text,
Modal,
ModalOverlay,
ModalContent,
ModalHeader,
ModalCloseButton,
ModalBody,
ModalFooter,
SlideIn,
useDisclosure,
useToast,
} from '@chakra-ui/core'
import WithPseudoBox from './withPseudoBox'
import { Formik } from 'formik'
import { t, Trans } from '@lingui/macro'
import { UPDATE_USER_PROFILE } from './graphql/mutations'
import { useMutation } from '@apollo/client'
import { useUserState } from './UserState'
import { number, object } from 'yup'
import { fieldRequirements } from './fieldRequirements'
import { TrackerButton } from './TrackerButton'
import PhoneNumberField from './PhoneNumberField'
function EditableUserPhoneNumber({ detailValue }) {
const { isOpen, onOpen, onClose } = useDisclosure()
const { currentUser } = useUserState()
const toast = useToast()
const initialFocusRef = useRef()
const [updateUserProfile, { error: _updateUserProfileError }] = useMutation(
UPDATE_USER_PROFILE,
{
context: {
headers: {
authorization: currentUser.jwt,
},
},
onError: ({ message }) => {
toast({
title: t`An error occurred while updating your phone number.`,
description: message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted() {
toast({
title: t`Changed User Phone Number`,
description: t`You have successfully updated your phone number.`,
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
onClose()
},
},
)
const validationSchema = object().shape({
phoneNumber: number()
.required(fieldRequirements.phoneNumber.required.message)
.typeError(fieldRequirements.phoneNumber.typeError.message),
})
return (
<Stack>
<Heading as="h3" size="md">
<Trans>Phone Number:</Trans>
</Heading>
<Stack isInline align="center">
<Icon name="phone" color="gray.300" />
{detailValue ? (
<Text>{detailValue}</Text>
) : (
<Trans>No current phone number</Trans>
)}
<TrackerButton
ml="auto"
onClick={onOpen}
variant="primary"
fontSize="sm"
px="3"
>
<Trans>Edit</Trans>
</TrackerButton>
</Stack>
<SlideIn in={isOpen}>
{(styles) => (
<Modal
isOpen={true}
onClose={onClose}
initialFocusRef={initialFocusRef}
>
<ModalOverlay opacity={styles.opacity} />
<ModalContent pb="4" {...styles}>
<Formik
validateOnBlur={false}
initialValues={{
phoneNumber: '',
}}
validationSchema={validationSchema}
onSubmit={async (values) => {
// Submit update detail mutation
await updateUserProfile({
variables: {
phoneNumber: values.phoneNumber,
},
})
}}
>
{({ handleSubmit, isSubmitting }) => (
<form id="form" onSubmit={handleSubmit}>
<ModalHeader>
<Trans>Edit </Trans>
</ModalHeader>
<ModalCloseButton />
<ModalBody>
<Stack spacing="4" p="6">
{detailValue && (
<Stack>
<Heading as="h3" size="sm">
<Trans>Current Phone Number:</Trans>
</Heading>
<Text>{detailValue}</Text>
</Stack>
)}
<PhoneNumberField
name="phoneNumber"
label={t`New Phone Number:`}
ref={initialFocusRef}
/>
</Stack>
</ModalBody>
<ModalFooter>
<TrackerButton
isLoading={isSubmitting}
type="submit"
mr="4"
variant="primary"
>
<Trans>Confirm</Trans>
</TrackerButton>
</ModalFooter>
</form>
)}
</Formik>
</ModalContent>
</Modal>
)}
</SlideIn>
</Stack>
)
}
EditableUserPhoneNumber.propTypes = {
detailValue: string,
}
export default WithPseudoBox(EditableUserPhoneNumber)