81 lines
2.5 KiB
JavaScript
81 lines
2.5 KiB
JavaScript
import React from "react"
|
|
import { connect } from "react-redux"
|
|
import { bindActionCreators } from "redux"
|
|
|
|
import { loginUser } from "../../actions/users"
|
|
|
|
function mapStateToProps(state) {
|
|
return {
|
|
isLogged: state.userStore.get("isLogged"),
|
|
loading: state.userStore.get("loading"),
|
|
};
|
|
}
|
|
const mapDispatchToProps = (dispatch) =>
|
|
bindActionCreators({ loginUser }, dispatch)
|
|
|
|
class UserLoginForm extends React.PureComponent {
|
|
constructor(props) {
|
|
super(props);
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
}
|
|
componentWillReceiveProps(nextProps) {
|
|
if (!nextProps.isLogged) {
|
|
return
|
|
}
|
|
if (!nextProps.location.query.redirect) {
|
|
// Redirect home
|
|
nextProps.router.push("/");
|
|
} else {
|
|
// Redirect to the previous page
|
|
nextProps.router.push(nextProps.location.query.redirect);
|
|
}
|
|
}
|
|
handleSubmit(e) {
|
|
e.preventDefault();
|
|
if (this.props.loading) {
|
|
return;
|
|
}
|
|
const username = this.refs.username.value;
|
|
const password = this.refs.password.value;
|
|
this.props.loginUser(username, password);
|
|
}
|
|
render() {
|
|
return (
|
|
<div className="container">
|
|
<div className="content-fluid">
|
|
<div className="col-md-6 col-md-offset-3 col-xs-12">
|
|
<h2>Log in</h2>
|
|
<hr/>
|
|
<form ref="loginForm" className="form-horizontal" onSubmit={(e) => this.handleSubmit(e)}>
|
|
<div>
|
|
<label htmlFor="user_email">Username</label>
|
|
<br/>
|
|
<input className="form-control" type="username" ref="username"/>
|
|
<p></p>
|
|
</div>
|
|
<div>
|
|
<label htmlFor="user_password">Password</label>
|
|
<br/>
|
|
<input className="form-control" type="password" ref="password"/>
|
|
<p></p>
|
|
</div>
|
|
<div>
|
|
{this.props.loading &&
|
|
<button className="btn btn-primary pull-right">
|
|
<i className="fa fa-spinner fa-spin"></i>
|
|
</button>
|
|
}
|
|
{this.props.loading ||
|
|
<input className="btn btn-primary pull-right" type="submit" value="Log in"/>
|
|
}
|
|
<br/>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
export default connect(mapStateToProps, mapDispatchToProps)(UserLoginForm);
|