forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateUserPage.js
More file actions
240 lines (226 loc) · 7.01 KB
/
CreateUserPage.js
File metadata and controls
240 lines (226 loc) · 7.01 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import React, { useState } from 'react'
import {
Box,
Button,
Divider,
Heading,
Stack,
Text,
useToast,
Checkbox,
Link,
} from '@chakra-ui/react'
import { useMutation } from '@apollo/client'
import { Link as RouteLink, useParams } from 'react-router-dom'
import { Formik } from 'formik'
import { t, Trans } from '@lingui/macro'
import { ArrowForwardIcon, CheckCircleIcon } from '@chakra-ui/icons'
import { LanguageSelect } from './LanguageSelect'
import { EmailField } from '../components/fields/EmailField'
import { DisplayNameField } from '../components/fields/DisplayNameField'
import { PasswordConfirmation } from '../components/fields/PasswordConfirmation'
import { LoadingMessage } from '../components/LoadingMessage'
import { createValidationSchema } from '../utilities/fieldRequirements'
import { useUserVar } from '../utilities/userState'
import { activate } from '../utilities/i18n.config'
import { SIGN_UP } from '../graphql/mutations'
export default function CreateUserPage() {
const { login } = useUserVar()
const toast = useToast()
const userOrgToken = useParams().userOrgToken || ''
const [showVerifyMessage, setShowVerifyMessage] = useState(false)
const [signUp, { loading }] = useMutation(SIGN_UP, {
onError(error) {
toast({
title: error.message,
description: t`Unable to create your account, please try again.`,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ signUp }) {
if (signUp.result.__typename === 'AuthResult') {
login({
jwt: signUp.result.authToken,
tfaSendMethod: signUp.result.user.tfaSendMethod,
userName: signUp.result.user.userName,
emailValidated: signUp.result.user.emailValidated,
})
if (signUp.result.user.preferredLang === 'ENGLISH') activate('en')
else if (signUp.result.user.preferredLang === 'FRENCH') activate('fr')
setShowVerifyMessage(true)
// Display a welcome message
toast({
title: t`Account created.`,
description: t`Welcome, you are successfully signed in to your new account!`,
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else if (signUp.result.__typename === 'SignUpError') {
toast({
title: t`Unable to create account, please try again.`,
description: signUp.result.description,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else {
toast({
title: t`Incorrect send method received.`,
description: t`Incorrect signUp.result typename.`,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
console.log('Incorrect signUp.result typename.')
}
},
})
if (loading) return <LoadingMessage />
if (showVerifyMessage)
return (
<Stack px="8" mx="auto" overflow="hidden" align="center">
<Stack isInline align="center">
<CheckCircleIcon color="strong" />
<Text fontWeight="bold" fontSize="2xl" textAlign="center">
<Trans>
A verification link has been sent to your email account
</Trans>
</Text>
</Stack>
<Divider />
<Text fontSize="lg">
<Trans>
Please follow the link in order to verify your account and start
using Tracker.
</Trans>
</Text>
<Divider />
<Button
as={RouteLink}
to="/"
color="primary"
bg="transparent"
borderColor="primary"
borderWidth="1px"
rightIcon={<ArrowForwardIcon />}
>
<Trans>Continue</Trans>
</Button>
</Stack>
)
const addUserToOrgText = userOrgToken ? (
<Text fontSize="md">
Your account will automatically be linked to the organization that invited
you.
</Text>
) : (
''
)
return (
<Box px="4" mx="auto" overflow="hidden" w="100%">
<Formik
validationSchema={createValidationSchema([
'email',
'displayName',
'password',
'confirmPassword',
'lang',
])}
initialValues={{
email: '',
displayName: '',
password: '',
confirmPassword: '',
lang: '',
}}
onSubmit={async (values) => {
signUp({
variables: {
userName: values.email,
displayName: values.displayName,
password: values.password,
confirmPassword: values.confirmPassword,
preferredLang: values.lang,
signUpToken: userOrgToken,
},
})
}}
>
{({ handleSubmit, isSubmitting }) => (
<form id="form" onSubmit={handleSubmit}>
<Heading
as="h1"
fontSize="3xl"
mb="8"
textAlign={{ lg: 'left', md: 'center' }}
>
<Trans>Register</Trans>
</Heading>
<Box mb="4">
<Text fontWeight="bold" mb="2" fontSize="lg">
<Trans>Welcome to Tracker, please enter your details.</Trans>
</Text>
<Text>
<Trans>
Let's get you set up so you can verify your account
information and begin using Tracker.
</Trans>
</Text>
{addUserToOrgText}
</Box>
<Stack
direction={['column', 'row']}
mb="4"
w={{ lg: '50%', md: '100%' }}
>
<EmailField />
<DisplayNameField />
</Stack>
<PasswordConfirmation
direction={['column', 'row']}
w={{ lg: '50%', md: '100%' }}
mb="2"
/>
<LanguageSelect name="lang" w={{ lg: '25%', md: '50%' }} mb="6" />
<Box ml={{ lg: '12', md: '0' }} mb="4">
<Checkbox
colorScheme="orange"
isRequired
mb="4"
borderColor="black"
>
I agree to all Terms, Privacy Policy & Code of Conduct
Guidelines
</Checkbox>
<Box>
<Button
variant="primary"
type="submit"
id="submitBtn"
isLoading={isSubmitting}
w={['100%', '25%']}
mb="4"
>
<Trans>Create Account</Trans>
</Button>
</Box>
<Text>
Already have an account?{' '}
<Link as={RouteLink} to="/sign-in">
Log in
</Link>
</Text>
</Box>
</form>
)}
</Formik>
</Box>
)
}