-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
SignUpForm.tsx
205 lines (196 loc) · 6.69 KB
/
SignUpForm.tsx
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
import React from "react";
import { useService } from "@xstate/react";
import { Interpreter } from "xstate";
import { Link } from "react-router-dom";
import {
Button,
Container,
CssBaseline,
TextField,
Grid,
Box,
Typography,
makeStyles,
} from "@material-ui/core";
import { Formik, Form, Field, FieldProps } from "formik";
import { string, object, ref } from "yup";
import RWALogo from "./SvgRwaLogo";
import Footer from "./Footer";
import { SignUpPayload } from "../models";
import { AuthMachineContext, AuthMachineEvents } from "../machines/authMachine";
const validationSchema = object({
firstName: string().required("First Name is required"),
lastName: string().required("Last Name is required"),
username: string().required("Username is required"),
password: string()
.min(4, "Password must contain at least 4 characters")
.required("Enter your password"),
confirmPassword: string()
.required("Confirm your password")
.oneOf([ref("password")], "Password does not match"),
});
const useStyles = makeStyles((theme) => ({
paper: {
marginTop: theme.spacing(8),
display: "flex",
flexDirection: "column",
alignItems: "center",
},
logo: {
color: theme.palette.primary.main,
},
form: {
width: "100%", // Fix IE 11 issue.
marginTop: theme.spacing(1),
},
submit: {
margin: theme.spacing(3, 0, 2),
},
}));
export interface Props {
authService: Interpreter<AuthMachineContext, any, AuthMachineEvents, any>;
}
const SignUpForm: React.FC<Props> = ({ authService }) => {
const classes = useStyles();
const [, sendAuth] = useService(authService);
const initialValues: SignUpPayload & { confirmPassword: string } = {
firstName: "",
lastName: "",
username: "",
password: "",
confirmPassword: "",
};
const signUpPending = (payload: SignUpPayload) => sendAuth({ type: "SIGNUP", ...payload });
return (
<Container component="main" maxWidth="xs">
<CssBaseline />
<div className={classes.paper}>
<div>
<RWALogo className={classes.logo} />
</div>
<Typography component="h1" variant="h5" data-test="signup-title">
Sign Up
</Typography>
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={async (values, { setSubmitting, setFieldValue }) => {
setSubmitting(true);
signUpPending(values);
}}
>
{({ isValid, isSubmitting, dirty }) => (
<Form className={classes.form}>
<Field name="firstName">
{({ field, meta: { error, value, initialValue, touched } }: FieldProps) => (
<TextField
variant="outlined"
margin="normal"
required
fullWidth
id="firstName"
label="First Name"
type="text"
autoFocus
data-test="signup-first-name"
error={(touched || value !== initialValue) && Boolean(error)}
helperText={touched || value !== initialValue ? error : ""}
{...field}
/>
)}
</Field>
<Field name="lastName">
{({ field, meta: { error, value, initialValue, touched } }: FieldProps) => (
<TextField
variant="outlined"
margin="normal"
required
fullWidth
id="lastName"
label="Last Name"
type="text"
data-test="signup-last-name"
error={(touched || value !== initialValue) && Boolean(error)}
helperText={touched || value !== initialValue ? error : ""}
{...field}
/>
)}
</Field>
<Field name="username">
{({ field, meta: { error, value, initialValue, touched } }: FieldProps) => (
<TextField
variant="outlined"
margin="normal"
required
fullWidth
id="username"
label="Username"
type="text"
data-test="signup-username"
error={(touched || value !== initialValue) && Boolean(error)}
helperText={touched || value !== initialValue ? error : ""}
{...field}
/>
)}
</Field>
<Field name="password">
{({ field, meta: { error, value, initialValue, touched } }: FieldProps) => (
<TextField
variant="outlined"
margin="normal"
required
fullWidth
label="Password"
type="password"
id="password"
data-test="signup-password"
error={(touched || value !== initialValue) && Boolean(error)}
helperText={touched || value !== initialValue ? error : ""}
{...field}
/>
)}
</Field>
<Field name="confirmPassword">
{({ field, meta: { error, value, initialValue, touched } }: FieldProps) => (
<TextField
variant="outlined"
margin="normal"
required
fullWidth
label="Confirm Password"
id="confirmPassword"
data-test="signup-confirmPassword"
type="password"
error={(touched || value !== initialValue) && Boolean(error)}
helperText={touched || value !== initialValue ? error : ""}
{...field}
/>
)}
</Field>
<Button
type="submit"
fullWidth
variant="contained"
color="primary"
className={classes.submit}
data-test="signup-submit"
disabled={!isValid || isSubmitting}
>
Sign Up
</Button>
<Grid container>
<Grid item>
<Link to="/signin">{"Have an account? Sign In"}</Link>
</Grid>
</Grid>
</Form>
)}
</Formik>
</div>
<Box mt={8}>
<Footer />
</Box>
</Container>
);
};
export default SignUpForm;