Skip to content
This repository was archived by the owner on Oct 6, 2024. It is now read-only.

Commit e2a8bde

Browse files
committed
Fix interaction between Profile and App.js
1 parent 1b9169e commit e2a8bde

File tree

3 files changed

+55
-54
lines changed

3 files changed

+55
-54
lines changed

src/App.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,7 @@ export default class App extends Component {
2828
});
2929
}
3030

31-
componentDidUpdate() {
32-
this.loadPosts();
33-
}
34-
35-
loadPosts() {
36-
this.userService.getCurrentUser().then(user => {
37-
if (user) this.setState({user}
38-
)
39-
})
40-
}
31+
setUser = user => this.setState({user});
4132

4233
render() {
4334
return (
@@ -68,7 +59,7 @@ loadPosts() {
6859
render={({location, history}) =>
6960
<Login history={history}
7061
location={location}
71-
setUser={user => this.setState({user})}/>}/>
62+
setUser={this.setUser}/>}/>
7263
<Route path="/logout"
7364
exact
7465
render={({history}) => {
@@ -83,7 +74,7 @@ loadPosts() {
8374
component={Admin}/>
8475
<Route
8576
path="/profile"
86-
component={Profile}/>
77+
render={({history}) => <Profile history={history} setUser={this.setUser}/>}/>
8778
</div>
8879
</div>
8980
</Router>

src/components/Profile.js

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import React from "react";
2-
import { SelectUSState } from "react-select-us-states";
2+
import {SelectUSState} from "react-select-us-states";
33
import UserService from "../services/UserService";
4+
45
class 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>)

src/services/UserService.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,18 @@ export default class UserService {
7373
} else {
7474
throw new HttpError(response);
7575
}
76-
}).then(response => {
76+
}).then(response => {
7777
return response.json();
78-
}).catch(error => {
78+
}).catch(error => {
7979
return {};
8080
});
8181
}
8282

83-
updateCurrentUser(user, id) {
84-
return fetch(this.urlPrefix + `/api/users/${id}`,
83+
updateUser(user) {
84+
return fetch(this.urlPrefix + `/api/users/${user.id}`,
8585
{
8686
body: JSON.stringify(user),
8787
method: "PUT",
88-
credentials: "include",
8988
headers: {
9089
"Content-Type": "application/json"
9190
}

0 commit comments

Comments
 (0)