'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import type { Product } from '../../features/inventory/types'; import { UNIT_OPTIONS } from '../../lib/units'; type Props = { products: Product[]; }; export default function InventoryForm({ products }: Props) { const [isPending, setIsPending] = useState(false); const [error, setError] = useState(null); const [isOpen, setIsOpen] = useState(false); const router = useRouter(); const LOCATION_OPTIONS = [ { value: '', label: 'Välj plats' }, { value: 'Kyl', label: 'Kyl' }, { value: 'Frys', label: 'Frys' }, { value: 'Skafferi', label: 'Skafferi' }, { value: 'Annat', label: 'Annat' }, ]; function parseQuantityInput(input: string, defaultUnit: string) { const match = input.trim().match(/^([\d.,]+)\s*([a-zA-Z]*)$/); if (!match) return { quantity: NaN, unit: defaultUnit }; let [, num, unit] = match; num = num.replace(',', '.'); unit = unit.toLowerCase() || defaultUnit; const value = parseFloat(num); // Konvertera alltid till defaultUnit if (defaultUnit === 'kg') { if (unit === 'g' || unit === 'gram') return { quantity: value / 1000, unit: 'kg' }; if (unit === 'hg' || unit === 'hektogram') return { quantity: value / 10, unit: 'kg' }; if (unit === 'kg' || unit === 'kilogram' || unit === '') return { quantity: value, unit: 'kg' }; } if (defaultUnit === 'g') { if (unit === 'kg' || unit === 'kilogram') return { quantity: value * 1000, unit: 'g' }; if (unit === 'hg' || unit === 'hektogram') return { quantity: value * 100, unit: 'g' }; if (unit === 'g' || unit === 'gram' || unit === '') return { quantity: value, unit: 'g' }; } // Lägg till fler konverteringar vid behov return { quantity: value, unit: defaultUnit }; } return (
{isOpen && (
{ e.preventDefault(); setError(null); setIsPending(true); const form = e.currentTarget; const formData = new FormData(form); const raw = formData.get('quantity') as string; const unit = formData.get('unit') as string; const { quantity, unit: parsedUnit } = parseQuantityInput(raw, unit); formData.set('quantity', String(quantity)); formData.set('unit', parsedUnit); try { const payload: Record = { productId: Number(formData.get('productId')), quantity, unit: parsedUnit, }; const location = String(formData.get('location') || '').trim(); if (location) payload.location = location; payload.opened = formData.get('opened') === 'on'; const brand = String(formData.get('brand') || '').trim(); if (brand) payload.brand = brand; const suitableFor = String(formData.get('suitableFor') || '').trim(); if (suitableFor) payload.suitableFor = suitableFor; const bestBeforeDate = String(formData.get('bestBeforeDate') || '').trim(); if (bestBeforeDate) payload.bestBeforeDate = bestBeforeDate; const res = await fetch('/api/admin/inventory-item', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }); if (!res.ok) { const data = await res.json().catch(() => ({})); throw new Error(data?.error || 'Kunde inte spara'); } form.reset(); router.refresh(); } catch (err) { setError(err instanceof Error ? err.message : 'Okänt fel'); } finally { setIsPending(false); } }} style={{ display: 'grid', gap: '0.75rem', padding: '1rem', border: '1px solid #ddd', borderTop: 'none', borderRadius: '0 0 8px 8px', marginBottom: '0', }} >

Lägg till hemmavara

{error ?

{error}

: null} )}
); }