From ecd78e2f0f15beba9985b86c4fa1c14f5a38f39a Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Wed, 25 Sep 2024 16:09:23 +0530 Subject: [PATCH] feat: ensure partial rendering does not happen --- src/routes/userRoute.tsx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/routes/userRoute.tsx b/src/routes/userRoute.tsx index 711f9e31..4a405e9d 100644 --- a/src/routes/userRoute.tsx +++ b/src/routes/userRoute.tsx @@ -8,7 +8,8 @@ interface UserRouteProps { } const UserRoute: React.FC = ({ children }) => { - const [ok, setOk] = useState(true); // Default to true to allow rendering while fetching + const [loading, setLoading] = useState(true); + const [ok, setOk] = useState(false); const navigate = useNavigate(); const { notify } = useGlobalInfoStore(); @@ -26,6 +27,8 @@ const UserRoute: React.FC = ({ children }) => { } } catch (err: any) { handleRedirect(err.response?.data?.error || 'An error occurred. Please login again.'); + } finally { + setLoading(false); // Remove loading state regardless of success or failure } }; @@ -33,13 +36,16 @@ const UserRoute: React.FC = ({ children }) => { setOk(false); if (errorMessage) { notify('error', errorMessage); + } else { + notify('error', 'Please login again to continue'); } navigate('/login'); }; - // If ok is true, render the children (protected route) + // Block rendering if loading the authentication status + if (loading) return null; + return <>{ok ? children : null}; }; export default UserRoute; -