From 523fc663e81dc4f679a2eb839c28d7a2343950c0 Mon Sep 17 00:00:00 2001 From: Urtzi Alfaro Date: Mon, 18 Aug 2025 14:07:58 +0200 Subject: [PATCH] Improve the dahboard 1 --- .../src/components/auth/ProtectedRoute.tsx | 56 +++++++++++++++++-- frontend/src/store/slices/authSlice.ts | 2 + 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/frontend/src/components/auth/ProtectedRoute.tsx b/frontend/src/components/auth/ProtectedRoute.tsx index 428deafc..847284e1 100644 --- a/frontend/src/components/auth/ProtectedRoute.tsx +++ b/frontend/src/components/auth/ProtectedRoute.tsx @@ -1,7 +1,9 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { Navigate, useLocation } from 'react-router-dom'; -import { useSelector } from 'react-redux'; +import { useSelector, useDispatch } from 'react-redux'; import { RootState } from '../../store'; +import { completeOnboarding } from '../../store/slices/authSlice'; +import { OnboardingRouter } from '../../utils/onboardingRouter'; interface ProtectedRouteProps { children: React.ReactNode; @@ -9,7 +11,9 @@ interface ProtectedRouteProps { const ProtectedRoute: React.FC = ({ children }) => { const location = useLocation(); + const dispatch = useDispatch(); const { isAuthenticated, user } = useSelector((state: RootState) => state.auth); + const [onboardingCheck, setOnboardingCheck] = useState<'checking' | 'complete' | 'incomplete'>('checking'); // Check if user is authenticated if (!isAuthenticated || !user) { @@ -17,16 +21,56 @@ const ProtectedRoute: React.FC = ({ children }) => { return ; } - // Check if user needs onboarding (except for onboarding and settings routes) + // Sync onboarding status with backend on mount and navigation + useEffect(() => { + const checkOnboardingStatus = async () => { + try { + // If user already marked as onboarding complete, skip API check + if (user.isOnboardingComplete) { + setOnboardingCheck('complete'); + return; + } + + // Check actual onboarding progress from backend + const canAccess = await OnboardingRouter.canAccessDashboard(); + + if (canAccess) { + // User has completed onboarding according to backend, update Redux state + dispatch(completeOnboarding()); + setOnboardingCheck('complete'); + } else { + setOnboardingCheck('incomplete'); + } + } catch (error) { + console.error('Error checking onboarding status:', error); + // On error, use current Redux state + setOnboardingCheck(user.isOnboardingComplete ? 'complete' : 'incomplete'); + } + }; + + checkOnboardingStatus(); + }, [user.isOnboardingComplete, dispatch]); + + // Show loading while checking onboarding status + if (onboardingCheck === 'checking') { + return ( +
+
+
+ ); + } + + // Route-based logic const isOnboardingRoute = location.pathname.includes('/onboarding'); const isSettingsRoute = location.pathname.includes('/settings'); - if (!user.isOnboardingComplete && !isOnboardingRoute && !isSettingsRoute) { + // If onboarding not complete and not on onboarding/settings route, redirect to onboarding + if (onboardingCheck === 'incomplete' && !isOnboardingRoute && !isSettingsRoute) { return ; } - // If user completed onboarding but is on onboarding route, redirect to dashboard - if (user.isOnboardingComplete && isOnboardingRoute) { + // If onboarding complete but on onboarding route, redirect to dashboard + if (onboardingCheck === 'complete' && isOnboardingRoute) { return ; } diff --git a/frontend/src/store/slices/authSlice.ts b/frontend/src/store/slices/authSlice.ts index 5f5cd0a2..2dd5cf57 100644 --- a/frontend/src/store/slices/authSlice.ts +++ b/frontend/src/store/slices/authSlice.ts @@ -61,6 +61,8 @@ const authSlice = createSlice({ completeOnboarding: (state) => { if (state.user) { state.user.isOnboardingComplete = true; + // Persist updated user data to localStorage + localStorage.setItem('user_data', JSON.stringify(state.user)); } }, },