"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { useTranslations } from "next-intl";
import { ChefHat, Loader2, Minus, Plus } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
} from "@/components/ui/dialog";
import { Label } from "@/components/ui/label";
import { Input } from "@/components/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { FakeProgressBar } from "@/components/ui/fake-progress-bar";
function Counter({ value, onChange, max = 6 }: { value: number; onChange: (v: number) => void; max?: number }) {
return (
{value}
);
}
export function BatchCookGenerateDialog({
open,
onOpenChange,
}: {
open: boolean;
onOpenChange: (open: boolean) => void;
}) {
const t = useTranslations("batchCooking");
const tCommon = useTranslations("common");
const tRecipe = useTranslations("recipe");
const router = useRouter();
const [dinners, setDinners] = useState(4);
const [lunches, setLunches] = useState(0);
const [servings, setServings] = useState(4);
const [dietaryPrefs, setDietaryPrefs] = useState("");
const [difficulty, setDifficulty] = useState<"" | "easy" | "medium" | "hard">("");
const [busy, setBusy] = useState(false);
function handleClose() {
if (busy) return;
onOpenChange(false);
}
async function handleGenerate() {
setBusy(true);
try {
const res = await fetch("/api/v1/ai/batch-cook/generate", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
dinners,
lunches,
servings,
dietaryPrefs: dietaryPrefs.trim() || undefined,
difficulty: difficulty || undefined,
}),
});
if (!res.ok) {
const err = await res.json() as { error?: string };
toast.error(err.error ?? t("generateFailed"));
return;
}
const { id } = await res.json() as { id: string };
toast.success(t("generateSuccess"));
onOpenChange(false);
router.push(`/recipes/${id}`);
} finally {
setBusy(false);
}
}
const canSubmit = dinners + lunches > 0;
return (
);
}