AllenYuan

auth - login view - handlers

Apr 24th, 2020
760
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. // ...
Advertisement
Add Comment
Please, Sign In to add comment