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
183 lines (170 loc) · 5.79 KB
/
CreateUserPage.js
File metadata and controls
183 lines (170 loc) · 5.79 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
import React from 'react'
import { Button, Stack, useToast, Box, Heading, Text } from '@chakra-ui/core'
import { useMutation } from '@apollo/client'
import { object, string } from 'yup'
import { Link as RouteLink, useHistory, useParams } from 'react-router-dom'
import { Formik } from 'formik'
import { SIGN_UP } from './graphql/mutations'
import { useUserState } from './UserState'
import EmailField from './EmailField'
import PasswordConfirmation from './PasswordConfirmation'
import LanguageSelect from './LanguageSelect'
import { t, Trans } from '@lingui/macro'
import { i18n } from '@lingui/core'
import DisplayNameField from './DisplayNameField'
import { fieldRequirements } from './fieldRequirements'
import { TrackerButton } from './TrackerButton'
import { LoadingMessage } from './LoadingMessage'
import { activate } from './i18n.config'
export default function CreateUserPage() {
const { login } = useUserState()
const history = useHistory()
const toast = useToast()
const userOrgToken = useParams().userOrgToken || ''
const validationSchema = object().shape({
email: string()
.required(i18n._(fieldRequirements.email.required.message))
.email(i18n._(fieldRequirements.email.email.message)),
displayName: string().required(
i18n._(fieldRequirements.displayName.required.message),
),
password: string()
.required(i18n._(fieldRequirements.password.required.message))
.min(
fieldRequirements.password.min.minLength,
i18n._(fieldRequirements.password.min.message),
),
confirmPassword: string()
.required(i18n._(fieldRequirements.confirmPassword.required.message))
.oneOf(
fieldRequirements.confirmPassword.oneOf.types,
i18n._(fieldRequirements.confirmPassword.oneOf.message),
),
lang: string()
.required(i18n._(fieldRequirements.lang.required.message))
.oneOf(fieldRequirements.lang.oneOf.types),
})
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,
})
if (signUp.result.user.preferredLang === 'ENGLISH') activate('en')
else if (signUp.result.user.preferredLang === 'FRENCH') activate('fr')
// redirect to the home page.
history.push('/')
// 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 />
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">
<Formik
validationSchema={validationSchema}
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="2xl" mb="6" textAlign="center">
<Trans>
Create an account by entering an email and password.
</Trans>
</Heading>
{addUserToOrgText}
<EmailField name="email" width="100%" mb="4" />
<DisplayNameField name="displayName" width="100%" mb="4" />
<PasswordConfirmation spacing="4" width="100%" mb="4" />
<LanguageSelect name="lang" width="100%" mb="4" />
<Stack spacing={4} isInline justifyContent="space-between" mb="4">
<TrackerButton
type="submit"
id="submitBtn"
isLoading={isSubmitting}
variant="primary"
>
<Trans>Create Account</Trans>
</TrackerButton>
<Button
as={RouteLink}
to="/sign-in"
color="primary"
bg="transparent"
borderColor="primary"
borderWidth="1px"
>
<Trans>Back</Trans>
</Button>
</Stack>
</form>
)}
</Formik>
</Box>
)
}