"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { useTranslations } from "next-intl"; import { Sparkles } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card"; import { Checkbox } from "@/components/ui/checkbox"; import { Label } from "@/components/ui/label"; import { PushSubscribeButton } from "@/components/pwa/push-subscribe-button"; const DIETARY_TAGS = ["vegan", "vegetarian", "glutenFree", "dairyFree", "nutFree", "halal", "kosher"] as const; const ALLERGEN_TAGS = ["peanuts", "treeNuts", "dairy", "eggs", "shellfish", "fish", "soy", "gluten"] as const; type DietaryTag = (typeof DIETARY_TAGS)[number]; type AllergenTag = (typeof ALLERGEN_TAGS)[number]; const DIET_LABEL_KEYS: Record = { vegan: "dietVegan", vegetarian: "dietVegetarian", glutenFree: "dietGlutenFree", dairyFree: "dietDairyFree", nutFree: "dietNutFree", halal: "dietHalal", kosher: "dietKosher", }; const ALLERGEN_LABEL_KEYS: Record = { peanuts: "allergenPeanuts", treeNuts: "allergenTreeNuts", dairy: "allergenDairy", eggs: "allergenEggs", shellfish: "allergenShellfish", fish: "allergenFish", soy: "allergenSoy", gluten: "allergenGluten", }; const TOTAL_STEPS = 3; export function OnboardingWizard() { const t = useTranslations("onboarding"); const tCommon = useTranslations("common"); const router = useRouter(); const [step, setStep] = useState(1); const [dietaryTags, setDietaryTags] = useState>>({}); const [allergens, setAllergens] = useState>(new Set()); const [finishing, setFinishing] = useState(false); function toggleDietaryTag(tag: DietaryTag, checked: boolean) { setDietaryTags((prev) => ({ ...prev, [tag]: checked })); } function toggleAllergen(tag: AllergenTag, checked: boolean) { setAllergens((prev) => { const next = new Set(prev); if (checked) next.add(tag); else next.delete(tag); return next; }); } async function finish(withData: boolean) { setFinishing(true); try { await fetch("/api/v1/users/me/onboarding", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(withData ? { dietaryTags, allergens: Array.from(allergens) } : {}), }); } catch { // Onboarding completion isn't critical-path — if it fails, the user // just sees the wizard once more next time. Not worth blocking or // erroring loudly for a best-effort save. } finally { router.push("/recipes"); } } return (

{t("stepOf", { step, total: TOTAL_STEPS })}

{step === 1 && <>{t("welcomeTitle")}} {step === 2 && t("dietaryTitle")} {step === 3 && t("notificationsTitle")} {step === 1 && t("welcomeBody")} {step === 2 && t("dietaryBody")} {step === 3 && t("notificationsBody")}
{step === 2 && (

{t("dietaryTagsLabel")}

{DIETARY_TAGS.map((tag) => (
toggleDietaryTag(tag, checked === true)} />
))}

{t("allergensLabel")}

{ALLERGEN_TAGS.map((tag) => (
toggleAllergen(tag, checked === true)} />
))}
)} {step === 3 && ( )} {step === 1 && ( <> )} {step === 2 && ( <> )} {step === 3 && ( )}
); }