"use client"; import { useRef, useState, useTransition } from "react"; import { createContainer, updateContainer } from "../server/actions"; import type { ContainerDto } from "../server/queries"; const CONTAINER_TYPES = [ { value: "shelf", label: "Shelf" }, { value: "terrarium", label: "Terrarium" }, { value: "raised-bed", label: "Raised bed" }, { value: "window-box", label: "Window box" }, { value: "single-pot", label: "Single pot" }, { value: "outdoor", label: "Outdoor" }, { value: "other", label: "Other" }, ]; type Props = { existing?: ContainerDto; onSuccess?: () => void; onCancel?: () => void; }; export function ContainerForm({ existing, onSuccess, onCancel }: Props) { const [error, setError] = useState(null); const [isPending, startTransition] = useTransition(); const formRef = useRef(null); function handleSubmit(e: React.FormEvent) { e.preventDefault(); const fd = new FormData(e.currentTarget); const input = { name: fd.get("name") as string, type: fd.get("type") as string, locationNotes: (fd.get("locationNotes") as string) || null, }; setError(null); startTransition(async () => { try { if (existing) { await updateContainer({ id: existing.id, ...input }); } else { await createContainer(input); } formRef.current?.reset(); onSuccess?.(); } catch { setError("Something went wrong. Please try again."); } }); } return (