1+ import React , { Component } from 'react'
2+ import { connect } from 'react-redux' ;
3+ import { Redirect } from 'react-router-dom' ;
4+ import Input from '../../components/UI/Input/Input'
5+ import Button from '../../components/UI/Button/Button'
6+ import Spinner from '../../components/UI/Spinner/Spinner'
7+ import classes from './Auth.module.css' ;
8+ import * as actions from '../../store/actions/index' ;
9+ import { updateObject , checkValidity } from '../../shared/utility' ;
10+
11+ class Auth extends Component {
12+ state = {
13+ controls : {
14+ email : {
15+ elementType : 'input' ,
16+ elementConfig : {
17+ type : 'email' ,
18+ placeholder : 'Email Address'
19+ } ,
20+ value : '' ,
21+ validation : {
22+ required : true ,
23+ isEmail : true
24+ } ,
25+ valid : false ,
26+ touched : false
27+ } ,
28+ password : {
29+ elementType : 'input' ,
30+ elementConfig : {
31+ type : 'password' ,
32+ placeholder : 'Password'
33+ } ,
34+ value : '' ,
35+ validation : {
36+ required : true ,
37+ minLength : 6
38+ } ,
39+ valid : false ,
40+ touched : false
41+ }
42+ } ,
43+ isSignup : true
44+ }
45+
46+ componentDidMount ( ) {
47+ if ( ! this . props . buildingBurger && this . props . authRedirectPath !== '/' ) {
48+ this . props . onSetAuthRedirectPath ( ) ;
49+ }
50+ }
51+
52+ inputchangedHandler = ( event , controlName ) => {
53+ console . log ( event . target . value , controlName ) ;
54+ const updateControls = updateObject ( this . state . controls , {
55+ [ controlName ] : updateObject ( this . state . controls [ controlName ] , {
56+ value : event . target . value ,
57+ valid : checkValidity ( event . target . value , this . state . controls [ controlName ] . validation ) ,
58+ touched : true
59+ } )
60+ } ) ;
61+
62+ this . setState ( { controls : updateControls } ) ;
63+ }
64+ submitHandler = ( event ) => {
65+ event . preventDefault ( ) ;
66+ this . props . onAuth ( this . state . controls . email . value , this . state . controls . password . value , this . state . isSignup ) ;
67+ }
68+ swithAuthModeHandler = ( ) => {
69+ this . setState ( prevState => {
70+ return {
71+ isSignup : ! prevState . isSignup
72+ } ;
73+ } ) ;
74+ }
75+ render ( ) {
76+ const formElementsArray = [ ] ;
77+ for ( let key in this . state . controls ) {
78+ formElementsArray . push ( {
79+ id : key ,
80+ config : this . state . controls [ key ]
81+ } ) ;
82+ }
83+ console . log ( 'rendering the auth again' ) ;
84+ let form = formElementsArray . map ( formElement => (
85+ < Input
86+ key = { formElement . id }
87+ elementType = { formElement . config . elementType }
88+ elementConfig = { formElement . config . elementConfig }
89+ value = { formElement . value }
90+ invalid = { ! formElement . config . valid }
91+ shouldValidate = { formElement . config . validation }
92+ touched = { formElement . config . touched }
93+ changed = { ( event ) => this . inputchangedHandler ( event , formElement . id ) } />
94+ ) ) ;
95+
96+ if ( this . props . loading ) {
97+ form = < Spinner />
98+ }
99+
100+ let errorMessage = null ;
101+ if ( this . props . error ) {
102+ errorMessage = ( < p > { this . props . error . message } </ p > ) ;
103+ }
104+ let authRedirect = null ;
105+ if ( this . props . isAuthenticated ) {
106+ authRedirect = < Redirect to = { this . props . authRedirectPath } />
107+ }
108+
109+ return (
110+ < div className = { classes . Auth } >
111+ { authRedirect }
112+ { errorMessage }
113+ < form onSubmit = { this . submitHandler } >
114+ { form }
115+ < Button btnType = "Success" > Submit</ Button >
116+ </ form >
117+ < Button btnType = "Danger" clicked = { this . swithAuthModeHandler } > SWITCH TO { this . state . isSignup ? "SIGNIN" : "SIGNUP" } </ Button >
118+ </ div >
119+ ) ;
120+ }
121+ }
122+
123+ const mapStateToProps = state => {
124+ return {
125+ loading : state . auth . loading ,
126+ error : state . auth . error ,
127+ isAuthenticated : state . auth . token !== null ,
128+ buildingBurger : state . burgerBuilder . building ,
129+ authRedirectPath : state . auth . authRedirectPath
130+
131+ }
132+ } ;
133+ const mapDispatchToProps = dispatch => {
134+ return {
135+ onAuth : ( email , password , isSignup ) => dispatch ( actions . auth ( email , password , isSignup ) ) ,
136+ onSetAuthRedirectPath : ( ) => dispatch ( actions . setAuthRedirectPath ( '/' ) )
137+ }
138+ }
139+ export default connect ( mapStateToProps , mapDispatchToProps ) ( Auth ) ;
0 commit comments