78 lines
2.4 KiB
JavaScript
78 lines
2.4 KiB
JavaScript
import React from 'react'
|
|
import { connect } from 'react-redux'
|
|
import { bindActionCreators } from 'redux'
|
|
|
|
import { loginUser } from '../../actions/users'
|
|
|
|
function mapStateToProps(state) {
|
|
return { user: state.userStore };
|
|
}
|
|
const mapDispatchToProps = (dispatch) =>
|
|
bindActionCreators({ loginUser }, dispatch)
|
|
|
|
class UserLoginForm extends React.Component {
|
|
constructor(props) {
|
|
super(props);
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
}
|
|
componentWillReceiveProps(nextProps) {
|
|
if (!nextProps.user.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.user.userLoading) {
|
|
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.user.userLoading &&
|
|
<button className="btn btn-primary pull-right">
|
|
<i className="fa fa-spinner fa-spin"></i>
|
|
</button>
|
|
}
|
|
{this.props.user.userLoading ||
|
|
<input className="btn btn-primary pull-right" type="submit" value="Log in"/>
|
|
}
|
|
<br/>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
export default connect(mapStateToProps, mapDispatchToProps)(UserLoginForm);
|