forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditableUserDisplayName.js
More file actions
177 lines (167 loc) · 4.88 KB
/
EditableUserDisplayName.js
File metadata and controls
177 lines (167 loc) · 4.88 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
import React, { useRef } from 'react'
import {
Button,
Flex,
Heading,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
Stack,
Text,
useDisclosure,
useToast,
} from '@chakra-ui/react'
import { PersonIcon } from '../theme/Icons'
import { Formik } from 'formik'
import { t, Trans } from '@lingui/macro'
import { useMutation } from '@apollo/client'
import { string } from 'prop-types'
import { DisplayNameField } from '../components/fields/DisplayNameField'
import { createValidationSchema } from '../utilities/fieldRequirements'
import { UPDATE_USER_PROFILE } from '../graphql/mutations'
export function EditableUserDisplayName({ detailValue }) {
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 display name.`,
description: message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ updateUserProfile }) {
if (updateUserProfile.result.__typename === 'UpdateUserProfileResult') {
toast({
title: t`Changed User Display Name`,
description: t`You have successfully updated your display name.`,
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
onClose()
} else if (
updateUserProfile.result.__typename === 'UpdateUserProfileError'
) {
toast({
title: t`Unable to update to your display name, 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 (
<Stack>
<Heading as="h3" size="md">
<Trans>Display Name:</Trans>
</Heading>
<Flex align="center">
<PersonIcon
color="gray.300"
mr={2}
boxSize="icons.lg"
aria-hidden="true"
/>
<Text>{detailValue}</Text>
<Button
variant="primary"
ml="auto"
onClick={onOpen}
fontSize="sm"
px="3"
>
<Trans>Edit</Trans>
</Button>
</Flex>
<Modal
isOpen={isOpen}
onClose={onClose}
initialFocusRef={initialFocusRef}
motionPreset="slideInBottom"
>
<ModalOverlay />
<ModalContent pb="4">
<Formik
validateOnBlur={false}
initialValues={{
displayName: '',
}}
initialTouched={{
displayName: true,
}}
validationSchema={createValidationSchema(['displayName'])}
onSubmit={async (values) => {
// Submit update detail mutation
await updateUserProfile({
variables: {
displayName: values.displayName,
},
})
}}
>
{({ handleSubmit, isSubmitting }) => (
<form id="form" onSubmit={handleSubmit}>
<ModalHeader>
<Trans>Edit Display Name</Trans>
</ModalHeader>
<ModalCloseButton />
<ModalBody>
<Stack spacing="4" p="6">
<Heading as="h3" size="sm">
<Trans>Current Display Name:</Trans>
</Heading>
<Text>{detailValue}</Text>
<DisplayNameField
name="displayName"
label={t`New Display Name:`}
ref={initialFocusRef}
/>
</Stack>
</ModalBody>
<ModalFooter>
<Button
variant="primary"
isLoading={isSubmitting}
type="submit"
mr="4"
>
<Trans>Confirm</Trans>
</Button>
</ModalFooter>
</form>
)}
</Formik>
</ModalContent>
</Modal>
</Stack>
)
}
EditableUserDisplayName.propTypes = {
detailValue: string,
}