|
| 1 | +import React, { Component, PropTypes } from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
| 3 | +import { pushState } from 'redux-router'; |
| 4 | +import { login } from '../../redux/modules/auth'; |
| 5 | + |
| 6 | +import './LoginPage.scss'; |
| 7 | + |
| 8 | +class Login extends Component { |
| 9 | + |
| 10 | + componentWillReceiveProps(nextProps) { |
| 11 | + console.log('Login: componentWillReceiveProps: '+nextProps); |
| 12 | + console.log('user: '+this.props.user+'\tnextUser='+nextProps.user); |
| 13 | + |
| 14 | + if (nextProps.user) { |
| 15 | + // logged in, let's show home |
| 16 | + this.dispatch(pushState(null, '/counter')); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + handleLogin(event) { |
| 21 | + event.preventDefault(); |
| 22 | + const username = this.refs.username; // need for getDOMNode() call going away in React 0.14 |
| 23 | + const password = this.refs.password; |
| 24 | + this.props.dispatch(login(username.value, password.value)); |
| 25 | + // username.value = ''; |
| 26 | + // password.value = ''; |
| 27 | + } |
| 28 | + |
| 29 | + render(){ |
| 30 | + const {user, loginError} = this.props; |
| 31 | + return( |
| 32 | + <div className="container"> |
| 33 | + <div className="row"> |
| 34 | + <div className="col-md-4 col-md-offset-4"> |
| 35 | + <div className="panel panel-default panel-signin"> |
| 36 | + <div className="panel-heading"> |
| 37 | + <h3 className="panel-title">Please Log in</h3> |
| 38 | + </div> |
| 39 | + <form className="form-signin"> |
| 40 | + |
| 41 | + <div className="input-group"> |
| 42 | + <span className="input-group-addon"><i className="fa fa-user"/></span> |
| 43 | + <input type="text" ref="username" className="form-control" placeholder="Username" required autofocus/> |
| 44 | + </div> |
| 45 | + |
| 46 | + <div className="input-group"> |
| 47 | + <span className="input-group-addon"><i className="fa fa-lock"/></span> |
| 48 | + <input type="password" ref="password" className="form-control" placeholder="Password" required/> |
| 49 | + </div> |
| 50 | + |
| 51 | + <div className="checkbox"> |
| 52 | + <label> |
| 53 | + <input type="checkbox" value="remember-me"/> Remember me |
| 54 | + </label> |
| 55 | + </div> |
| 56 | + |
| 57 | + { |
| 58 | + !user && loginError && |
| 59 | + <div className="alert alert-danger"> |
| 60 | + {loginError.message}. Hint: use admin/password to log in. |
| 61 | + </div> |
| 62 | + } |
| 63 | + |
| 64 | + <button className="btn btn-primary btn-block" onClick={::this.handleLogin}><i className="fa fa-sign-in"/>{' '}Log in</button> |
| 65 | + </form> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +Login.propTypes = { |
| 75 | + user: PropTypes.string, |
| 76 | + loginError: PropTypes.object, |
| 77 | + dispatch: PropTypes.func.isRequired, |
| 78 | + routerState: PropTypes.object.isRequired |
| 79 | +}; |
| 80 | + |
| 81 | +function mapStateToProps(state){ |
| 82 | + const { auth, router } = state; |
| 83 | + if(auth){ |
| 84 | + return {user: auth.user, loginError: auth.loginError, routerState: router}; |
| 85 | + }else{ |
| 86 | + return {user: null, routerState: router}; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export default connect(mapStateToProps)(Login); |
0 commit comments