11import React , { Component } from 'react' ;
22import PropTypes from 'prop-types' ;
3+ import TextField from './TextField' ;
4+ import axios from 'axios' ;
35
46export default class Profile extends Component {
57 constructor ( ) {
68 super ( ) ;
79 this . state = {
810 firstName : '' ,
11+ firstNameError : '' ,
912 lastName : '' ,
13+ lastNameError : '' ,
1014 jobTitle : '' ,
15+ jobTitleError : '' ,
1116 email : '' ,
17+ emailError : '' ,
1218 phoneNo : '' ,
19+ phoneNoError : '' ,
1320 shortBio : '' ,
21+ shortBioError : '' ,
1422 }
1523 }
1624 change ( e ) {
17- // this.props.onChange({ [e.target.name]: e.target.value });
25+ const name = e . target . name ;
26+ this . validate ( name ) ;
27+
1828 this . setState ( {
1929 [ e . target . name ] : e . target . value
2030 } ) ;
31+
32+ }
33+
34+ validate ( name ) {
35+ let isError = false ;
36+ let errors = { } ;
37+ if ( ( name === "phoneNo" || name === "submit" ) && this . state . phoneNo . length < 5 ) {
38+ isError = true ;
39+ errors . phoneNoError = "Phone number must be at least 5 numbers long" ;
40+ }
41+ else if ( name === "phoneNo" ) {
42+ isError = false ;
43+ errors . phoneNoError = null ;
44+ }
45+
46+
47+ if ( ( name === "email" || name === "submit" ) && this . state . email . indexOf ( "@" ) === - 1 ) {
48+ isError = true ;
49+ errors . emailError = "Requires valid email" ;
50+ }
51+ else if ( name === "email" ) {
52+ isError = false ;
53+ errors . emailError = null ;
54+ }
55+ this . setState ( {
56+ ...this . state ,
57+ ...errors
58+ } )
59+
60+
61+ return isError ;
2162 }
2263
2364 onSubmit ( e ) {
2465 e . preventDefault ( ) ;
2566 console . log ( this . state ) ;
26-
67+ const err = this . validate ( ) ;
68+ console . log ( "eroare:" , err ) ;
69+ if ( ! err ) {
70+ axios . put ( `${ this . props . serverUrl } users/5b646febeebb915ff8b221be` ,
71+ {
72+ name :{
73+ first : this . state . firstName ,
74+ last :this . state . lastName } ,
75+ position :this . state . jobTitle ,
76+ email :this . state . email ,
77+ phone :this . state . phoneNo ,
78+ bio :this . state . shortBio
79+ }
80+ )
81+ this . setState ( {
82+ firstName : '' ,
83+ lastName : '' ,
84+ jobTitle : '' ,
85+ email : '' ,
86+ phoneNo : '' ,
87+ shortBio : ''
88+ } ) ;
89+ }
2790 }
2891
2992 render ( ) {
@@ -43,14 +106,14 @@ export default class Profile extends Component {
43106 < img src = "https://www.w3schools.com/w3images/avatar2.png" className = "profile-page-image-content" alt = "Avatar" />
44107 </ div >
45108 < div className = "profile-page-title" >
46- < h3 > { this . state . firstName + " " + this . state . lastName } </ h3 >
47- < h4 > { this . state . jobTitle } </ h4 >
109+ < h3 > { this . props . user . name . first + " " + this . props . user . name . last } </ h3 >
110+ < h4 > { this . props . user . name . position } </ h4 >
48111 </ div >
49112 < div className = "profile-page-information" >
50113 < ul className = "profile-page-information-list" >
51- < li > < p > < img src = "https://cdn3.iconfinder.com/data/icons/pyconic-icons-1-2/512/phone-call-active-512.png" className = "icon-image" /> { this . state . phoneNo } </ p > </ li >
52- < li > < p > < img src = "https://cdn1.iconfinder.com/data/icons/education-set-01/512/email-open-512.png" className = "icon-image" /> { this . state . email } </ p > </ li >
53- < li > < p > < img src = "https://cdn1.iconfinder.com/data/icons/social-messaging-productivity-1-1/128/gender-male2-512.png" className = "icon-image" /> { this . state . shortBio } </ p > </ li >
114+ < li > < p > < img src = "https://cdn3.iconfinder.com/data/icons/pyconic-icons-1-2/512/phone-call-active-512.png" className = "icon-image" /> { this . props . user . phone } </ p > </ li >
115+ < li > < p > < img src = "https://cdn1.iconfinder.com/data/icons/education-set-01/512/email-open-512.png" className = "icon-image" /> { this . props . user . email } </ p > </ li >
116+ < li > < p > < img src = "https://cdn1.iconfinder.com/data/icons/social-messaging-productivity-1-1/128/gender-male2-512.png" className = "icon-image" /> { this . props . user . bio } </ p > </ li >
54117 < div className = "custom-button" onClick = { this . handleEditProfile } > Edit Profile
55118 </ div >
56119 </ ul >
@@ -65,26 +128,33 @@ export default class Profile extends Component {
65128
66129 < div className = "form-content" >
67130
68- < p > First name: < input name = "firstName" placeholder = "First name"
69- value = { this . state . firstName }
70- onChange = { e => this . change ( e ) } /> </ p >
71- < p > Last name: < input name = "lastName" placeholder = "Last name"
72- value = { this . state . lastName }
73- onChange = { e => this . change ( e ) } /> </ p >
74- < p > Position: < input name = "jobTitle" placeholder = "Position"
75- onChange = { e => this . change ( e ) } /> </ p >
76- < p > Phone number: < input name = "phoneNo" placeholder = "Phone number"
77- onChange = { e => this . change ( e ) } /> </ p >
78- < p > Email:< input name = "email" placeholder = "Email"
79- onChange = { e => this . change ( e ) } /> </ p >
80- < p > Short bio:< input name = "shortBio" placeholder = "Short bio"
81- onChange = { e => this . change ( e ) } /> </ p >
131+ < div > First name: < TextField name = "firstName" placeholder = "First name"
132+ errorText = { this . state . firstNameError }
133+ defaultValue = { this . state . firstName }
134+ onChange = { e => this . change ( e ) } /> </ div >
135+ < div > Last name: < TextField name = "lastName" placeholder = "Last name"
136+ errorText = { this . state . lastNameError }
137+
138+ onChange = { e => this . change ( e ) } /> </ div >
139+ < div > Position: < TextField name = "jobTitle" placeholder = "Position"
140+
141+ errorText = { this . state . jobTitleError }
142+ onChange = { e => this . change ( e ) } /> </ div >
143+ < div > Phone number: < TextField name = "phoneNo" placeholder = "Phone number"
144+ errorText = { this . state . phoneNoError }
145+ onChange = { e => this . change ( e ) } /> </ div >
146+ < div > Email:< TextField name = "email" placeholder = "Email"
147+ errorText = { this . state . emailError }
148+ onChange = { e => this . change ( e ) } /> </ div >
149+ < div > Short bio:< TextField name = "shortBio" placeholder = "Short bio"
150+ errorText = { this . state . shortBioError }
151+ onChange = { e => this . change ( e ) } /> </ div >
82152
83153
84154
85155 </ div >
86156 { /* <input type="submit" className="action" /> */ }
87- < button className = "custom-button" onClick = { e => this . onSubmit ( e ) } > Submit</ button >
157+ < button name = "submit" className = "custom-button" onClick = { e => this . onSubmit ( e ) } > Submit</ button >
88158 </ form >
89159 </ div >
90160 </ div >
0 commit comments