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
169 lines (156 loc) · 5.01 KB
/
CreateUserPage.js
File metadata and controls
169 lines (156 loc) · 5.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
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 { useLingui } from '@lingui/react'
import DisplayNameField from './DisplayNameField'
import { fieldRequirements } from './fieldRequirements'
import { TrackerButton } from './TrackerButton'
export default function CreateUserPage() {
const { login } = useUserState()
const history = useHistory()
const toast = useToast()
const { i18n } = useLingui()
const { userOrgToken } = useParams()
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, error }] = useMutation(SIGN_UP, {
onError() {
console.log(error)
toast({
title: i18n._(t`An error occurred.`),
description: i18n._(
t`Unable to create your account, please try again.`,
),
status: 'error',
duration: 9000,
isClosable: true,
})
},
onCompleted({ signUp }) {
login({
jwt: signUp.authResult.authToken,
tfa: signUp.authResult.user.tfa,
userName: signUp.authResult.user.userName,
})
// redirect to the home page.
history.push('/')
// Display a welcome message
toast({
title: i18n._(t`Account created.`),
description: i18n._(
t`Welcome, you are successfully signed in to your new account!`,
),
status: 'success',
duration: 9000,
isClosable: true,
})
},
})
if (loading)
return (
<p>
<Trans>Loading...</Trans>
</p>
)
if (error) return <p>{String(error)}</p>
const addUserToOrgText =
userOrgToken !== undefined ? (
<Text fontSize="md">
Your account will automatically be linked to the organization that
invited you.
</Text>
) : (
''
)
return (
<Box px="8" 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>
)
}