From b3beb6426526ae4ac20a52c7a743c2d7180addfb Mon Sep 17 00:00:00 2001 From: =bhagwanSingh <=bhagwan.r@hicglobalsolutions.com> Date: Wed, 8 Oct 2025 15:56:06 +0530 Subject: [PATCH] ProtectedRoute Update --- src/components/routes_helper/ProtectedRoute.tsx | 16 ++++++++++------ src/store/slices/userSlice.ts | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/components/routes_helper/ProtectedRoute.tsx b/src/components/routes_helper/ProtectedRoute.tsx index ec98db7..d694725 100644 --- a/src/components/routes_helper/ProtectedRoute.tsx +++ b/src/components/routes_helper/ProtectedRoute.tsx @@ -1,14 +1,18 @@ import { JSX, useLayoutEffect } from 'react'; -import { useDispatch } from "react-redux"; +import { useDispatch, useSelector } from "react-redux"; import { fetchUserDetails } from '../../store/slices/userSlice'; -import { AppDispatch } from '../../store/store'; +import { AppDispatch, RootState } from '../../store/store'; const ProtectedRoute = ({ children }: { children: JSX.Element }) => { const dispatch = useDispatch(); - - // useLayoutEffect(() => { - dispatch(fetchUserDetails()); - // }, []); + const user = useSelector((state: RootState) => state.user); + + useLayoutEffect(() => { + // Fetch only if we don't have user data yet + if (user.id === null && user.loading === false) { + dispatch(fetchUserDetails()); + } + }, [dispatch, user, status]); return children; }; diff --git a/src/store/slices/userSlice.ts b/src/store/slices/userSlice.ts index d01df7c..f00fca9 100644 --- a/src/store/slices/userSlice.ts +++ b/src/store/slices/userSlice.ts @@ -32,7 +32,7 @@ const initialState: UserState = { language: "", account: null, picture: undefined, - loading: true, + loading: false, error: null, authorized: false, accessToken: undefined,