"use client"; import { useState } from "react"; import { toast } from "sonner"; import { ResponsiveDialog } from "@/components/responsive-dialog"; import { Button } from "@/components/ui/button"; import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { tr } from "@/i18n/tr"; import { SHOPPING_ITEM_KINDS, type ShoppingItemKind } from "@/shared/shopping"; import { quantityInput } from "@/shared/stock"; import { addShoppingItemAction, updateShoppingItemAction } from "../actions"; import type { ItemOptions, ShoppingItemView } from "../service"; const t = tr.shopping; const f = t.fields; type Errors = Record; /** Kalem ekle (ürün tipi / belirli marka / serbest metin) ya da düzenle (miktar, not). */ export function ShoppingItemDialog({ listId, item, options, onClose, }: { listId: string; item: ShoppingItemView | null; options: ItemOptions; onClose: () => void; }) { const [v, setV] = useState({ kind: (item?.kind ?? "type") as ShoppingItemKind, productTypeId: item?.productTypeId ?? "", productId: item?.productId ?? "", text: item?.text ?? "", quantity: item?.quantity != null ? quantityInput(item.quantity) : "", unit: item?.kind === "text" ? item.unit : "", note: item?.note ?? "", }); const [errors, setErrors] = useState({}); const [busy, setBusy] = useState(false); const set = (k: K, x: (typeof v)[K]) => setV((s) => ({ ...s, [k]: x })); const typeUnit = options.types.find((x) => x.id === v.productTypeId)?.unit ?? ""; const unitSuffix = v.kind === "type" ? (item?.unit ?? typeUnit) : v.kind === "product" ? t.packageUnit : ""; const productGroups = new Map(); for (const p of options.products) productGroups.set(p.typeName, [...(productGroups.get(p.typeName) ?? []), p]); async function submit(e: React.FormEvent) { e.preventDefault(); setBusy(true); const r = item ? await updateShoppingItemAction(listId, item.id, { quantity: v.quantity, text: v.text, unit: v.unit, note: v.note, }) : await addShoppingItemAction(listId, v); setBusy(false); if (r.ok) { toast.success(item ? t.toast.itemSaved : t.toast.itemAdded); onClose(); } else setErrors({ ...r.fieldErrors, root: r.fieldErrors ? undefined : r.error }); } return ( !o && onClose()} title={item ? t.editItemTitle : t.addItemTitle} description={item && item.kind !== "text" ? item.label : undefined} footer={ <> } >
{item ? null : ( {f.kind} set("kind", x as ShoppingItemKind)}> {SHOPPING_ITEM_KINDS.map((k) => ( {t.kinds[k]} ))} {t.kindHints[v.kind]} )} {!item && v.kind === "type" ? ( {f.productType} {errors.productTypeId ? {errors.productTypeId} : null} ) : null} {!item && v.kind === "product" ? ( {f.product} {errors.productId ? {errors.productId} : null} ) : null} {v.kind === "text" ? ( {f.text} set("text", e.target.value)} aria-invalid={Boolean(errors.text)} autoFocus={!item} /> {errors.text ? {errors.text} : null} ) : null}
{v.kind === "product" ? f.packages : f.quantity} {unitSuffix ? ` (${unitSuffix})` : ""} set("quantity", e.target.value)} className="text-right tabular-nums" aria-invalid={Boolean(errors.quantity)} /> {errors.quantity ? {errors.quantity} : null} {v.kind === "text" ? ( {f.unit} set("unit", e.target.value)} /> ) : null}
{v.kind !== "text" ? ( {f.quantityHint} ) : null} {f.itemNote} set("note", e.target.value)} /> {errors.root ? {errors.root} : null}
); }