diff --git a/frontend/app/recipes/[id]/edit/page.tsx b/frontend/app/recipes/[id]/edit/page.tsx index 3fdb38c9..f5f355bd 100644 --- a/frontend/app/recipes/[id]/edit/page.tsx +++ b/frontend/app/recipes/[id]/edit/page.tsx @@ -1,544 +1,10 @@ -import { redirect } from 'next/navigation'; - -interface Props { - params: Promise<{ id: string }>; -} - -// Detaljsidan hanterar nu både visning och redigering. -// Omdirigera från gammal /edit-URL till /recipes/[id] -export default async function EditRecipeRedirect({ params }: Props) { - const { id } = await params; - redirect(`/recipes/${id}`); -} - -const MARKDOWN_HELP = ` -**Fetstil:** **text** eller __text__ -*Kursiv:* *text* eller _text_ -• Punktlista: - punkt eller * punkt -# Rubrik 1 -## Rubrik 2 -### Rubrik 3 -`; - -function SimpleMarkdownPreview({ text }: { text: string }) { - const lines = text.split('\n'); - - return ( -
- {lines.map((line, i) => { - // Enkel bearbetning - if (line.startsWith('# ')) { - return

{line.slice(2)}

; - } - if (line.startsWith('## ')) { - return

{line.slice(3)}

; - } - if (line.startsWith('- ') || line.startsWith('* ')) { - return
• {line.slice(2)}
; - } - if (line.trim() === '') { - return
; - } - return
{line}
; - })} -
- ); -} - -export default function EditRecipePage() { - const router = useRouter(); - const params = useParams(); - const recipeId = params && (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 [isDeleting, setIsDeleting] = useState(false); - const [error, setError] = useState(null); - const [showPreview, setShowPreview] = useState(false); - const [showMarkdownHelp, setShowMarkdownHelp] = useState(false); - - useEffect(() => { - const loadData = async () => { - if (!recipeId) { - setError('Receptet hittades inte.'); - setIsLoading(false); - return; - } - - 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(({ location: _loc, ...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) { - const errorMessage = await parseErrorResponse(response); - throw new Error(errorMessage); - } - - router.push('/recipes'); - } catch (err) { - const message = err instanceof Error ? err.message : 'Ett okänt fel inträffade. Försök igen.'; - setError(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)' }, - { value: 'port', label: 'port (portioner)' }, - { value: 'efter smak', label: 'Efter smak' }, - { value: 'förp', label: 'förp (förpackning)' }, - { value: 'klyfta', label: 'klyfta' }, - ]; - - 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}

} - -
- {/* Receptdetaljer */} -
-

Receptdetaljer

- -
- - setRecipe({ ...recipe, name: e.target.value })} - required - style={{ - width: '100%', - padding: '0.75rem', - border: '1px solid #ddd', - borderRadius: '4px', - fontSize: '1rem', - minHeight: '44px', - boxSizing: 'border-box', - }} - /> -
- -
- -