fix ki modell
This commit is contained in:
@@ -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 }) {
|
export default function IngredientList({ ingredients, editable, onChange }) {
|
||||||
const [checked, setChecked] = useState({});
|
const [checked, setChecked] = useState({});
|
||||||
|
|
||||||
|
const normalized = useMemo(() => normalizeIngredients(ingredients), [ingredients]);
|
||||||
|
|
||||||
const toggle = (i) => setChecked(prev => ({ ...prev, [i]: !prev[i] }));
|
const toggle = (i) => setChecked(prev => ({ ...prev, [i]: !prev[i] }));
|
||||||
|
|
||||||
const updateIngredient = (i, field, value) => {
|
const updateIngredient = (i, field, value) => {
|
||||||
const updated = ingredients.map((ing, idx) =>
|
const updated = normalized.map((ing, idx) =>
|
||||||
idx === i ? { ...ing, [field]: value } : ing
|
idx === i ? { ...ing, [field]: value } : ing
|
||||||
);
|
);
|
||||||
onChange(updated);
|
onChange(updated);
|
||||||
};
|
};
|
||||||
|
|
||||||
const addIngredient = () => {
|
const addIngredient = () => {
|
||||||
onChange([...ingredients, { name: '', amount: '', unit: '' }]);
|
onChange([...normalized, { name: '', amount: '', unit: '' }]);
|
||||||
};
|
};
|
||||||
|
|
||||||
const removeIngredient = (i) => {
|
const removeIngredient = (i) => {
|
||||||
onChange(ingredients.filter((_, idx) => idx !== i));
|
onChange(normalized.filter((_, idx) => idx !== i));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h3 className="font-semibold text-lg mb-3">Zutaten</h3>
|
<h3 className="font-semibold text-lg mb-3">Zutaten</h3>
|
||||||
<ul className="space-y-2">
|
<ul className="space-y-2">
|
||||||
{ingredients.map((ing, i) => (
|
{normalized.map((ing, i) => (
|
||||||
<li key={i} className="flex items-center gap-2">
|
<li key={i} className="flex items-center gap-2">
|
||||||
{!editable && (
|
{!editable && (
|
||||||
<input
|
<input
|
||||||
|
|||||||
Reference in New Issue
Block a user