website refactor
This commit is contained in:
@@ -1,66 +1,36 @@
|
||||
'use client';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { OnboardingWizardClient } from './OnboardingWizardClient';
|
||||
import { OnboardingPageQuery } from '@/lib/page-queries/page-queries/OnboardingPageQuery';
|
||||
import { SearchParamBuilder } from '@/lib/routing/search-params/SearchParamBuilder';
|
||||
import { routes } from '@/lib/routing/RouteConfig';
|
||||
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useEffect } from 'react';
|
||||
|
||||
import OnboardingWizard from '@/components/onboarding/OnboardingWizard';
|
||||
import { useAuth } from '@/lib/auth/AuthContext';
|
||||
|
||||
// Shared state components
|
||||
import { useCurrentDriver } from "@/lib/hooks/driver/useCurrentDriver";
|
||||
import { StatefulPageWrapper } from '@/components/shared/state/StatefulPageWrapper';
|
||||
|
||||
// Template component that accepts data
|
||||
function OnboardingTemplate({ data }: { data: any }) {
|
||||
return <OnboardingWizard />;
|
||||
}
|
||||
|
||||
export default function OnboardingPage() {
|
||||
const router = useRouter();
|
||||
const { session } = useAuth();
|
||||
|
||||
// Check if user is logged in
|
||||
const shouldRedirectToLogin = !session;
|
||||
|
||||
// Fetch current driver data using DI + React-Query
|
||||
const { data: driver, isLoading, error, refetch } = useCurrentDriver({
|
||||
enabled: !!session,
|
||||
});
|
||||
|
||||
const shouldRedirectToDashboard = !isLoading && Boolean(driver);
|
||||
|
||||
useEffect(() => {
|
||||
if (shouldRedirectToLogin) {
|
||||
router.replace('/auth/login?returnTo=/onboarding');
|
||||
return;
|
||||
/**
|
||||
* Onboarding Page
|
||||
*
|
||||
* Server Component that handles authentication and authorization.
|
||||
* Redirects to login if not authenticated.
|
||||
* Redirects to dashboard if already onboarded.
|
||||
*/
|
||||
export default async function OnboardingPage() {
|
||||
// Use PageQuery to check if user is already onboarded
|
||||
const result = await OnboardingPageQuery.execute();
|
||||
|
||||
if (result.isErr()) {
|
||||
const error = result.getError();
|
||||
|
||||
if (error === 'unauthorized') {
|
||||
redirect(`${routes.auth.login}${SearchParamBuilder.auth(routes.protected.onboarding)}`);
|
||||
} else if (error === 'serverError' || error === 'networkError') {
|
||||
// Show error page or let them proceed with a warning
|
||||
// For now, we'll let them proceed
|
||||
}
|
||||
if (shouldRedirectToDashboard) {
|
||||
router.replace('/dashboard');
|
||||
} else {
|
||||
const viewData = result.unwrap();
|
||||
|
||||
if (viewData.isAlreadyOnboarded) {
|
||||
redirect(routes.protected.dashboard);
|
||||
}
|
||||
}, [router, shouldRedirectToLogin, shouldRedirectToDashboard]);
|
||||
|
||||
if (shouldRedirectToLogin) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (shouldRedirectToDashboard) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// For the StatefulPageWrapper, we need to provide data even if it's empty
|
||||
// The page is workflow-driven, not data-driven
|
||||
const wrapperData = driver || {};
|
||||
|
||||
return (
|
||||
<main className="min-h-screen bg-deep-graphite">
|
||||
<StatefulPageWrapper
|
||||
data={wrapperData}
|
||||
isLoading={isLoading}
|
||||
error={error}
|
||||
retry={refetch}
|
||||
Template={OnboardingTemplate}
|
||||
loading={{ variant: 'full-screen', message: 'Loading onboarding...' }}
|
||||
/>
|
||||
</main>
|
||||
);
|
||||
return <OnboardingWizardClient />;
|
||||
}
|
||||
Reference in New Issue
Block a user