'use client'; import { useState, useEffect } from 'react'; import { useRouter, useParams } from 'next/navigation'; import { fetchJson } from '../../../../lib/api'; import type { Product, Recipe } from '../../../../features/inventory/types'; export default function EditRecipePage() { const router = useRouter(); const params = useParams(); const recipeId = Array.isArray(params.id) ? params.id[0] : params.id; const [recipe, setRecipe] = useState({ name: '', description: '', instructions: '', ingredients: [{ productId: 0, quantity: '', unit: '', note: '', location: '' }], }); const [products, setProducts] = useState([]); const [isLoading, setIsLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); const [error, setError] = useState(null); useEffect(() => { const loadData = async () => { try { // Ladda produkter const productsData = await fetchJson('/api/products'); setProducts(productsData); // Ladda receptet const recipeData = await fetchJson(`/api/recipes/${recipeId}`); setRecipe({ name: recipeData.name, description: recipeData.description || '', instructions: recipeData.instructions || '', ingredients: recipeData.ingredients.map((ing: any) => ({ productId: ing.productId, quantity: ing.quantity.toString(), unit: ing.unit, note: ing.note || '', location: ing.location || '', })), }); } catch (err) { setError((err as Error).message); } finally { setIsLoading(false); } }; loadData(); }, [recipeId]); const handleIngredientChange = (index: number, field: string, value: string | number) => { const newIngredients = [...recipe.ingredients]; newIngredients[index] = { ...newIngredients[index], [field]: value }; setRecipe({ ...recipe, ingredients: newIngredients }); }; const addIngredient = () => { setRecipe({ ...recipe, ingredients: [...recipe.ingredients, { productId: 0, quantity: '', unit: '', note: '', location: '' }], }); }; const removeIngredient = (index: number) => { const newIngredients = [...recipe.ingredients]; newIngredients.splice(index, 1); setRecipe({ ...recipe, ingredients: newIngredients }); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsSaving(true); setError(null); // Konvertera quantity till number för varje ingrediens const recipeToSend = { ...recipe, ingredients: recipe.ingredients.map((ing) => ({ ...ing, quantity: Number(ing.quantity), })), }; try { const response = await fetch(`/api/recipes/${recipeId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(recipeToSend), }); if (!response.ok) { throw new Error('Kunde inte uppdatera receptet'); } router.push('/recipes'); } catch (err) { setError((err as Error).message); } finally { setIsSaving(false); } }; const UNIT_OPTIONS = [ { value: '', label: 'Välj enhet' }, { value: 'g', label: 'g (gram)' }, { value: 'kg', label: 'kg (kilogram)' }, { value: 'hg', label: 'hg (hektogram)' }, { value: 'ml', label: 'ml (milliliter)' }, { value: 'dl', label: 'dl (deciliter)' }, { value: 'l', label: 'l (liter)' }, { value: 'st', label: 'st (styck)' }, { value: 'tsk', label: 'tsk (tesked)' }, { value: 'msk', label: 'msk (matsked)' }, ]; const LOCATION_OPTIONS = [ { value: '', label: 'Välj plats' }, { value: 'Kyl', label: 'Kyl' }, { value: 'Frys', label: 'Frys' }, { value: 'Skafferi', label: 'Skafferi' }, { value: 'Annat', label: 'Annat' }, ]; if (isLoading) { return (

Laddar recept...

); } return (

Redigera recept

{error &&

{error}

}