Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
113
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 { Redirect } from 'react-router-dom';
  4.  
  5. import classes from './Auth.css';
  6.  
  7. import Input from '../../components/UI/Input/Input';
  8. import Button from '../../components/UI/Button/Button';
  9. import Spinner from '../../components/UI/Spinner/Spinner';
  10.  
  11. import * as actions from '../../store/actions/';
  12.  
  13. class Auth extends Component {
  14.  
  15.   state = {
  16.     controls: {
  17.       email: {
  18.         elementType: 'input',
  19.         elementConfig: {
  20.           type: 'email',
  21.           placeholder: 'your email'
  22.         },
  23.         value: '',
  24.         validation: {
  25.           required: true,
  26.           isEmail: true
  27.         },
  28.         valid: false,
  29.         touched: false
  30.       },
  31.       password: {
  32.         elementType: 'input',
  33.         elementConfig: {
  34.           type: 'password',
  35.           placeholder: 'Password'
  36.         },
  37.         value: '',
  38.         validation: {
  39.           required: true,
  40.           minLength: 7
  41.         },
  42.         valid: false,
  43.         touched: false
  44.       }
  45.     },
  46.     isSignUp: true,
  47.   };
  48.  
  49.   inputChangedHandler = (event, controlName) => {
  50.     const updateControls = {
  51.       ...this.state.controls,
  52.       [controlName]: {
  53.         ...this.state.controls[controlName],
  54.         value: event.target.value,
  55.         valid: this.checkValidity(event.target.value, this.state.controls[controlName].validation),
  56.         touched: true
  57.       }
  58.     };
  59.     this.setState({ controls: updateControls });
  60.   };
  61.  
  62.   switchAuthModeHandler = () => {
  63.     this.setState(prevState => {
  64.       return { isSignUp: !prevState.isSignUp }
  65.     })
  66.   };
  67.  
  68.   componentDidMount() {
  69.     // When we want to redirect to checkout when not creating a burger
  70.     if (!this.props.buildingBurger && this.props.authRedirectPath !== '/') {
  71.       this.props.onSetAuthRedirectPath();
  72.     }
  73.   }
  74.  
  75.   checkValidity(value, rules) {
  76.     let isValid = true;
  77.     if (rules.required) {
  78.       isValid = value.trim() !== '' && isValid;
  79.     }
  80.  
  81.     if (rules.minLength) {
  82.       isValid = value.length >= rules.minLength && isValid;
  83.     }
  84.  
  85.     if (rules.maxLength) {
  86.       isValid = value.length <= rules.maxLength && isValid;
  87.     }
  88.  
  89.     if (rules.isEmail) {
  90.       const emailRule = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  91.       isValid = emailRule.test(value) && isValid;
  92.     }
  93.     return isValid;
  94.   }
  95.  
  96.   submitHandler = (event) => {
  97.     event.preventDefault();
  98.  
  99.     this.props.onAuth(
  100.       this.state.controls.email.value,
  101.       this.state.controls.password.value,
  102.       this.state.isSignUp
  103.     );
  104.   };
  105.  
  106.   render() {
  107.     const formElementArray = [];
  108.     for (let key in this.state.controls) {
  109.       formElementArray.push({
  110.         id: key,
  111.         config: this.state.controls[key]
  112.       })
  113.     }
  114.  
  115.     let form = formElementArray.map(formElement => (
  116.       <Input
  117.         changed={(event) => this.inputChangedHandler(event, formElement.id)}
  118.         key={formElement.id}
  119.         value={formElement.config.value}
  120.         elementType={formElement.config.elementType}
  121.         invalid={!formElement.config.valid}
  122.         shouldValidate={formElement.config.validation}
  123.         touched={formElement.config.touched}
  124.         errorMessage={form}
  125.         elementConfig={formElement.config.elementConfig}/>
  126.     ));
  127.  
  128.     if (this.props.loading) {
  129.       form = <Spinner/>
  130.     }
  131.  
  132.     let errorMessage = null;
  133.  
  134.     // Use message coming from Firebase
  135.     if (this.props.error) {
  136.       errorMessage = (
  137.         <p>{this.props.error.message}</p>
  138.       );
  139.     }
  140.  
  141.     // Redirect away from the form when user is signed in
  142.     let authRedirect = null;
  143.     if (this.props.isAuthenticated) {
  144.       authRedirect = <Redirect to={this.props.authRedirectPath}/>
  145.     }
  146.  
  147.     return (
  148.       <div className={classes.Auth}>
  149.         {authRedirect}
  150.         {errorMessage}
  151.         <form onSubmit={this.submitHandler}>
  152.           {form}
  153.           <Button btnType="Success">Submit</Button>
  154.         </form>
  155.  
  156.         <Button
  157.           clicked={this.switchAuthModeHandler}
  158.           btnType="Danger">
  159.           {this.state.isSignUp ? 'Let\'s SIGN IN' : 'Let\'s SIGN UP'}
  160.         </Button>
  161.       </div>
  162.     );
  163.   }
  164. }
  165.  
  166. // data props from Reducers
  167. const mapStateToProps = state => {
  168.   return {
  169.     loading: state.auth.loading,
  170.     error: state.auth.error,
  171.     isAuthenticated: state.auth.token !== null,
  172.     buildingBurger: state.burgerBuilder.building,
  173.     authRedirectPath: state.auth.authRedirectPath
  174.   }
  175. };
  176.  
  177. const mapDispatchToProps = dispatch => {
  178.   return {
  179.     onAuth: (email, password, isSignup) => dispatch(actions.auth(email, password, isSignup)),
  180.     onSetAuthRedirectPath: () => dispatch(actions.setAuthRedirectPath('/'))
  181.   }
  182. };
  183.  
  184. export default connect(mapStateToProps, mapDispatchToProps)(Auth);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement