Skip to content

Commit 32f05eb

Browse files
author
Andrei Alexandru
committed
2 parents 97bf01c + e3eb22c commit 32f05eb

File tree

5 files changed

+136
-23
lines changed

5 files changed

+136
-23
lines changed

views/src/components/Profile.jsx

Lines changed: 92 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,92 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
3+
import TextField from './TextField';
4+
import axios from 'axios';
35

46
export 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>

views/src/components/TextField.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
class TextField extends React.Component {
5+
render() {
6+
const errorText = this.props.errorText;
7+
return (
8+
<div>
9+
<input className={errorText ? "input-error" : "input-ok"}
10+
name={this.props.name}
11+
type={this.props.type}
12+
defaultValue={this.props.defaultValue}
13+
placeholder={this.props.placeholder}
14+
onChange={e => this.props.onChange(e)} />
15+
<div className="error-form"><span className="error-text-content">{errorText}</span>
16+
</div>
17+
</div>
18+
)
19+
}
20+
}
21+
22+
export default TextField;
23+
TextField.propTypes = {
24+
name:PropTypes.string,
25+
type: PropTypes.string,
26+
defaultValue: PropTypes.string,
27+
placeholder: PropTypes.string,
28+
onChange: PropTypes.func,
29+
errorText: PropTypes.string
30+
31+
}

views/src/reducers/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ const initialState = {
3737
_id: '5b646febeebb915ff8b221be',
3838
name: {
3939
first: 'Jon',
40-
last: 'Doe'
40+
last: 'Doe',
41+
position:'admin',
42+
bio:"sunt frumos",
43+
phone:"071231311",
44+
4145
},
4246
wishlist: [],
4347
wishlistProducts: [],

views/src/scss/components/index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
@import 'profile';
88
@import 'departments';
99
@import 'menu';
10+
@import 'textfield';
1011
@import 'modal';
1112
@import 'notification';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.input-error{
2+
border-bottom:1px;
3+
border-bottom-color:red;
4+
}
5+
.error-text-content{
6+
color:red;
7+
}

0 commit comments

Comments
 (0)