Skip to content

Commit 33f1a7d

Browse files
committed
feat: Add US OpenClassrooms-Student-Center#4 (Profile) user profile fetching functionality + Profile component refactor + Redux state update
1 parent 6d72d50 commit 33f1a7d

4 files changed

Lines changed: 71 additions & 8 deletions

File tree

frontend/src/pages/Profile.jsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
import { useEffect } from 'react';
2+
import { useDispatch, useSelector } from 'react-redux';
3+
import { fetchUserProfile } from '../service/user/userApi';
4+
15
const Profile = () => {
2-
return (
6+
const dispatch = useDispatch();
7+
const profile = useSelector((state) => state.user);
8+
9+
useEffect(() => {
10+
dispatch(fetchUserProfile());
11+
}, [dispatch]);
12+
13+
console.log(profile);
14+
return profile.user ? (
315
<main className='main bg-dark'>
416
<div className='header'>
517
<h1>
618
Welcome back
719
<br />
8-
Tony Jarvis!
20+
{profile.user.body.firstName + ' ' + profile.user.body.lastName}!
921
</h1>
1022
<button className='edit-button'>Edit Name</button>
1123
</div>
@@ -41,7 +53,11 @@ const Profile = () => {
4153
</div>
4254
</section>
4355
</main>
56+
) : (
57+
<main className='main bg-dark'>
58+
<h1>Loading...</h1>
59+
</main>
4460
);
4561
};
4662

47-
export default Profile;
63+
export default Profile;

frontend/src/service/store.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { configureStore } from '@reduxjs/toolkit';
2-
import authSlice from '../service/user/userSlice';
2+
import { authReducer, userReducer } from '../service/user/userSlice';
33

4-
// The value of isAuthenticated is determined by
4+
// The value of isAuthenticated is determined by
55
// whether there is a 'token' item in the local storage.
66
const preloadedState = {
77
auth: {
88
isAuthenticated: !!localStorage.getItem('token'),
99
},
10+
user: {
11+
user: null,
12+
loading: false,
13+
},
1014
};
1115

1216
export const store = configureStore({
1317
reducer: {
14-
auth: authSlice,
18+
auth: authReducer,
19+
user: userReducer,
1520
},
1621
preloadedState,
1722
});

frontend/src/service/user/userApi.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,25 @@ export const authenticate = createAsyncThunk(
2222
}
2323
}
2424
);
25+
26+
// Async thunk to fetch user profile
27+
export const fetchUserProfile = createAsyncThunk(
28+
'user/fetchProfile',
29+
async () => {
30+
const token = localStorage.getItem('token');
31+
const response = await fetch('http://localhost:3001/api/v1/user/profile', {
32+
method: 'POST',
33+
headers: {
34+
Authorization: `Bearer ${token}`,
35+
},
36+
});
37+
const data = await response.json();
38+
if (response.ok) {
39+
console.log(data.message);
40+
return data;
41+
} else {
42+
window.alert(data.message);
43+
throw new Error(data.message);
44+
}
45+
}
46+
);

frontend/src/service/user/userSlice.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createSlice } from '@reduxjs/toolkit';
2-
import { authenticate } from './userApi';
2+
import { authenticate, fetchUserProfile } from './userApi';
33

44
// The authSlice reducer manages the state of the user's authentication status.
55
const authSlice = createSlice({
@@ -18,4 +18,24 @@ const authSlice = createSlice({
1818
},
1919
});
2020

21-
export default authSlice.reducer;
21+
const userSlice = createSlice({
22+
name: 'user',
23+
initialState: { user: null, loading: false },
24+
reducers: {},
25+
extraReducers: (builder) => {
26+
builder
27+
.addCase(fetchUserProfile.pending, (state) => {
28+
state.loading = true;
29+
})
30+
.addCase(fetchUserProfile.fulfilled, (state, action) => {
31+
state.user = action.payload;
32+
state.loading = false;
33+
})
34+
.addCase(fetchUserProfile.rejected, (state) => {
35+
state.loading = false;
36+
});
37+
},
38+
});
39+
40+
export const authReducer = authSlice.reducer;
41+
export const userReducer = userSlice.reducer;

0 commit comments

Comments
 (0)