Showing errors on login screen

This commit is contained in:
Cássio Zen 2016-12-27 16:58:02 -02:00
parent 8509b11104
commit ca98f72c0c
2 changed files with 46 additions and 8 deletions

View File

@ -13,6 +13,11 @@
.card img { .card img {
display: block; display: block;
margin: auto; margin: auto;
padding-bottom: 5px;
}
.errorMsg {
color: #dd0000;
} }
.button { .button {

View File

@ -10,7 +10,8 @@ export default class AuthenticationPage extends React.Component {
onLogin: React.PropTypes.func.isRequired, onLogin: React.PropTypes.func.isRequired,
}; };
state = { username: "", password: "" }; state = { username: "", password: "", errors: {} };
handleChange = (name, value) => { handleChange = (name, value) => {
this.setState({ ...this.state, [name]: value }); this.setState({ ...this.state, [name]: value });
@ -18,24 +19,56 @@ export default class AuthenticationPage extends React.Component {
handleLogin = (e) => { handleLogin = (e) => {
e.preventDefault(); e.preventDefault();
const { username, password } = this.state;
const errors = {};
if (!username) {
errors.username = 'Make sure to enter your user name';
}
if (!password) {
errors.password = 'Please enter your password';
}
if (Object.keys(errors).length > 0) {
this.setState({ errors });
return;
}
AuthenticationPage.authClient.login(this.state.username, this.state.password, true) AuthenticationPage.authClient.login(this.state.username, this.state.password, true)
.then((user) => { .then((user) => {
this.props.onLogin(user); this.props.onLogin(user);
}) })
.catch((err) => { throw err; }); .catch((error) => {
this.setState({ errors: { server: error.description || error.msg || error }, loggingIn: false });
});
}; };
render() { render() {
const { loginError } = this.state; const { errors } = this.state;
return ( return (
<section className={styles.root}> <section className={styles.root}>
{loginError && <p>{loginError}</p>}
<Card className={styles.card}> <Card className={styles.card}>
<img src={logo} width={100} /> <img src={logo} width={100} role="presentation" />
<Input type="text" label="Username" name="username" value={this.state.username} onChange={this.handleChange.bind(this, "username")} /> {errors.server && <p>
<Input type="password" label="Password" name="password" value={this.state.password} onChange={this.handleChange.bind(this, "password")} /> <span className={styles.errorMsg}>{errors.server}</span>
</p>}
<Input
type="text"
label="Username"
name="username"
value={this.state.username}
error={errors.username}
onChange={this.handleChange.bind(this, "username")} // eslint-disable-line
/>
<Input
type="password"
label="Password"
name="password"
value={this.state.password}
error={errors.password}
onChange={this.handleChange.bind(this, "password")} // eslint-disable-line
/>
<Button <Button
className={styles.button} className={styles.button}
raised raised