@@ -2,23 +2,26 @@ import React from 'react';
22import PropTypes from 'prop-types' ;
33
44import { APIConsumer } from '../data/api' ;
5+ import { UserConsumer } from '../data/user' ;
56
67class Login extends React . Component {
78 constructor ( props ) {
89 super ( props ) ;
910
1011 this . state = {
11- isLoggedIn : false ,
1212 email : localStorage . getItem ( 'email' ) || '' ,
1313 password : '' ,
14- rememberMe : false
14+ autoFocusPassword : false
1515 } ;
1616
17+ if ( this . state . email !== '' ) {
18+ this . state . autoFocusPassword = true ;
19+ }
20+
1721 this . goBack = this . goBack . bind ( this ) ;
1822 this . handleLogin = this . handleLogin . bind ( this ) ;
1923 this . handleEmail = this . handleEmail . bind ( this ) ;
2024 this . handlePassword = this . handlePassword . bind ( this ) ;
21- this . handleRememberMe = this . handleRememberMe . bind ( this ) ;
2225 }
2326
2427 goBack ( ) {
@@ -27,15 +30,26 @@ class Login extends React.Component {
2730 // this.props.location.history.push(dest);
2831 }
2932
30- handleLogin ( e , api ) {
33+ handleLogin ( e , api , done ) {
3134 e . preventDefault ( ) ;
3235
3336 localStorage . setItem ( 'email' , this . state . email ) ;
3437
35- console . log ( api ) ;
36-
37- // TODO: actually hit API to login, then call this function
38- this . props . userLogin ( { email : this . state . email } ) ;
38+ api . post ( {
39+ url : '/login' ,
40+ body : {
41+ email : this . state . email ,
42+ password : this . state . password
43+ }
44+ } , ( body ) => {
45+ if ( body . success ) {
46+ return done ( { email : this . state . email } ) ;
47+ }
48+
49+ alert ( 'Bad email / password.' ) ;
50+ } , ( err ) => {
51+ alert ( err . errorMessages ) ;
52+ } ) ;
3953
4054 return false ;
4155 }
@@ -48,53 +62,77 @@ class Login extends React.Component {
4862 this . setState ( { password : e . target . value } ) ;
4963 }
5064
51- handleRememberMe ( ) {
52- this . setState ( ( current ) => ( { rememberMe : ! current . rememberMe } ) ) ;
53- }
54-
5565 render ( ) {
5666 return (
57- < div id = "main-wrapper" className = "container" >
58- < h2 > Admin</ h2 >
67+ < UserConsumer >
5968 {
60- this . state . isLoggedIn &&
61- < div > You are logged in </ div >
62- }
63- {
64- ! this . state . isLoggedIn &&
65- < div className = "row justify-content-center" >
66- < APIConsumer >
69+ ( userContext ) => (
70+ < div id = "main-wrapper" className = "container" >
71+ < h2 > Admin </ h2 >
72+ {
73+ userContext . isLoggedIn &&
74+ < div > You are logged in </ div >
75+ }
6776 {
68- ( api ) => (
69- < form onSubmit = { ( e ) => this . handleLogin ( e , api ) } className = "col-3" >
70- < h3 className = "row" > Login</ h3 >
71- < div className = "row pb-2" >
72- < input type = "email" className = "form-control" placeholder = "Email" value = { this . state . email } onChange = { this . handleEmail } />
73- </ div >
74- < div className = "row pb-4" >
75- < input type = "password" className = "form-control" placeholder = "Password" value = { this . state . password } onChange = { this . handlePassword } />
76- </ div >
77- < div className = "row pb-4 form-check" >
78- < input type = "checkbox" className = "form-check-input" id = "rememberMe" value = { this . state . rememberMe } onChange = { this . handleRememberMe } />
79- < label htmlFor = "rememberMe" > Remember Me</ label >
80- </ div >
81- < div className = "row" >
82- < button type = "submit" className = "btn btn-primary" > Login</ button >
83- </ div >
84- </ form >
85- )
77+ ! userContext . isLoggedIn &&
78+ < div className = "row justify-content-center" >
79+ < APIConsumer >
80+ {
81+ ( api ) => (
82+
83+ < form onSubmit = { ( e ) => this . handleLogin ( e , api , userContext . login ) } className = "col-3" >
84+ < h3 className = "row" > Login</ h3 >
85+ < div className = "row pb-2" >
86+ < input
87+ type = "email"
88+ className = "form-control"
89+ placeholder = "Email"
90+ value = { this . state . email }
91+ name = "email"
92+ onChange = { this . handleEmail }
93+ autoFocus = { ! this . state . autoFocusPassword }
94+ />
95+ </ div >
96+ < div className = "row pb-4" >
97+ < input
98+ type = "password"
99+ className = "form-control"
100+ placeholder = "Password"
101+ name = "password"
102+ value = { this . state . password }
103+ onChange = { this . handlePassword }
104+ autoFocus = { this . state . autoFocusPassword }
105+ />
106+ </ div >
107+ < div className = "row pb-4 form-check" >
108+ < input
109+ type = "checkbox"
110+ className = "form-check-input"
111+ id = "rememberMe"
112+ defaultChecked = { userContext . isRememberMeOn }
113+ onChange = { ( ) => userContext . setRememberMe ( ! userContext . isRememberMeOn ) }
114+ />
115+ < label htmlFor = "rememberMe" > Remember Me</ label >
116+ </ div >
117+ < div className = "row" >
118+ < button type = "submit" className = "btn btn-primary" > Login</ button >
119+ </ div >
120+ </ form >
121+ )
122+ }
123+ </ APIConsumer >
124+ </ div >
86125 }
87- </ APIConsumer >
88- </ div >
126+ </ div >
127+ )
89128 }
90- </ div >
129+ </ UserConsumer >
91130 ) ;
92131 }
93132}
94133
95134Login . propTypes = {
96- location : PropTypes . object . isRequired ,
97- userLogin : PropTypes . func . isRequired
135+ // userLogin: PropTypes.func.isRequired
98136} ;
99137
100138Login . defaultProps = { } ;
0 commit comments