Skip to content

Commit

Permalink
set user context after refreshing page
Browse files Browse the repository at this point in the history
  • Loading branch information
radekm2000 committed Mar 18, 2024
1 parent 3532656 commit 9057420
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions client/ecommerce/src/api/axios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,8 @@ export const addReview = async ({
const response = await axiosApi.post(`reviews`, data);
return response.data;
};

export const fetchUserInfoWhenLostContext = async () => {
const response = await axiosApi.get("users/profile");
return response.data as User;
};
14 changes: 13 additions & 1 deletion client/ecommerce/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import { useNotifications } from "../hooks/useNotifications";
import { useProductNotifications } from "../hooks/useProductNotifications";
import { useMarkProductNotificationAsRead } from "../hooks/useMarkProductNotificationAsRead";
import PopoverPopupState from "./PopoverPopupState";
import { useUserInfo } from "../hooks/useUserInfo";
import { useFetchUserInfo } from "../hooks/useFetchUserInfo";

export const NotAuthed = () => {
return (
Expand Down Expand Up @@ -122,7 +124,7 @@ export const Navbar = () => {
const [searchInputValue, setSearchInputValue] = useState("");
const [value, setValue] = useState(0);
const below800 = useMediaQuery(800);
const { user } = useUserContext();
const { user, setUser } = useUserContext();
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const [mobileMoreAnchorEl, setMobileMoreAnchorEl] =
useState<null | HTMLElement>(null);
Expand All @@ -135,6 +137,15 @@ export const Navbar = () => {
const handleProductNotificationsIconClick = () => {
markProductNotificationsAsRead();
};
const {
data: userData,
isLoading: isUserLoading,
isSuccess: isFetchedUserSuccess,
} = useFetchUserInfo(user.id);
if (isFetchedUserSuccess && !isUserLoading) {
setUser(userData);
}
console.log(user);
const {
data: productNotificationsReceived,
isLoading: isProductNotificationsLoading,
Expand All @@ -144,6 +155,7 @@ export const Navbar = () => {
const { mutate: markProductNotificationsAsRead } = mutation;
const { data: notificationsReceived, isLoading: isNotificationsLoading } =
useNotifications(user.id);

if (!user.username && !user.id) {
return;
}
Expand Down
10 changes: 10 additions & 0 deletions client/ecommerce/src/hooks/useFetchUserInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useQuery } from "@tanstack/react-query";
import { fetchUserInfoWhenLostContext } from "../api/axios";

export const useFetchUserInfo = (userId: number) => {
return useQuery({
queryKey: ["user", "me"],
queryFn: fetchUserInfoWhenLostContext,
enabled: userId === 0,
});
};

0 comments on commit 9057420

Please sign in to comment.