-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from Code-Hammers/user-profile
User profile
- Loading branch information
Showing
8 changed files
with
251 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit"; | ||
import axios from "axios"; | ||
import { IProfile } from "../../../types/profile"; | ||
|
||
interface ProfileState { | ||
profile: IProfile | null; //TODO ADD PROPER TYPING ONCE OBJECT IS FINALIZED | ||
status: "idle" | "loading" | "failed"; | ||
error: string | null; | ||
} | ||
|
||
const initialState: ProfileState = { | ||
profile: null, | ||
status: "idle", | ||
error: null, | ||
}; | ||
|
||
//TODO REVIEW TYPING | ||
export const fetchUserProfile = createAsyncThunk( | ||
"profile/fetchUserProfile", | ||
async (userID: string, thunkAPI) => { | ||
try { | ||
const response = await axios.get(`/api/profiles/${userID}`); | ||
return response.data; | ||
} catch (error) { | ||
if (axios.isAxiosError(error)) { | ||
return thunkAPI.rejectWithValue( | ||
error.response?.data || "Error fetching profiles" | ||
); | ||
} | ||
return thunkAPI.rejectWithValue("An unexpected error occurred"); | ||
} | ||
} | ||
); | ||
|
||
const userProfileSlice = createSlice({ | ||
name: "profile", | ||
initialState, | ||
reducers: { | ||
resetProfileState(state) { | ||
state.profile = null; | ||
state.status = "idle"; | ||
state.error = null; | ||
}, | ||
}, | ||
extraReducers: (builder) => { | ||
builder | ||
.addCase(fetchUserProfile.pending, (state) => { | ||
state.status = "loading"; | ||
}) | ||
.addCase(fetchUserProfile.fulfilled, (state, action) => { | ||
state.profile = action.payload; | ||
state.status = "idle"; | ||
}) | ||
.addCase(fetchUserProfile.rejected, (state, action) => { | ||
state.status = "failed"; | ||
//TODO state.error = action.payload as string; WHAT WOULD PAYLOAD LOOK LIKE HERE? | ||
//TODO HOOK UP GOOD ERROR INFO TO ERROR STATE | ||
}); | ||
}, | ||
}); | ||
|
||
export default userProfileSlice.reducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import "@testing-library/jest-dom"; | ||
import Profile from "./Profile"; | ||
import { useAppSelector, useAppDispatch } from "../../app/hooks"; | ||
import { fetchUserProfile } from "../../features/userProfile/userProfileSlice"; | ||
|
||
jest.mock("../../app/hooks", () => ({ | ||
useAppDispatch: jest.fn(), | ||
useAppSelector: jest.fn(), | ||
})); | ||
|
||
jest.mock("../../features/userProfile/userProfileSlice", () => ({ | ||
fetchUserProfile: jest.fn(), | ||
})); | ||
|
||
describe("Profile Component", () => { | ||
const mockDispatch = jest.fn(); | ||
const mockUser = { | ||
_id: "12345", | ||
name: "John Doe", | ||
}; | ||
//TODO MOCK BETTER USERPROFILE DATA | ||
beforeEach(() => { | ||
(useAppDispatch as jest.Mock).mockReturnValue(mockDispatch); | ||
(useAppSelector as jest.Mock).mockImplementation((selector) => | ||
selector({ | ||
user: { userData: mockUser }, | ||
userProfile: { profile: null }, | ||
}) | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("renders the Profile component", () => { | ||
render(<Profile />); | ||
const profileTitle = screen.getByText("Profile"); | ||
expect(profileTitle).toBeInTheDocument(); | ||
}); | ||
|
||
it("dispatches fetchUserProfile on component mount", () => { | ||
render(<Profile />); | ||
expect(mockDispatch).toHaveBeenCalledWith(fetchUserProfile(mockUser._id)); | ||
}); | ||
|
||
it("displays the user's ID", () => { | ||
render(<Profile />); | ||
const userIdDisplay = screen.getByText(mockUser._id); | ||
expect(userIdDisplay).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React, { useEffect } from "react"; | ||
import { useAppSelector, useAppDispatch } from "../../app/hooks"; | ||
import { fetchUserProfile } from "../../features/userProfile/userProfileSlice"; | ||
|
||
const Profile = (): JSX.Element => { | ||
const dispatch = useAppDispatch(); | ||
const userProfile = useAppSelector((state) => state.userProfile.profile); | ||
const user = useAppSelector((state) => state.user.userData); | ||
|
||
useEffect(() => { | ||
dispatch(fetchUserProfile(user._id)); | ||
}, [dispatch]); | ||
return ( | ||
<div className="min-h-screen bg-gray-100 flex flex-col items-center justify-center"> | ||
<h1 className="text-4xl font-extrabold mb-4">Profile</h1> | ||
<h2 className="text-4xl font-extrabold mb-4">{user._id}</h2> | ||
<h2 className="text-4xl font-extrabold mb-4">{userProfile?.bio}</h2> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Profile; |
23 changes: 23 additions & 0 deletions
23
client/src/pages/Profiles/__snapshots__/Profiles.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`MainPage Component renders correctly 1`] = ` | ||
[ | ||
<div | ||
className="min-h-screen bg-gray-100 flex flex-col items-center justify-center" | ||
> | ||
<h1 | ||
className="text-4xl font-extrabold mb-4" | ||
> | ||
PROFILES | ||
</h1> | ||
</div>, | ||
<div> | ||
<div> | ||
User1 | ||
</div> | ||
<div> | ||
User2 | ||
</div> | ||
</div>, | ||
] | ||
`; |