"use client"; import { useState } from "react"; import { useTranslations } from "next-intl"; import { Plus, ShoppingCart } from "lucide-react"; import { useRouter } from "next/navigation"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; export function NewShoppingListButton() { const t = useTranslations("mealPlan"); const tCommon = useTranslations("common"); const router = useRouter(); const [open, setOpen] = useState(false); const [name, setName] = useState(""); const [weekStart, setWeekStart] = useState(""); const [saving, setSaving] = useState(false); async function create() { if (!name.trim()) return; setSaving(true); try { const res = await fetch("/api/v1/shopping-lists", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: name.trim(), fromMealPlanWeek: weekStart || undefined, }), }); if (!res.ok) { toast.error(t("listCreateFailed")); return; } const { id } = await res.json() as { id: string }; toast.success(t("listCreated")); setOpen(false); setName(""); setWeekStart(""); router.push(`/shopping-lists/${id}`); } finally { setSaving(false); } } return ( <> setOpen(true)}> {t("newList")} {t("newListTitle")} {t("listNameLabel")} setName(e.target.value)} placeholder={t("listNamePlaceholder")} /> {t("generateFromWeek")} setWeekStart(e.target.value)} /> {t("generateFromWeekHint")} setOpen(false)}>{tCommon("cancel")} {saving ? t("listCreating") : t("listCreate")} > ); }
{t("generateFromWeekHint")}