AllenYuan

auth - login view

Apr 24th, 2020
1,447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import ListErrors from './ListErrors';
  4. import agent from '../agent';
  5.  
  6. // extract data from auth reducer
  7. const mapStateToProps = state => ({ ...state.auth });
  8.  
  9. const mapDispatchToProps = dispatch => ({
  10.   // update form values => reducer puts values into state => redux puts state into props and we re-render
  11.   onChangeEmail: value =>
  12.     dispatch({ type: 'UPDATE_FIELD_AUTH', key: 'email', value }),
  13.   onChangePassword: value =>
  14.     dispatch({ type: 'UPDATE_FIELD_AUTH', key: 'password', value }),
  15.   // action with login request
  16.   onSubmit: (email, password) =>
  17.     dispatch({ type: 'LOGIN', payload: agent.Auth.login(email, password) })
  18. });
  19.  
  20. class Login extends React.Component {
  21.   constructor() {
  22.     super();
  23.     this.changeEmail = event => this.props.onChangeEmail(event.target.value);
  24.     this.changePassword = event => this.props.onChangePassword(event.target.value);
  25.     this.submitForm = (email, password) => event => {
  26.       event.preventDefault();
  27.       this.props.onSubmit(email, password);
  28.     };
  29.   }
  30.  
  31.   render() {
  32.     const { email, password } = this.props;
  33.     return (
  34.       <div className="auth-page">
  35.         {/* fixed width responsive container */}
  36.         <div className="container page">
  37.           {/* row to wrap column */}
  38.           <div className="row">
  39.             {/* add bootstrap column classes to align form elements and
  40.             use offset to center the column */}
  41.             <div className="col-md-6 offset-md-3 col-xs-12">
  42.               <h1 className="text-xs-center">Sign In</h1>
  43.               <p className="text-xs-center">
  44.                 <a>
  45.                   Need an account?
  46.                 </a>
  47.               </p>
  48.  
  49.               <ListErrors errors={this.props.errors} />
  50.               {/* login form */}
  51.               <form> onSubmit={this.submitForm(email, password)}>
  52.                 <fieldset>
  53.                   {/* email input */}
  54.                   <fieldset className="form-group">
  55.                     {/* on change update email in state */}
  56.                     <input
  57.                       className="form-control form-control-lg"
  58.                       type="email"
  59.                       placeholder="Email"
  60.                       value={email}
  61.                       onChange={this.changeEmail} />
  62.                   </fieldset>
  63.                   {/* on change update password in state */}
  64.                   <fieldset className="form-group">
  65.                     <input
  66.                       className="form-control form-control-lg"
  67.                       type="password"
  68.                       placeholder="Password"
  69.                       value={password}
  70.                       onChange={this.changePassword} />
  71.                   </fieldset>
  72.                   {/* submit button */}
  73.                   <button
  74.                     className="btn btn-lg btn-primary pull-xs-right"
  75.                     type="submit"
  76.                     disabled={this.props.inProgress}>
  77.                     Sign in
  78.                   </button>
  79.                 </fieldset>
  80.               </form>
  81.             </div>
  82.           </div>
  83.         </div>
  84.       </div>
  85.     );
  86.   }
  87. }
  88.  
  89. export default connect(mapStateToProps, mapDispatchToProps)(Login);
Advertisement
Add Comment
Please, Sign In to add comment