-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
SignUpForm.js
288 lines (270 loc) · 9.27 KB
/
SignUpForm.js
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import React from "react";
import { useNavigate } from "react-router-dom";
import { useMutation } from "react-query";
import LoadingButton from "@mui/lab/LoadingButton";
import {
Box,
Button,
Card,
CardContent,
Checkbox,
Fade,
FormControl,
FormControlLabel,
FormHelperText as FHT,
Stack,
TextField,
Typography,
Divider,
} from "@mui/material";
import { InlineErrorHandler } from ".";
import { WordmarkState, passwordValidation } from "globals.js";
import { styled } from "@mui/material/styles";
import { HelpPrivacyTermsButton } from "Components";
import { useToggle } from "hooks/useToggle";
import BaseAPI from "api/AuthAPI";
import { useFormik } from "formik";
import * as Yup from "yup";
const PREFIX = "SignUpForm";
const classes = {
button: `${PREFIX}-button`,
card: `${PREFIX}-card`,
cardContent: `${PREFIX}-card-content`,
logo: `${PREFIX}-logo`,
};
const Root = styled("div")(({ theme }) => ({
display: "flex",
height: "100%",
width: "100%",
alignItems: "center",
justifyContent: "center",
position: "absolute",
[`& .${classes.button}`]: {
paddingTop: theme.spacing(3),
paddingBottom: theme.spacing(3),
justifyContent: "space-between",
},
[`& .${classes.card}`]: {
borderRadius: theme.spacing(2),
width: "500px",
},
[`& .${classes.cardContent}`]: {
padding: "48px 40px !important",
},
[`& .${classes.logo}`]: {
width: "100%",
maxWidth: "130px",
},
}));
// VALIDATION SCHEMA
const SignupSchema = Yup.object().shape({
email: Yup.string().email("Invalid email").required("Email is required"),
name: Yup.string().required("Full name is required"),
affiliation: Yup.string()
.min(2, "Affiliation must be at least 2 characters long")
.required("Affiliation is required"),
password: passwordValidation(Yup.string()).required("Password is required"),
confirmPassword: Yup.string()
.required("Password confirmation is required")
.oneOf([Yup.ref("password"), null], "Passwords must match"),
});
const SignUpForm = (props) => {
// Pass the useFormik() hook initial form values, a validate function that will be called when
// form values change or fields are blurred, and a submit function that will
// be called when the form is submitted
const navigate = useNavigate();
const [showPassword, toggleShowPassword] = useToggle(false);
const initialValues = {
email: "",
name: "",
affiliation: "",
password: "",
confirmPassword: "",
publicAccount: true,
};
const formik = useFormik({
initialValues: initialValues,
validationSchema: SignupSchema,
});
const { error, isError, mutate } = useMutation(BaseAPI.signup, {
onSuccess: () => {
let email = formik.values.email;
formik.setValues(initialValues, false);
if (typeof props.showNotification === "function") {
props.showNotification(
`A confirmation email has been sent to ${email}.`,
);
}
navigate("/signin");
},
});
const handleSubmit = () => {
mutate(formik.values);
};
const handleSignIn = () => {
navigate("/signin");
};
const handleEnterKey = (e) => {
if (e.keyCode === 13) {
handleSubmit(e);
}
};
return (
<Root>
<Fade in>
<Box>
<Card className={classes.card} variant="outlined">
<CardContent className={classes.cardContent}>
<Stack spacing={3}>
<img
className={classes.logo}
src={WordmarkState()}
alt="ASReview LAB"
/>
<Typography variant="h5">Create your profile</Typography>
<Stack
spacing={3}
component="form"
noValidate
>
<TextField
id="email"
name="email"
label="Email"
size="small"
type="email"
autoComplete="email"
fullWidth
value={formik.values.email}
onChange={formik.handleChange}
onBlur={formik.handleBlur}
/>
{formik.touched.email && formik.errors.email ? (
<FHT error={true}>{formik.errors.email}</FHT>
) : null}
<FormControl>
<Stack direction="row" spacing={2}>
<TextField
id="password"
label="Password"
size="small"
autoComplete="new-password"
fullWidth
type={showPassword ? "text" : "password"}
value={formik.values.password}
onChange={formik.handleChange}
onBlur={formik.handleBlur}
/>
<TextField
id="confirmPassword"
label="Confirm Password"
size="small"
autoComplete="new-password"
fullWidth
type={showPassword ? "text" : "password"}
onKeyDown={handleEnterKey}
value={formik.values.confirmPassword}
onChange={formik.handleChange}
onBlur={formik.handleBlur}
/>
</Stack>
</FormControl>
{formik.touched.password && formik.errors.password ? (
<FHT error={true}>{formik.errors.password}</FHT>
) : null}
{formik.touched.confirmPassword &&
formik.errors.confirmPassword ? (
<FHT error={true}>{formik.errors.confirmPassword}</FHT>
) : null}
<FormControl>
<FormControlLabel
control={
<Checkbox
id="public"
color="primary"
onChange={toggleShowPassword}
/>
}
label="Show password"
/>
{false && (
<>
<FormControlLabel
control={
<Checkbox
color="primary"
id="publicAccount"
defaultChecked={formik.values.publicAccount}
value={formik.values.publicAccount}
onChange={formik.handleChange}
onBlur={formik.handleBlur}
/>
}
label="Make this account public"
/>
<FHT>
Making this account public allows you to collaborate.
</FHT>
</>
)}
</FormControl>
{isError && <InlineErrorHandler message={error.message} />}
<Divider />
<TextField
id="name"
name="name"
label="Full name"
size="small"
autoComplete="name"
fullWidth
value={formik.values.name}
onChange={formik.handleChange}
onBlur={formik.handleBlur}
/>
{formik.touched.name && formik.errors.name ? (
<FHT error={true}>{formik.errors.name}</FHT>
) : null}
<TextField
id="affiliation"
label="Affiliation"
size="small"
name="affiliation"
autoComplete="organization"
fullWidth
value={formik.values.affiliation}
onChange={formik.handleChange}
onBlur={formik.handleBlur}
/>
{formik.touched.affiliation && formik.errors.affiliation ? (
<FHT error={true}>{formik.errors.affiliation}</FHT>
) : null}
<Stack className={classes.button} direction="row">
<Button
id="sign-in"
onClick={handleSignIn}
sx={{ textTransform: "none" }}
>
Sign In instead
</Button>
<LoadingButton
//loading={isLoading}
id="create-profile"
variant="contained"
color="primary"
onClick={handleSubmit}
disabled={!(formik.isValid && formik.dirty)}
>
Create
</LoadingButton>
</Stack>
</Stack>
</Stack>
</CardContent>
</Card>
<HelpPrivacyTermsButton />
</Box>
</Fade>
</Root>
);
};
export default SignUpForm;