Skip to content

Instantly share code, notes, and snippets.

@waylandc
Last active April 10, 2018 03:18
Show Gist options
  • Save waylandc/a47fa5c3d3817d0b38c740625a26ec43 to your computer and use it in GitHub Desktop.
Save waylandc/a47fa5c3d3817d0b38c740625a26ec43 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { Button, FormGroup, FormControl, ControlLabel } from 'react-bootstrap';
import Axios from 'axios';
import './Login.css';
export default class Login extends Component {
constructor(props) {
super(props);
this.state = {
email: "",
password: "",
errors: [],
};
}
validateForm() {
return this.state.email.length > 0 && this.state.password.length > 0;
}
handleChange = event => {
this.setState({
[event.target.id]: event.target.value
});
}
handleSubmit = (event) => {
event.preventDefault();
// sent request to our REST API
Axios.post('/users/login',
{
email: this.state.email,
password: this.state.password
}
)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
const errs = [];
errs.push(error);
this.setState({ errors: errs });
console.log(error);
});
}
render() {
const { errors } = this.state;
return (
<div className="Login">
{errors.map(error => (
<p key={error}>Error: {error}</p>
))}
<form onSubmit={this.handleSubmit}>
<FormGroup controlId="email" bsSize="large">
<ControlLabel>Username</ControlLabel>
<FormControl
autoFocus
type="string"
value={this.state.email}
onChange={this.handleChange}
/>
</FormGroup>
<FormGroup controlId="password" bsSize="large">
<ControlLabel>Password</ControlLabel>
<FormControl
value={this.state.password}
onChange={this.handleChange}
type="password"
/>
</FormGroup>
<Button
block
bsSize="large"
disabled={!this.validateForm()}
type="submit"
>Login</Button>
</form>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment