This repository was archived by the owner on Oct 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSignup.tsx
124 lines (112 loc) · 3.28 KB
/
Signup.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
import * as React from 'react';
import { RouteComponentProps, Link } from 'react-router-dom';
import {
withStyles,
WithStyles,
FormControl,
InputLabel,
Input,
Button,
Typography,
} from '@material-ui/core';
import { accountsPassword } from './accounts';
import FormError from './components/FormError';
const styles = () => ({
formContainer: {
display: 'flex',
flexDirection: 'column' as 'column',
},
});
const LogInLink = (props: any) => <Link to="/login" {...props} />;
interface State {
firstName: string;
lastName: string;
email: string;
password: string;
error: string | null;
}
class Signup extends React.Component<
WithStyles<'formContainer'> & RouteComponentProps<{}>,
State
> {
state = {
firstName: '',
lastName: '',
email: '',
password: '',
error: null,
};
onChangeFirstName = ({ target }: React.ChangeEvent<HTMLInputElement>) => {
this.setState({ firstName: target.value });
};
onChangeLastName = ({ target }: React.ChangeEvent<HTMLInputElement>) => {
this.setState({ lastName: target.value });
};
onChangeEmail = ({ target }: React.ChangeEvent<HTMLInputElement>) => {
this.setState({ email: target.value });
};
onChangePassword = ({ target }: React.ChangeEvent<HTMLInputElement>) => {
this.setState({ password: target.value });
};
onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
this.setState({ error: null });
try {
await accountsPassword.createUser({
firstName: this.state.firstName,
lastName: this.state.firstName,
email: this.state.email,
password: this.state.password,
});
this.props.history.push('/login');
} catch (err) {
this.setState({ error: err.message });
}
};
render() {
const { classes } = this.props;
const { firstName, lastName, email, password, error } = this.state;
return (
<form onSubmit={this.onSubmit} className={classes.formContainer}>
<Typography variant="display1" gutterBottom>
Sign up
</Typography>
<FormControl margin="normal">
<InputLabel htmlFor="firstName">Fist name</InputLabel>
<Input
id="firstName"
value={firstName}
onChange={this.onChangeFirstName}
/>
</FormControl>
<FormControl margin="normal">
<InputLabel htmlFor="lastName">Last name</InputLabel>
<Input
id="lastName"
value={lastName}
onChange={this.onChangeLastName}
/>
</FormControl>
<FormControl margin="normal">
<InputLabel htmlFor="email">Email</InputLabel>
<Input id="email" value={email} onChange={this.onChangeEmail} />
</FormControl>
<FormControl margin="normal">
<InputLabel htmlFor="password">Password</InputLabel>
<Input
id="password"
type="password"
value={password}
onChange={this.onChangePassword}
/>
</FormControl>
<Button variant="raised" color="primary" type="submit">
Sign Up
</Button>
{error && <FormError error={error} />}
<Button component={LogInLink}>Log In</Button>
</form>
);
}
}
export default withStyles(styles)(Signup);