|
| 1 | +import React, { Component } from "react"; |
| 2 | +import Input from "../../Components/UI/Input/Input"; |
| 3 | +import Button from "../../Components/UI/Button/Button"; |
| 4 | +import classes from './Auth.css'; |
| 5 | +import * as actions from '../../store/actions/index'; |
| 6 | +import {connect} from 'react-redux'; |
| 7 | +import Spinner from '../../Components/UI/Spinner/Spinner'; |
| 8 | +import { Redirect } from 'react-router-dom'; |
| 9 | + |
| 10 | +class Auth extends Component { |
| 11 | + state = { |
| 12 | + controls: { |
| 13 | + email: { |
| 14 | + elementType: "input", |
| 15 | + elementConfig: { |
| 16 | + type: "email", |
| 17 | + placeholder: "Enter Emailaddress", |
| 18 | + }, |
| 19 | + value: "", |
| 20 | + validation: { |
| 21 | + required: true, |
| 22 | + isEmail: true, |
| 23 | + }, |
| 24 | + valid: false, |
| 25 | + touched: false, |
| 26 | + }, |
| 27 | + password: { |
| 28 | + elementType: "input", |
| 29 | + elementConfig: { |
| 30 | + type: "password", |
| 31 | + placeholder: "Enter password", |
| 32 | + }, |
| 33 | + value: "", |
| 34 | + validation: { |
| 35 | + required: true, |
| 36 | + minLength: 6, |
| 37 | + }, |
| 38 | + valid: false, |
| 39 | + touched: false, |
| 40 | + }, |
| 41 | + }, |
| 42 | + isSignUp: true |
| 43 | + }; |
| 44 | + |
| 45 | + componentDidMount(){ |
| 46 | + |
| 47 | + if(!this.props.buildingBurger && this.props.authRedirect!=='/'){ |
| 48 | + this.props.onSetAuthRedirectPath(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + inputChangedHandler = (event, inputIdentifier) => { |
| 53 | + const updatedControls = { |
| 54 | + ...this.state.controls, |
| 55 | + [inputIdentifier]:{ ...this.state.controls[inputIdentifier], |
| 56 | + value: event.target.value, |
| 57 | + valid: this.checkValidity(event.target.value,this.state.controls[inputIdentifier].validation), |
| 58 | + touched: true |
| 59 | + }, |
| 60 | + }; |
| 61 | + this.setState({controls: updatedControls}); |
| 62 | + } |
| 63 | + |
| 64 | + checkValidity(value, rules) { |
| 65 | + let isValid = true; |
| 66 | + |
| 67 | + if (rules.required) { |
| 68 | + isValid = value !== "" && isValid; |
| 69 | + } |
| 70 | + if (rules.minLength) { |
| 71 | + isValid = value.length >= rules.minLength && isValid; |
| 72 | + } |
| 73 | + if (rules.maxLength) { |
| 74 | + isValid = value.length <= rules.maxLength && isValid; |
| 75 | + } |
| 76 | + if(rules.isEmail){ |
| 77 | + const pattern = /[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/; |
| 78 | + isValid = pattern.test(value) && isValid; |
| 79 | + } |
| 80 | + return isValid; |
| 81 | + } |
| 82 | + |
| 83 | + submitHandler =(event) =>{ |
| 84 | + event.preventDefault(); |
| 85 | + this.props.onAuth(this.state.controls.email.value, this.state.controls.password.value, this.state.isSignUp); |
| 86 | + } |
| 87 | + |
| 88 | + switchAuthModeHandler = () =>{ |
| 89 | + this.setState(prevState => { |
| 90 | + return { |
| 91 | + isSignUp : !prevState.isSignUp |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | + render() { |
| 96 | + const formElementsArray = []; |
| 97 | + for (let key in this.state.controls) { |
| 98 | + formElementsArray.push({ id: key, config: this.state.controls[key] }); |
| 99 | + } |
| 100 | + let form = formElementsArray.map((formElement) => ( |
| 101 | + <Input |
| 102 | + key={formElement.id} |
| 103 | + changed={(event) => this.inputChangedHandler(event, formElement.id)} |
| 104 | + elementType={formElement.config.elementType} |
| 105 | + elementConfig={formElement.config.elementConfig} |
| 106 | + validation={formElement.config.validation} |
| 107 | + touched={formElement.config.touched} |
| 108 | + valid={formElement.config.valid} |
| 109 | + value={formElement.config.value} |
| 110 | + /> |
| 111 | + )); |
| 112 | + |
| 113 | + if(this.props.loading){ |
| 114 | + form =<Spinner/>; |
| 115 | + } |
| 116 | + let errorMessage = null; |
| 117 | + if(this.props.error){ |
| 118 | + errorMessage =(<p>{this.props.error.message}</p>) |
| 119 | + } |
| 120 | + let redirect = null; |
| 121 | + if(this.props.isAuthenticated){ |
| 122 | + redirect =<Redirect to={this.props.authRedirectPath}/> |
| 123 | + } |
| 124 | + return ( |
| 125 | + <div className= {classes.Auth}> |
| 126 | + {redirect} |
| 127 | + {errorMessage} |
| 128 | + <form onSubmit={this.submitHandler}> |
| 129 | + {form} |
| 130 | + <Button btnType="Success">{this.state.isSignUp? 'SignUp' :'SignIn'}</Button> |
| 131 | + </form> |
| 132 | + <Button clicked = {this.switchAuthModeHandler} |
| 133 | + btnType="Danger">Swith to {this.state.isSignUp? 'SignIn' :'SignUp'}</Button> |
| 134 | + </div> |
| 135 | + ); |
| 136 | + } |
| 137 | +} |
| 138 | +const mapStateToProps =(state) =>{ |
| 139 | + return{ |
| 140 | + loading: state.auth.loading, |
| 141 | + error: state.auth.error, |
| 142 | + token : state.auth.token, |
| 143 | + isAuthenticated : state.auth.token!==null, |
| 144 | + buildingBurger: state.burgerBuilder.building, |
| 145 | + authRedirectPath: state.auth.authRedirect |
| 146 | + } |
| 147 | +} |
| 148 | +const mapDispatchToProps =(dispatch) =>{ |
| 149 | + return{ |
| 150 | + onAuth : (email,passWord,isSignUp) => dispatch(actions.auth(email,passWord,isSignUp)), |
| 151 | + onSetAuthRedirectPath: () => dispatch(actions.setAuthRedirectPath('/')) |
| 152 | + } |
| 153 | +} |
| 154 | +export default connect(mapStateToProps,mapDispatchToProps)(Auth); |
0 commit comments