diff --git a/frontend/src/components/IngredientList.jsx b/frontend/src/components/IngredientList.jsx index 5e0b850..827fb34 100644 --- a/frontend/src/components/IngredientList.jsx +++ b/frontend/src/components/IngredientList.jsx @@ -1,30 +1,53 @@ -import { useState } from 'react'; +import { useState, useMemo } from 'react'; + +function parseIngredientString(raw) { + if (typeof raw !== 'string') return raw; + const m = raw.match(/^([\d½¼¾⅓⅔.,–\-]+\s*(?:g|kg|ml|l|EL|TL|Stück|Packung|Prise|Bund|Dose|Scheibe|cm|dick)?)\s*(.*)/i); + if (m) { + const amount = m[1].trim(); + const rest = m[2].trim(); + const unitMatch = rest.match(/^((?:g|kg|ml|l|EL|TL|Stück|Packung|Prise|Bund|Dose|Scheibe|dick)s?)\s+(.*)/i); + if (unitMatch) { + return { amount, unit: unitMatch[1], name: unitMatch[2] }; + } + return { amount, unit: '', name: rest }; + } + return { amount: '', unit: '', name: raw }; +} + +function normalizeIngredients(ingredients) { + return ingredients.map(ing => + typeof ing === 'string' ? parseIngredientString(ing) : ing + ); +} export default function IngredientList({ ingredients, editable, onChange }) { const [checked, setChecked] = useState({}); + const normalized = useMemo(() => normalizeIngredients(ingredients), [ingredients]); + const toggle = (i) => setChecked(prev => ({ ...prev, [i]: !prev[i] })); const updateIngredient = (i, field, value) => { - const updated = ingredients.map((ing, idx) => + const updated = normalized.map((ing, idx) => idx === i ? { ...ing, [field]: value } : ing ); onChange(updated); }; const addIngredient = () => { - onChange([...ingredients, { name: '', amount: '', unit: '' }]); + onChange([...normalized, { name: '', amount: '', unit: '' }]); }; const removeIngredient = (i) => { - onChange(ingredients.filter((_, idx) => idx !== i)); + onChange(normalized.filter((_, idx) => idx !== i)); }; return (

Zutaten