'use client'; import { useState, useTransition } from 'react'; import type { Product } from '../../features/inventory/types'; import { addPantryItem } from './actions'; type Props = { products: Product[]; pantryProductIds: Set; }; export default function AddToPantryForm({ products, pantryProductIds }: Props) { const [selectedId, setSelectedId] = useState(''); const [isPending, startTransition] = useTransition(); const [error, setError] = useState(null); const available = products.filter((p) => !pantryProductIds.has(p.id)); function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!selectedId) return; setError(null); startTransition(async () => { try { await addPantryItem(Number(selectedId)); setSelectedId(''); } catch (err) { setError(err instanceof Error ? err.message : 'Okänt fel'); } }); } return (
{error && {error}}
); }