Advertisement
Guest User

Untitled

a guest
Apr 11th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import { connect } from "react-redux";
  3. import classnames from "classnames";
  4. import PropTypes from "prop-types";
  5. import { loginUser } from "../../store/actions/authActions";
  6.  
  7. class Login extends Component {
  8.   state = {
  9.     password: "",
  10.     email: "",
  11.     errors: {}
  12.   };
  13.  
  14. componentDidMount(){
  15.   if(this.props.auth.isAuth){
  16.     this.props.history.push('/dashboard')
  17.   }
  18. }
  19.  
  20.  
  21. static getDerivedStateFromProps(nextProps, prevState) {
  22.     if (nextProps.auth.isAuth) {
  23.       nextProps.history.push("/dashboard");
  24.     }
  25.    
  26.       if (nextProps.errors) {
  27.         return {
  28.           errors: nextProps.errors
  29.         };
  30.       }
  31.     return null
  32.    
  33.   }
  34.  
  35. // UNSAFE_componentWillReceiveProps(nextProps){
  36. //   if (nextProps.auth.isAuth) {
  37. //           nextProps.history.push("/dashboard");
  38. //         }
  39. //   if (nextProps.errors) {
  40.            
  41. //        this.setState({errors: nextProps.errors})
  42.            
  43. //   }
  44. // }
  45.  
  46.  
  47.  
  48.  
  49.   onInputChange = e => {
  50.     this.setState({ [e.target.name]: e.target.value });
  51.   };
  52.  
  53.   onSubmitHandler = e => {
  54.     e.preventDefault();
  55.     const user = {
  56.       email: this.state.email,
  57.       password: this.state.password
  58.     };
  59.  
  60.     this.props.loginUser(user, this.props.history);
  61.   };
  62.  
  63.   render() {
  64.     const { errors } = this.state;
  65.     return (
  66.       <div className="login">
  67.         <div className="container">
  68.           <div className="row">
  69.             <div className="col-md-8 m-auto">
  70.               <h1 className="display-4 text-center">Log In</h1>
  71.               <p className="lead text-center">
  72.                 Sign in to your DevConnector account
  73.               </p>
  74.               <form onSubmit={this.onSubmitHandler}>
  75.                 <div className="form-group">
  76.                   <input
  77.                     type="email"
  78.                     className={classnames("form-control form-control-lg", {
  79.                       "is-invalid": errors.email
  80.                     })}
  81.                     placeholder="E-Mail Address"
  82.                     name="email"
  83.                     value={this.state.email}
  84.                     onChange={this.onInputChange}
  85.                   />
  86.                   {errors.email && (
  87.                     <div className="invalid-feedback">{errors.email}</div>
  88.                   )}
  89.                 </div>
  90.                 <div className="form-group">
  91.                   <input
  92.                     type="password"
  93.                     className={classnames("form-control form-control-lg", {
  94.                       "is-invalid": errors.password
  95.                     })}
  96.                     placeholder="Password"
  97.                     name="password"
  98.                     value={this.state.password}
  99.                     onChange={this.onInputChange}
  100.                   />
  101.                   {errors.password && (
  102.                     <div className="invalid-feedback">{errors.password}</div>
  103.                   )}
  104.                 </div>
  105.                 <input type="submit" className="btn btn-info btn-block mt-4" />
  106.               </form>
  107.             </div>
  108.           </div>
  109.         </div>
  110.       </div>
  111.     );
  112.   }
  113. }
  114.  
  115. Login.propTypes = {
  116.   auth: PropTypes.object.isRequired,
  117.   loginUser: PropTypes.func.isRequired,
  118.   errors: PropTypes.object.isRequired
  119. };
  120.  
  121. const mapStateToProps = state => {
  122.   return {
  123.     auth: state.auth,
  124.     errors: state.errors
  125.   };
  126. };
  127.  
  128. export default connect(
  129.   mapStateToProps,
  130.   { loginUser }
  131. )(Login);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement