Skip to content

Commit d0f0afe

Browse files
Implemented user profile screen
1 parent d606cab commit d0f0afe

File tree

6 files changed

+163
-5
lines changed

6 files changed

+163
-5
lines changed

src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Registration from "./pages/registration/Registration";
66
import NavBar from "./components/layout/NavBar";
77
import NewPost from "./pages/posts/NewPost";
88
import NotFound from "./pages/error/NotFound";
9+
import UserProfile from "./pages/profile/UserProfile";
910

1011
const App = () => (
1112
<div className="App">
@@ -14,6 +15,7 @@ const App = () => (
1415
<Switch>
1516
<Route path="/login" component={Login}/>
1617
<Route path="/registration" component={Registration}/>
18+
<Route path="/profile" component={UserProfile}/>
1719
<Route path="/posts/new" component={NewPost}/>
1820

1921
<Route path="/posts" component={Posts} />

src/components/layout/NavBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const NavBar = () => {
2323
<li className="nav-item dropdown">
2424
<NavLink className="nav-link dropdown-toggle" to="#" id="navbarDropdown" role="button"
2525
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
26-
<i className="fas fa-user"/> <span>{user.name}</span>
26+
<i className="fas fa-user"/> <span>{user.user.name}</span>
2727
</NavLink>
2828
<div className="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
2929
<NavLink className="dropdown-item" to="/profile">

src/pages/profile/UserProfile.tsx

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import React, {useEffect, useState} from "react";
2+
import { useHistory } from "react-router-dom";
3+
import {InputText} from "primereact/inputtext";
4+
import AuthService from "../../services/AuthService";
5+
import UserService, {UpdateUserModel} from "../../services/UserService";
6+
7+
const UserProfile = () => {
8+
const authService = new AuthService();
9+
const userService = new UserService();
10+
const history = useHistory();
11+
12+
const [id, setId] = useState(-1);
13+
const [name, setName] = useState("");
14+
const [bio, setBio] = useState("");
15+
const [loc, setLoc] = useState("");
16+
const [githubUsername, setGithubUsername] = useState("");
17+
const [twitterUsername, setTwitterUsername] = useState("");
18+
19+
const getCurrentUserProfile = () => {
20+
console.log("authService.getCurrentUser():", authService.getCurrentUser())
21+
let userId = authService.getCurrentUser().user.id;
22+
userService.getUserProfile(userId).then(response => {
23+
const userProfile = response.data;
24+
console.log("userProfile:", userProfile)
25+
setId(userProfile.id)
26+
setName(userProfile.name)
27+
setBio(userProfile.bio || "")
28+
setLoc(userProfile.location || "")
29+
setGithubUsername(userProfile.githubUsername || "")
30+
setTwitterUsername(userProfile.twitterUsername || "")
31+
});
32+
}
33+
34+
useEffect(()=> {
35+
getCurrentUserProfile();
36+
}, []);
37+
38+
const handleProfileUpdate = (e: React.FormEvent) => {
39+
e.preventDefault();
40+
if (!name.trim()) {
41+
return;
42+
}
43+
const userProfile : UpdateUserModel = {
44+
id: id,
45+
name: name,
46+
bio: bio,
47+
location: loc,
48+
githubUsername: githubUsername,
49+
twitterUsername: twitterUsername
50+
}
51+
userService.updateUserProfile(userProfile)
52+
.then((response) => {
53+
console.log("update UserProfile success", response);
54+
alert('Updated UserProfile successfully')
55+
})
56+
.catch(e => {
57+
console.log("updateUserProfile error", e);
58+
alert('Failed to update UserProfile, try again')
59+
});
60+
};
61+
62+
return (
63+
<div className="container col-md-4">
64+
<div className="card">
65+
<div className="card-header text-center">
66+
<h3>My Profile</h3>
67+
</div>
68+
<div className="card-body">
69+
<form onSubmit={e => handleProfileUpdate(e)} className="row justify-content-center">
70+
<div className="form-group col-md-10">
71+
<label htmlFor="name">Name</label>
72+
<InputText
73+
id="name"
74+
className="form-control col-md-12"
75+
value={name}
76+
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setName(e.target.value)}
77+
/>
78+
</div>
79+
<div className="form-group col-md-10">
80+
<label htmlFor="email">Bio</label>
81+
<InputText
82+
id="bio"
83+
className="form-control col-md-12"
84+
value={bio}
85+
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setBio(e.target.value)}
86+
/>
87+
</div>
88+
<div className="form-group col-md-10">
89+
<label htmlFor="password">Location</label>
90+
<InputText
91+
id="loc"
92+
className="form-control col-md-12"
93+
value={loc}
94+
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setLoc(e.target.value)}
95+
/>
96+
</div>
97+
<div className="form-group col-md-10">
98+
<label htmlFor="password">GitHub UserName</label>
99+
<InputText
100+
id="ghUsername"
101+
className="form-control col-md-12"
102+
value={githubUsername}
103+
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setGithubUsername(e.target.value)}
104+
/>
105+
</div>
106+
<div className="form-group col-md-10">
107+
<label htmlFor="password">Twitter UserName</label>
108+
<InputText
109+
id="twUsername"
110+
className="form-control col-md-12"
111+
value={twitterUsername}
112+
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setTwitterUsername(e.target.value)}
113+
/>
114+
</div>
115+
<div className="form-group col-md-10">
116+
<button type="submit" className="btn btn-primary">
117+
Save
118+
</button>
119+
</div>
120+
</form>
121+
</div>
122+
</div>
123+
</div>
124+
);
125+
};
126+
127+
export default UserProfile;

src/pages/registration/Registration.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const Registration = () => {
1212
const [email, setEmail] = useState("");
1313
const [password, setPassword] = useState("");
1414

15-
const handleLogin = (e: React.FormEvent) => {
15+
const handleRegistration = (e: React.FormEvent) => {
1616
e.preventDefault();
1717
if (!name.trim() || !email.trim() || !password.trim()) {
1818
return;
@@ -35,7 +35,7 @@ const Registration = () => {
3535
<h3>Registration Form</h3>
3636
</div>
3737
<div className="card-body">
38-
<form onSubmit={e => handleLogin(e)} className="row justify-content-center">
38+
<form onSubmit={e => handleRegistration(e)} className="row justify-content-center">
3939
<div className="form-group col-md-10">
4040
<label htmlFor="name">Name</label>
4141
<InputText

src/services/AuthService.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import {loadState, saveState, cleanState} from "../store/localStorage";
22
import axios from "./axios-config";
33

4+
export interface LoginUserModel {
5+
6+
access_token: string,
7+
user: UserModel
8+
}
9+
10+
export interface UserModel {
11+
id: number,
12+
name: string,
13+
email: string,
14+
roles: string[]
15+
}
416
export default class AuthService {
517

618
performLogin = (credentials: {}) => {
@@ -15,7 +27,7 @@ export default class AuthService {
1527
});
1628
}
1729

18-
setCurrentUser = (user: {}) => {
30+
setCurrentUser = (user: LoginUserModel) => {
1931
const state = loadState()
2032
const newState = {
2133
...state,
@@ -24,7 +36,7 @@ export default class AuthService {
2436
saveState(newState);
2537
}
2638

27-
getCurrentUser = () => {
39+
getCurrentUser = (): LoginUserModel => {
2840
const state = loadState()
2941
return state.user || {};
3042
}

src/services/UserService.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
import axios from "./axios-config";
22

3+
export interface UpdateUserModel {
4+
id: number
5+
name: string
6+
bio: string
7+
location: string
8+
githubUsername: string
9+
twitterUsername: string
10+
}
11+
312
export default class AuthService {
413

514
performRegistration = (user: {}) => {
615
return axios.post("/users", user);
716
}
17+
18+
updateUserProfile = (user: UpdateUserModel) => {
19+
return axios.put("/user", user);
20+
}
21+
22+
getUserProfile = (id: number) => {
23+
return axios.get<UpdateUserModel>("/users/"+id);
24+
}
825
}

0 commit comments

Comments
 (0)