This repository has been archived by the owner on Aug 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Login.js
184 lines (175 loc) · 4.68 KB
/
Login.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
import React, { Component } from 'react';
import { browserHistory } from 'react-router';
import TextField from 'material-ui/TextField';
import Grid from 'material-ui/Grid';
import Paper from 'material-ui/Paper';
import { withStyles } from 'material-ui/styles';
import Typography from 'material-ui/Typography';
import Button from 'material-ui/Button';
import Rain from './Rain/Rain';
import utils from '../utils/utils';
const styles = theme => ({
root: {
flexGrow: 1,
},
container: {
display: 'flex',
marginTop: '20px',
},
paper: {
textAlign: 'center',
color: theme.palette.text.secondary,
padding: 0,
width: 400,
},
title: {
padding: '40px',
paddingTop: '50px',
backgroundColor: theme.palette.primary.main,
color: '#666',
},
textField: {
marginLeft: theme.spacing.unit,
marginRight: theme.spacing.unit,
width: 235,
},
button: {
margin: '20px 10px 20px 10px',
},
});
class Login extends Component {
constructor(props) {
super(props);
this.state = {
user: '',
email: '',
password: '',
isAdmin: false,
};
this.handleChange = this.handleChange.bind(this);
this.userLogin = this.userLogin.bind(this);
}
componentDidMount() {
document.body.style.overflowY = 'hidden';
}
// User login
userLogin(e) {
const { email, password } = this.state;
e.preventDefault();
utils
.userLogin(email, password)
.then(response => {
if (response.status === 200) {
this.setState({
username: response.data.username,
});
browserHistory.push({
pathname: `/`,
state: {
username: response.data.username,
isAdmin: response.data.isAdmin,
token: response.data.token,
},
});
} else {
console.log('bad response ', response);
}
})
.catch(err => {
console.log('error in userLogin ', err);
});
}
// User Registration
register = () => {
browserHistory.push({
pathname: `/register`,
});
};
// Updates state based on table cell that was edited.
handleChange(e) {
const { value, id } = e.target;
switch (id) {
case 'email':
this.setState({ email: value });
break;
case 'password':
this.setState({ password: value });
break;
default:
break;
}
}
render() {
const { classes } = this.props;
const { email, password } = this.state;
return (
<div>
<Rain count={30} />
<Grid className={classes.root}>
<Grid
container
item
xs={12}
justify={'center'}
className={classes.container}
>
<Grid>
<Paper className={classes.paper}>
<Typography
className={classes.title}
variant="headline"
component="h3"
>
DebtTracker
</Typography>
<form className={classes.form}>
<Grid item xs={12}>
<TextField
id="email"
label="Email"
type="email"
value={email}
placeholder="Email"
onChange={this.handleChange}
className={classes.textField}
margin="normal"
/>
</Grid>
<Grid item xs={12}>
<TextField
id="password"
label="Password"
type="password"
value={password}
placeholder="Password"
onChange={this.handleChange}
className={classes.textField}
margin="normal"
/>
</Grid>
<Grid item xs={12}>
<Button
variant="raised"
className={classes.button}
onClick={event => this.userLogin(event)}
>
Login
</Button>
<Button
variant="raised"
className={classes.button}
onClick={() => this.register()}
>
Register
</Button>
</Grid>
</form>
</Paper>
</Grid>
</Grid>
</Grid>
</div>
);
}
}
export default withStyles(styles)(Login);