fix data flow issues
This commit is contained in:
@@ -2,44 +2,57 @@
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import OnboardingWizard from '@/components/onboarding/OnboardingWizard';
|
||||
import { Loader2 } from 'lucide-react';
|
||||
|
||||
// TODO: Re-enable API integration once backend is ready
|
||||
// import { redirect } from 'next/navigation';
|
||||
|
||||
import OnboardingWizard from '@/components/onboarding/OnboardingWizard';
|
||||
import { useAuth } from '@/lib/auth/AuthContext';
|
||||
import { useServices } from '@/lib/services/ServiceProvider';
|
||||
|
||||
export default function OnboardingPage() {
|
||||
const router = useRouter();
|
||||
const { session } = useAuth();
|
||||
const { driverService } = useServices();
|
||||
const [checking, setChecking] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
// TODO: Re-enable auth check once backend is ready
|
||||
// For now, just show onboarding after a brief check
|
||||
const checkDemoMode = () => {
|
||||
// Check if user has demo mode cookie
|
||||
const cookies = document.cookie.split(';');
|
||||
const demoModeCookie = cookies.find(c => c.trim().startsWith('gridpilot_demo_mode='));
|
||||
|
||||
if (!demoModeCookie) {
|
||||
// Not logged in, redirect to auth
|
||||
router.push('/auth/login?returnTo=/onboarding');
|
||||
return;
|
||||
// If user is not authenticated, redirect to login
|
||||
if (!session) {
|
||||
router.replace('/auth/login?returnTo=/onboarding');
|
||||
return;
|
||||
}
|
||||
|
||||
let cancelled = false;
|
||||
|
||||
const checkOnboarding = async () => {
|
||||
try {
|
||||
const driver = await driverService.getCurrentDriver();
|
||||
|
||||
if (cancelled) return;
|
||||
|
||||
// If driver profile exists, onboarding is complete – go to dashboard
|
||||
if (driver) {
|
||||
router.replace('/dashboard');
|
||||
return;
|
||||
}
|
||||
|
||||
// Otherwise allow onboarding wizard to render
|
||||
setChecking(false);
|
||||
} catch {
|
||||
// On error, allow onboarding to proceed so user isn't blocked
|
||||
if (!cancelled) {
|
||||
setChecking(false);
|
||||
}
|
||||
}
|
||||
|
||||
// For demo, skip onboarding and go to dashboard
|
||||
// In production, this would check if onboarding is complete
|
||||
router.push('/dashboard');
|
||||
};
|
||||
|
||||
// Brief delay to prevent flash
|
||||
const timer = setTimeout(() => {
|
||||
checkDemoMode();
|
||||
}, 500);
|
||||
checkOnboarding();
|
||||
|
||||
return () => clearTimeout(timer);
|
||||
}, [router]);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [session, driverService, router]);
|
||||
|
||||
// Show loading while checking
|
||||
// Show loading while checking auth/onboarding status
|
||||
if (checking) {
|
||||
return (
|
||||
<main className="min-h-screen bg-deep-graphite flex items-center justify-center">
|
||||
|
||||
Reference in New Issue
Block a user