11import React from "react" ;
2- import { SelectUSState } from "react-select-us-states" ;
2+ import { SelectUSState } from "react-select-us-states" ;
33import UserService from "../services/UserService" ;
4+
45class Profile extends React . Component {
56 constructor ( props ) {
67 super ( props ) ;
78 this . userService = UserService . getInstance ( ) ;
89 this . state = {
910 currentUser : {
10- // DIRECTLY GET USER INFO
11+ // DIRECTLY GET USER INFO
1112 firstName : "" ,
1213 id : 0 ,
1314 lastName : "" ,
@@ -19,36 +20,33 @@ class Profile extends React.Component {
1920 email : ""
2021 }
2122 } ;
22- // console.log(this.userService.getCurrentUser())
23-
24- this . userService . getCurrentUser ( ) . then ( user => console . log ( String ( user . id ) ) )
2523
26- this . setNewValue = this . setNewValue . bind ( this ) ;
2724 this . logOut = this . logOut . bind ( this ) ;
2825 this . handleInputChange = this . handleInputChange . bind ( this ) ;
2926 this . updateUser = this . updateUser . bind ( this ) ;
3027 }
3128
3229 componentDidMount ( ) {
33- this . userService . getCurrentUser ( ) . then ( user => this . setState ( {
34- currentUser : {
35- id : String ( user . id ) ,
36- firstName : user . firstName ,
37- lastName : user . lastName ,
38- zipCode : user . zipCode ,
39- email : user . email
30+ this . userService . getCurrentUser ( ) . then ( user => {
31+ if ( Object . entries ( user ) . length === 0 && user . constructor === Object ) {
32+ this . props . history . push ( "/login" ) ;
4033 }
41- }
42- ) )
43- }
44-
45- setNewValue ( newValue ) {
46- console . log ( 'this is the State code:' + newValue ) ;
34+ this . setState ( {
35+ currentUser : {
36+ id : String ( user . id ) ,
37+ firstName : user . firstName ,
38+ lastName : user . lastName ,
39+ zipCode : user . zipCode ,
40+ email : user . email
41+ }
42+ }
43+ )
44+ } )
4745 }
4846
4947 logOut ( ) {
50- this . setState ( { user : { email : null } } ) ;
51- this . props . history . push ( { pathname : "/logout" } ) ;
48+ this . setState ( { user : { email : null } } ) ;
49+ this . props . history . push ( { pathname : "/logout" } ) ;
5250 }
5351
5452 handleInputChange ( event ) {
@@ -67,42 +65,55 @@ class Profile extends React.Component {
6765 }
6866
6967 updateUser ( ) {
70- this . userService . updateCurrentUser ( this . state . currentUser , this . state . currentUser . id )
71- this . props . history . push ( { pathname : "./home" } ) ;
68+ this . userService . updateUser ( this . state . currentUser ) . then ( user => {
69+ this . props . setUser ( user ) ;
70+ this . setState ( {
71+ currentUser : {
72+ id : String ( user . id ) ,
73+ firstName : user . firstName ,
74+ lastName : user . lastName ,
75+ zipCode : user . zipCode ,
76+ email : user . email
77+ }
78+ }
79+ ) ;
80+ this . props . history . push ( "/home" ) ;
81+ } )
82+
7283 }
7384
7485 render ( ) {
7586 return (
7687 < div >
77- < div className = "box2" > < h1 > Profile</ h1 > </ div >
88+ < div className = "box2" > < h1 > Profile</ h1 > </ div >
7889 { /* <Form onSubmit = {this.updateUser}> */ }
7990 < div className = "row" >
80- < div className = "column" >
91+ < div className = "column" >
8192 < h2 > Legal Name</ h2 >
8293 < hr > </ hr >
8394 < div >
8495 < h6 > First Name</ h6 >
85- < input name = "firstName" value = { this . state . currentUser . firstName } onChange = { this . handleInputChange } />
96+ < input name = "firstName" value = { this . state . currentUser . firstName } onChange = { this . handleInputChange } />
8697 < h6 > Last Name</ h6 >
87- < input name = "lastName" value = { this . state . currentUser . lastName } onChange = { this . handleInputChange } />
98+ < input name = "lastName" value = { this . state . currentUser . lastName } onChange = { this . handleInputChange } />
8899 </ div >
89100 < hr > </ hr >
90101 </ div >
91- < div className = "column" >
102+ < div className = "column" >
92103 < h2 > Date of Birth</ h2 >
93104 < hr > </ hr >
94105 < h6 > Choose your Birthday</ h6 >
95- < input type = "month" name = "bd" value = { this . state . currentUser . DOB_MONTH } onChange = { this . handleInputChange } />
106+ < input type = "month" name = "bd" value = { this . state . currentUser . DOB_MONTH } onChange = { this . handleInputChange } />
96107 </ div >
97- < div className = "column" >
108+ < div className = "column" >
98109 < h2 > Location </ h2 >
99110 < hr > </ hr >
100111 < h6 > Street</ h6 >
101- < input type = "text" name = "street" value = { this . state . currentUser . Street } onChange = { this . handleInputChange } />
112+ < input type = "text" name = "street" value = { this . state . currentUser . Street } onChange = { this . handleInputChange } />
102113 < h6 > City</ h6 >
103- < input type = "text" name = "city" value = { this . state . currentUser . City } onChange = { this . handleInputChange } />
114+ < input type = "text" name = "city" value = { this . state . currentUser . City } onChange = { this . handleInputChange } />
104115 < h6 > State</ h6 >
105- < p >
116+ < p >
106117 < select name = "state" value = { this . state . currentUser . State } onChange = { this . handleInputChange } >
107118 < option value = "AL" > Alabama</ option >
108119 < option value = "AK" > Alaska</ option >
@@ -158,14 +169,14 @@ class Profile extends React.Component {
158169 </ select >
159170 </ p >
160171 < h6 > Zip</ h6 >
161- < input type = "text" name = "zip" value = { this . state . currentUser . zipCode } onChange = { this . handleInputChange } />
172+ < input type = "text" name = "zip" value = { this . state . currentUser . zipCode } onChange = { this . handleInputChange } />
162173 < h6 > Email</ h6 >
163- < input type = "text" name = "email" value = { this . state . currentUser . email } disabled style = { { background : "yellow" } } />
164- < br />
174+ < input type = "text" name = "email" value = { this . state . currentUser . email } readOnly = { true } />
175+ < br />
165176 </ div >
166177 </ div >
167178 < hr > </ hr >
168- < button type = "button" className = "button2" onClick = { this . updateUser } > Update </ button >
179+ < button type = "button" className = "button2" onClick = { this . updateUser } > Update</ button >
169180 < button type = "submit" className = "button2" onClick = { this . logOut } > Log out</ button >
170181 { /* </Formik> */ }
171182 </ div > )
0 commit comments