"use client"; import { useState, useEffect, useCallback } from "react"; import { useRouter } from "next/navigation"; import { AuthGuard } from "@/components/auth/AuthGuard"; import { Button } from "@/components/ui/Button"; import { Spinner } from "@/components/ui/Spinner"; import { api } from "@/lib/api"; import { EquipmentStep } from "@/components/onboarding/EquipmentStep"; import { GoalsStep } from "@/components/onboarding/GoalsStep"; import { WorkoutTypesStep } from "@/components/onboarding/WorkoutTypesStep"; import { ScheduleStep } from "@/components/onboarding/ScheduleStep"; import { DurationStep } from "@/components/onboarding/DurationStep"; import { MusclesStep } from "@/components/onboarding/MusclesStep"; import { InjuryStep } from "@/components/onboarding/InjuryStep"; import { ExcludedExercisesStep } from "@/components/onboarding/ExcludedExercisesStep"; import type { InjuryType } from "@/lib/types"; const STEP_LABELS = [ "Equipment", "Goals", "Workout Types", "Schedule", "Duration", "Muscles", "Injuries", "Excluded Exercises", ]; interface PreferencesData { equipment_ids: number[]; muscle_ids: number[]; workout_type_ids: number[]; fitness_level: number; primary_goal: string; secondary_goal: string; days_per_week: number; preferred_workout_duration: number; preferred_days: number[]; injury_types: InjuryType[]; excluded_exercise_ids: number[]; } export default function OnboardingPage() { const router = useRouter(); const [currentStep, setCurrentStep] = useState(0); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [hasExistingPrefs, setHasExistingPrefs] = useState(false); const [preferences, setPreferences] = useState({ equipment_ids: [], muscle_ids: [], workout_type_ids: [], fitness_level: 1, primary_goal: "general_fitness", secondary_goal: "", days_per_week: 4, preferred_workout_duration: 45, preferred_days: [], injury_types: [], excluded_exercise_ids: [], }); useEffect(() => { async function fetchExisting() { try { const existing = await api.getPreferences(); const hasPrefs = existing.available_equipment.length > 0 || existing.preferred_workout_types.length > 0 || existing.target_muscle_groups.length > 0; setHasExistingPrefs(hasPrefs); setPreferences({ equipment_ids: existing.available_equipment.map((e) => e.id), muscle_ids: existing.target_muscle_groups.map((m) => m.id), workout_type_ids: existing.preferred_workout_types.map((w) => w.id), fitness_level: existing.fitness_level || 1, primary_goal: existing.primary_goal || "general_fitness", secondary_goal: existing.secondary_goal || "", days_per_week: existing.days_per_week || 4, preferred_workout_duration: existing.preferred_workout_duration || 45, preferred_days: existing.preferred_days || [], injury_types: existing.injury_types || [], excluded_exercise_ids: existing.excluded_exercises || [], }); } catch { // No existing preferences - use defaults } finally { setLoading(false); } } fetchExisting(); }, []); const updatePreferences = useCallback( (updates: Partial) => { setPreferences((prev) => ({ ...prev, ...updates })); }, [] ); const handleNext = async () => { setSaving(true); try { await api.updatePreferences({ ...preferences }); if (currentStep === STEP_LABELS.length - 1) { router.push("/dashboard"); } else { setCurrentStep((prev) => prev + 1); } } catch (err) { console.error("Failed to save preferences:", err); } finally { setSaving(false); } }; const handleBack = () => { setCurrentStep((prev) => Math.max(0, prev - 1)); }; const progressPercent = ((currentStep + 1) / STEP_LABELS.length) * 100; return (
{/* Progress bar */}
Step {currentStep + 1} of {STEP_LABELS.length}
{STEP_LABELS[currentStep]} {hasExistingPrefs && ( )}
{/* Step content */}
{loading ? (
) : ( <> {currentStep === 0 && ( updatePreferences({ equipment_ids: ids })} /> )} {currentStep === 1 && ( updatePreferences(data)} /> )} {currentStep === 2 && ( updatePreferences({ workout_type_ids: ids }) } /> )} {currentStep === 3 && ( updatePreferences(data)} /> )} {currentStep === 4 && ( updatePreferences({ preferred_workout_duration: min }) } /> )} {currentStep === 5 && ( updatePreferences({ muscle_ids: ids })} /> )} {currentStep === 6 && ( updatePreferences({ injury_types: injuries }) } /> )} {currentStep === 7 && ( updatePreferences({ excluded_exercise_ids: ids }) } /> )} )}
{/* Bottom navigation */} {!loading && (
)}
); }