Advertisement
Guest User

Login

a guest
Nov 28th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Login
  2. import React from 'react';
  3. import { Link } from 'react-router-dom';
  4. import { connect } from 'react-redux';
  5. import { userActions } from './../_actions';
  6.  
  7. class LoginPage extends React.Component {
  8.     constructor(props) {
  9.         super(props);
  10.  
  11.         this.state = {
  12.             username: '',
  13.             password: '',
  14.             submitted: false
  15.         };
  16.  
  17.         this.handleChange = this.handleChange.bind(this);
  18.         this.handleSubmit = this.handleSubmit.bind(this);
  19.     }
  20.  
  21.     handleChange(e) {
  22.         const { name, value } = e.target;
  23.         this.setState({ [name]: value });
  24.     }
  25.  
  26.     handleSubmit(e) {
  27.         e.preventDefault();
  28.  
  29.         this.setState({ submitted: true });
  30.         const { username, password } = this.state;
  31.         const { dispatch } = this.props;
  32.         if (username && password) {
  33.             dispatch(userActions.login(username, password));
  34.         }
  35.     }
  36.  
  37.     render() {
  38.         const { loggingIn } = this.props;
  39.         const { username, password, submitted } = this.state;
  40.         return (
  41.             <div className="col-md-6 col-md-offset-3">
  42.                 <h2>Login</h2>
  43.                 <form name="form" onSubmit={this.handleSubmit}>
  44.                     <div className={'form-group' + (submitted && !username ? ' has-error' : '')}>
  45.                         <label htmlFor="username">Username</label>
  46.                         <input type="text" className="form-control" name="username" value={username} onChange={this.handleChange} />
  47.                         {submitted && !username &&
  48.                             <div className="help-block">Username is required</div>
  49.                         }
  50.                     </div>
  51.                     <div className={'form-group' + (submitted && !password ? ' has-error' : '')}>
  52.                         <label htmlFor="password">Password</label>
  53.                         <input type="password" className="form-control" name="password" value={password} onChange={this.handleChange} />
  54.                         {submitted && !password &&
  55.                             <div className="help-block">Password is required</div>
  56.                         }
  57.                     </div>
  58.                     <div className="form-group">
  59.                         <button className="btn btn-primary">Login</button>
  60.                         { loggingIn }
  61.                         <Link to="/register" className="btn btn-link">Register</Link>
  62.                     </div>
  63.                 </form>
  64.             </div>
  65.         );
  66.     }
  67. }
  68.  
  69. function mapStateToProps(state) {
  70.     const { loggingIn } = state.authentication;
  71.     return {
  72.         loggingIn
  73.     };
  74. }
  75.  
  76. export default connect(mapStateToProps)(LoginPage);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement