feat: Rezepte beim Zuordnen anlegen und offene Zutaten listen
Lebensmittel oder Rezept wählbar; gemappte Zutaten werden übernommen, offene erscheinen in der Liste. Dazu Fettgehalt-Suche, EPA-Stoffe, Rezept-CRUD und Wechsel bestehender Zuordnungen. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
132a364a3a
commit
a7e5511039
26 files changed
+1234
-147
No files matched your search
@@ -0,0 +1,142 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
|
||||
function parseNum(raw) {
|
||||
const n = parseFloat(String(raw ?? '').replace(',', '.'))
|
||||
return Number.isFinite(n) ? n : 0
|
||||
}
|
||||
|
||||
export function foodCreatePayload(name, macros, extras, servingG) {
|
||||
const attributes = {}
|
||||
for (const row of extras || []) {
|
||||
if (!row?.attr?.attr_key || row.value === '' || row.value == null) continue
|
||||
attributes[row.attr.attr_key] = parseNum(row.value)
|
||||
}
|
||||
const serving = parseNum(servingG)
|
||||
return {
|
||||
name_de: (name || '').trim(),
|
||||
macros_per_100g: {
|
||||
kcal: parseNum(macros.kcal),
|
||||
protein_g: parseNum(macros.protein_g),
|
||||
fat_g: parseNum(macros.fat_g),
|
||||
carbs_g: parseNum(macros.carbs_g),
|
||||
},
|
||||
attributes,
|
||||
serving_g: serving > 0 && serving !== 100 ? serving : null,
|
||||
}
|
||||
}
|
||||
|
||||
export default function FoodNutrientFields({ macros, setMacros, extras, setExtras, servingG, setServingG }) {
|
||||
const [q, setQ] = useState('')
|
||||
const [hits, setHits] = useState([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const timer = useRef(null)
|
||||
|
||||
useEffect(() => () => clearTimeout(timer.current), [])
|
||||
|
||||
const search = (term) => {
|
||||
clearTimeout(timer.current)
|
||||
const query = (term || '').trim()
|
||||
if (query.length < 2) {
|
||||
setHits([])
|
||||
return
|
||||
}
|
||||
timer.current = setTimeout(async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
setHits(await api.listFoodAttributes(query, 20))
|
||||
} catch {
|
||||
setHits([])
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, 250)
|
||||
}
|
||||
|
||||
const add = (attr) => {
|
||||
setExtras((list) => (
|
||||
list.some((r) => r.attr.attr_key === attr.attr_key) ? list : [...list, { attr, value: '' }]
|
||||
))
|
||||
setQ('')
|
||||
setHits([])
|
||||
}
|
||||
|
||||
const setValue = (key, value) => {
|
||||
setExtras((list) => list.map((r) => (r.attr.attr_key === key ? { ...r, value } : r)))
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<p style={{ fontSize: 12, color: 'var(--text3)', margin: '0 0 8px' }}>
|
||||
Zahlen in der Einheit des Stoffs. Bei Etikett „pro 8 ml / 8 g“ die Portionsgröße setzen — gespeichert wird immer pro 100 g.
|
||||
</p>
|
||||
<label style={{ display: 'block', fontSize: 12, color: 'var(--text2)', marginBottom: 8 }}>
|
||||
Angabe gilt für{' '}
|
||||
<input
|
||||
className="form-input"
|
||||
type="number"
|
||||
min="0.1"
|
||||
step="0.1"
|
||||
style={{ width: 80, display: 'inline-block', textAlign: 'left' }}
|
||||
value={servingG}
|
||||
onChange={(e) => setServingG(e.target.value)}
|
||||
/>
|
||||
{' '}g (100 = pro 100 g)
|
||||
</label>
|
||||
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 }}>
|
||||
{[['kcal', 'kcal'], ['protein_g', 'Protein g'], ['fat_g', 'Fett g'], ['carbs_g', 'Kohlenhydrate g']].map(([key, label]) => (
|
||||
<label key={key} style={{ fontSize: 12, color: 'var(--text2)' }}>
|
||||
{label}
|
||||
<input
|
||||
className="form-input"
|
||||
type="number"
|
||||
min="0"
|
||||
step="0.1"
|
||||
style={{ width: '100%', textAlign: 'left', marginTop: 4 }}
|
||||
value={macros[key]}
|
||||
onChange={(e) => setMacros({ ...macros, [key]: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
<p style={{ fontSize: 12, fontWeight: 600, margin: '12px 0 6px' }}>Weitere Stoffe (EPA, DHA, Ballaststoffe, …)</p>
|
||||
<input
|
||||
className="form-input"
|
||||
style={{ width: '100%', textAlign: 'left' }}
|
||||
placeholder="Stoff suchen, z. B. EPA oder Omega"
|
||||
value={q}
|
||||
onChange={(e) => { setQ(e.target.value); search(e.target.value) }}
|
||||
/>
|
||||
{loading && <p style={{ fontSize: 12, color: 'var(--text3)' }}>Suche…</p>}
|
||||
{hits.map((h) => (
|
||||
<button
|
||||
key={h.attr_key}
|
||||
type="button"
|
||||
className="btn btn-secondary btn-full"
|
||||
style={{ marginTop: 6, justifyContent: 'flex-start', textAlign: 'left', height: 'auto', padding: '8px 10px' }}
|
||||
onClick={() => add(h)}
|
||||
>
|
||||
<span>
|
||||
<strong style={{ display: 'block' }}>{h.name_de}</strong>
|
||||
<span style={{ fontSize: 11, color: 'var(--text3)' }}>{h.attr_key}{h.unit ? ` · ${h.unit}` : ''}{h.category ? ` · ${h.category}` : ''}</span>
|
||||
</span>
|
||||
</button>
|
||||
))}
|
||||
{extras.map((row) => (
|
||||
<label key={row.attr.attr_key} style={{ display: 'block', fontSize: 12, color: 'var(--text2)', marginTop: 8 }}>
|
||||
{row.attr.name_de}{row.attr.unit ? ` (${row.attr.unit})` : ''}
|
||||
<input
|
||||
className="form-input"
|
||||
type="number"
|
||||
min="0"
|
||||
step="0.001"
|
||||
style={{ width: '100%', textAlign: 'left', marginTop: 4 }}
|
||||
value={row.value}
|
||||
onChange={(e) => setValue(row.attr.attr_key, e.target.value)}
|
||||
placeholder="Wert in der Einheit oben"
|
||||
/>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
import { useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
|
||||
function emptyIng() {
|
||||
return { source_name_raw: '', quantity_g: '' }
|
||||
}
|
||||
|
||||
function mappingHint(name, learned) {
|
||||
const n = (name || '').trim().toLowerCase()
|
||||
if (!n) return null
|
||||
return (learned || []).find((m) => {
|
||||
const raw = (m.source_name_raw || '').toLowerCase()
|
||||
const norm = (m.source_name_normalized || '').toLowerCase()
|
||||
return raw === n || norm === n || raw.includes(n) || n.includes(norm)
|
||||
}) || null
|
||||
}
|
||||
|
||||
export function RecipeForm({ defaultName, defaultPortions, defaultIngredients, learned, disabled, submitLabel, onSave, onCancel }) {
|
||||
const [name, setName] = useState(defaultName || '')
|
||||
const [portions, setPortions] = useState(defaultPortions != null ? String(defaultPortions) : '1')
|
||||
const [ings, setIngs] = useState(
|
||||
(defaultIngredients || []).length
|
||||
? defaultIngredients.map((i) => ({ source_name_raw: i.source_name_raw || '', quantity_g: i.quantity_g ?? '' }))
|
||||
: [emptyIng()]
|
||||
)
|
||||
const [error, setError] = useState(null)
|
||||
const [saving, setSaving] = useState(false)
|
||||
|
||||
const save = async () => {
|
||||
const ingredients = ings
|
||||
.map((i) => ({
|
||||
source_name_raw: (i.source_name_raw || '').trim(),
|
||||
quantity_g: i.quantity_g === '' ? null : parseFloat(String(i.quantity_g).replace(',', '.')),
|
||||
}))
|
||||
.filter((i) => i.source_name_raw)
|
||||
if (!name.trim()) {
|
||||
setError('Rezeptname fehlt')
|
||||
return
|
||||
}
|
||||
if (!ingredients.length) {
|
||||
setError('Mindestens eine Zutat angeben')
|
||||
return
|
||||
}
|
||||
setSaving(true)
|
||||
setError(null)
|
||||
try {
|
||||
await onSave({
|
||||
name_raw: name.trim(),
|
||||
portions: parseFloat(String(portions).replace(',', '.')) || 1,
|
||||
ingredients,
|
||||
})
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={{ marginTop: 8, padding: 10, background: 'var(--surface2)', borderRadius: 8 }}>
|
||||
<p style={{ fontSize: 12, margin: '0 0 8px', color: 'var(--text2)' }}>
|
||||
Zutaten, die schon zugeordnet sind, werden übernommen. Offene erscheinen danach in der Liste zum Zuordnen.
|
||||
</p>
|
||||
{error && <div style={{ color: 'var(--danger)', fontSize: 13, marginBottom: 8 }}>{error}</div>}
|
||||
<input className="form-input" style={{ width: '100%', textAlign: 'left', marginBottom: 8 }} placeholder="Rezeptname" value={name} onChange={(e) => setName(e.target.value)} />
|
||||
<label style={{ fontSize: 12, color: 'var(--text2)' }}>
|
||||
Portionen
|
||||
<input className="form-input" type="number" min="0.1" step="0.1" style={{ width: 80, textAlign: 'left', marginLeft: 8 }} value={portions} onChange={(e) => setPortions(e.target.value)} />
|
||||
</label>
|
||||
<p style={{ fontSize: 12, margin: '10px 0 6px', color: 'var(--text2)' }}>Zutaten (Name + Gramm)</p>
|
||||
{ings.map((ing, i) => {
|
||||
const mapped = mappingHint(ing.source_name_raw, learned)
|
||||
return (
|
||||
<div key={i} style={{ marginBottom: 8 }}>
|
||||
<div style={{ display: 'grid', gridTemplateColumns: '1fr 80px', gap: 6 }}>
|
||||
<input className="form-input" style={{ textAlign: 'left' }} placeholder="z. B. Haferflocken" value={ing.source_name_raw} onChange={(e) => setIngs((list) => list.map((x, j) => j === i ? { ...x, source_name_raw: e.target.value } : x))} />
|
||||
<input className="form-input" type="number" min="0" step="0.1" style={{ textAlign: 'left' }} placeholder="g" value={ing.quantity_g} onChange={(e) => setIngs((list) => list.map((x, j) => j === i ? { ...x, quantity_g: e.target.value } : x))} />
|
||||
</div>
|
||||
{ing.source_name_raw.trim() && (
|
||||
<div style={{ fontSize: 11, color: mapped ? 'var(--accent-dark)' : 'var(--text3)', marginTop: 2 }}>
|
||||
{mapped ? `zugeordnet: ${mapped.food_name_de || mapped.source_name_raw}` : 'noch offen — nach dem Speichern in der Liste zuordnen'}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
<button type="button" className="btn btn-secondary" style={{ fontSize: 12 }} onClick={() => setIngs((list) => [...list, emptyIng()])}>Zutat hinzufügen</button>
|
||||
<div style={{ display: 'flex', gap: 8, marginTop: 10 }}>
|
||||
{onCancel && (
|
||||
<button type="button" className="btn btn-secondary" disabled={saving} onClick={onCancel}>Zurück</button>
|
||||
)}
|
||||
<button type="button" className="btn btn-primary" style={{ flex: 1 }} disabled={disabled || saving} onClick={save}>
|
||||
{saving ? 'Speichere…' : (submitLabel || 'Rezept speichern')}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function FoodRecipeEditor({ recipes, learned, onChanged }) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [editing, setEditing] = useState(null)
|
||||
const [draft, setDraft] = useState(null)
|
||||
const [error, setError] = useState(null)
|
||||
|
||||
const startNew = () => {
|
||||
setEditing('new')
|
||||
setDraft({ name: '', portions: '1' })
|
||||
setError(null)
|
||||
setOpen(true)
|
||||
}
|
||||
|
||||
const startEdit = (r) => {
|
||||
setEditing(r)
|
||||
setDraft(r)
|
||||
setError(null)
|
||||
setOpen(true)
|
||||
}
|
||||
|
||||
const save = async (body) => {
|
||||
if (editing === 'new') await api.createNutritionRecipe(body)
|
||||
else await api.updateNutritionRecipe(editing.id, body)
|
||||
setEditing(null)
|
||||
setDraft(null)
|
||||
onChanged?.()
|
||||
}
|
||||
|
||||
const remove = async (id) => {
|
||||
if (!confirm('Rezept wirklich löschen?')) return
|
||||
try {
|
||||
await api.deleteNutritionRecipe(id)
|
||||
if (editing && editing !== 'new' && editing.id === id) setEditing(null)
|
||||
onChanged?.()
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={{ marginTop: 16 }}>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 8 }}>
|
||||
<h3 style={{ fontSize: 14, margin: 0 }}>Eigene Rezepte / Listen ({recipes.length})</h3>
|
||||
<button type="button" className="btn btn-secondary" style={{ fontSize: 12 }} onClick={() => { setOpen((v) => !v); if (open) setEditing(null) }}>
|
||||
{open ? 'Einklappen' : 'Anzeigen'}
|
||||
</button>
|
||||
</div>
|
||||
{open && (
|
||||
<div style={{ marginTop: 8 }}>
|
||||
{error && <div style={{ color: 'var(--danger)', fontSize: 13, marginBottom: 8 }}>{error}</div>}
|
||||
<button type="button" className="btn btn-secondary btn-full" onClick={startNew}>Rezept anlegen</button>
|
||||
{recipes.map((r) => (
|
||||
<div key={r.id} style={{ borderTop: '1px solid var(--border)', padding: '8px 0', marginTop: 8 }}>
|
||||
<div style={{ fontWeight: 600 }}>{r.name_raw}</div>
|
||||
<div style={{ fontSize: 12, color: 'var(--text3)' }}>
|
||||
{(r.ingredients || []).length} Zutaten · {r.portions || 1} Portionen
|
||||
{r.source === 'manual' ? ' · selbst angelegt' : ''}
|
||||
</div>
|
||||
<div style={{ display: 'flex', gap: 8, marginTop: 6 }}>
|
||||
<button type="button" className="btn btn-secondary" style={{ fontSize: 12 }} onClick={() => startEdit(r)}>Bearbeiten</button>
|
||||
<button type="button" className="btn btn-secondary" style={{ fontSize: 12 }} onClick={() => remove(r.id)}>Löschen</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{editing === 'new' && (
|
||||
<RecipeForm learned={learned} submitLabel="Rezept anlegen" onSave={save} onCancel={() => setEditing(null)} />
|
||||
)}
|
||||
{editing && editing !== 'new' && (
|
||||
<RecipeForm
|
||||
key={editing.id}
|
||||
defaultName={draft?.name_raw}
|
||||
defaultPortions={draft?.portions}
|
||||
defaultIngredients={draft?.ingredients}
|
||||
learned={learned}
|
||||
submitLabel="Rezept speichern"
|
||||
onSave={save}
|
||||
onCancel={() => setEditing(null)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
import FoodNutrientFields, { foodCreatePayload } from './FoodNutrientFields'
|
||||
import { RecipeForm } from './FoodRecipeEditor'
|
||||
|
||||
const COUNT_UNITS = new Set(['stück', 'scheibe', 'portion', 'becher', 'tasse'])
|
||||
const UNIT_LABEL = {
|
||||
@@ -24,17 +26,20 @@ function detectUnit(...texts) {
|
||||
return null
|
||||
}
|
||||
|
||||
export default function FoodSearchModal({ title, initialQuery, quantityHint, onSelect, onClose }) {
|
||||
export default function FoodSearchModal({ title, initialQuery, quantityHint, allowRecipe, learned, onSelect, onCreateRecipe, onClose }) {
|
||||
const [q, setQ] = useState(initialQuery || '')
|
||||
const [hits, setHits] = useState([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState(null)
|
||||
const [creating, setCreating] = useState(false)
|
||||
const [showCreate, setShowCreate] = useState(false)
|
||||
const [createKind, setCreateKind] = useState(null)
|
||||
const [manual, setManual] = useState({
|
||||
name_de: initialQuery || '',
|
||||
kcal: '', protein_g: '', fat_g: '', carbs_g: '',
|
||||
})
|
||||
const [nutrientExtras, setNutrientExtras] = useState([])
|
||||
const [servingG, setServingG] = useState('100')
|
||||
const suggestedUnit = detectUnit(quantityHint, initialQuery)
|
||||
const [gramsPerUnit, setGramsPerUnit] = useState(
|
||||
suggestedUnit === 'el' ? '15' : suggestedUnit === 'tl' ? '5' : ''
|
||||
@@ -44,7 +49,7 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, onS
|
||||
const abortRef = useRef(null)
|
||||
const seqRef = useRef(0)
|
||||
|
||||
const extras = () => {
|
||||
const unitExtras = () => {
|
||||
const g = parseFloat(String(gramsPerUnit).replace(',', '.'))
|
||||
if (!suggestedUnit || !g || g <= 0) return {}
|
||||
return { grams_per_unit: g, source_unit: suggestedUnit }
|
||||
@@ -111,16 +116,8 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, onS
|
||||
setCreating(true)
|
||||
setError(null)
|
||||
try {
|
||||
const food = await api.createUserFood({
|
||||
name_de: name,
|
||||
macros_per_100g: {
|
||||
kcal: parseFloat(String(manual.kcal).replace(',', '.')) || 0,
|
||||
protein_g: parseFloat(String(manual.protein_g).replace(',', '.')) || 0,
|
||||
fat_g: parseFloat(String(manual.fat_g).replace(',', '.')) || 0,
|
||||
carbs_g: parseFloat(String(manual.carbs_g).replace(',', '.')) || 0,
|
||||
},
|
||||
})
|
||||
onSelect(food, extras())
|
||||
const food = await api.createUserFood(foodCreatePayload(name, manual, nutrientExtras, servingG))
|
||||
onSelect(food, unitExtras())
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
} finally {
|
||||
@@ -160,12 +157,12 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, onS
|
||||
ref={inputRef}
|
||||
className="form-input"
|
||||
style={{ width: '100%', textAlign: 'left' }}
|
||||
placeholder="Name eingeben, z. B. Haferflocken"
|
||||
placeholder="Name und Fettgehalt, z. B. Joghurt 10%"
|
||||
value={q}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
<p style={{ fontSize: 12, color: 'var(--text3)', margin: '8px 0 0' }}>
|
||||
Suche nach dem Namen. Fehlt der Treffer, legst du unten einen eigenen Eintrag an.
|
||||
Fettgehalt mit eingeben, z. B. Joghurt 10 % oder 9,5 %. Ohne Zahl erscheint oft nur die 3,5 %-Klasse. Fehlt der Treffer, eigenen Eintrag anlegen.
|
||||
</p>
|
||||
{suggestedUnit && (
|
||||
<label style={{ display: 'block', marginTop: 10, fontSize: 13 }}>
|
||||
@@ -196,7 +193,7 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, onS
|
||||
type="button"
|
||||
className="btn btn-secondary btn-full"
|
||||
style={{ marginTop: 8, justifyContent: 'flex-start', textAlign: 'left', height: 'auto', padding: '10px 12px' }}
|
||||
onClick={() => onSelect(h, extras())}
|
||||
onClick={() => onSelect(h, unitExtras())}
|
||||
>
|
||||
<span>
|
||||
<strong style={{ display: 'block' }}>{h.name_de}</strong>
|
||||
@@ -213,45 +210,62 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, onS
|
||||
{!showCreate ? (
|
||||
<button type="button" className="btn btn-secondary btn-full" onClick={() => {
|
||||
setShowCreate(true)
|
||||
setCreateKind(allowRecipe && onCreateRecipe ? null : 'food')
|
||||
setManual((s) => ({ ...s, name_de: s.name_de || q || initialQuery || '' }))
|
||||
}}>
|
||||
Eigenes Lebensmittel anlegen
|
||||
{allowRecipe && onCreateRecipe ? 'Lebensmittel oder Rezept anlegen' : 'Eigenes Lebensmittel anlegen'}
|
||||
</button>
|
||||
) : !createKind ? (
|
||||
<div>
|
||||
<p style={{ fontSize: 13, fontWeight: 600, marginBottom: 8 }}>Was möchtest du anlegen?</p>
|
||||
<p style={{ fontSize: 12, color: 'var(--text2)', margin: '0 0 8px' }}>Lebensmittel = ein Katalogeintrag. Rezept = mehrere Zutaten; offene Zutaten erscheinen danach in der Liste.</p>
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8 }}>
|
||||
<button type="button" className="btn btn-primary" onClick={() => setCreateKind('food')}>Lebensmittel</button>
|
||||
<button type="button" className="btn btn-secondary" onClick={() => setCreateKind('recipe')}>Rezept</button>
|
||||
<button type="button" className="btn btn-secondary" onClick={() => { setShowCreate(false); setCreateKind(null) }}>Zurück</button>
|
||||
</div>
|
||||
</div>
|
||||
) : createKind === 'recipe' ? (
|
||||
<RecipeForm
|
||||
defaultName={manual.name_de || q || initialQuery || ''}
|
||||
learned={learned}
|
||||
disabled={creating}
|
||||
submitLabel="Rezept anlegen und zuordnen"
|
||||
onCancel={() => setCreateKind(null)}
|
||||
onSave={onCreateRecipe}
|
||||
/>
|
||||
) : (
|
||||
<div>
|
||||
<p style={{ fontSize: 13, fontWeight: 600, marginBottom: 8 }}>Eigener Eintrag (pro 100 g)</p>
|
||||
<p style={{ fontSize: 13, fontWeight: 600, marginBottom: 8 }}>Eigener Eintrag</p>
|
||||
<input
|
||||
className="form-input"
|
||||
style={{ width: '100%', textAlign: 'left', marginBottom: 8 }}
|
||||
placeholder="Name"
|
||||
placeholder="Name, z. B. Norsan Omega-3 Algenöl"
|
||||
value={manual.name_de}
|
||||
onChange={(e) => setManual({ ...manual, name_de: e.target.value })}
|
||||
/>
|
||||
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 }}>
|
||||
{[['kcal', 'kcal'], ['protein_g', 'Protein g'], ['fat_g', 'Fett g'], ['carbs_g', 'Kohlenhydrate g']].map(([key, label]) => (
|
||||
<label key={key} style={{ fontSize: 12, color: 'var(--text2)' }}>
|
||||
{label}
|
||||
<input
|
||||
className="form-input"
|
||||
type="number"
|
||||
min="0"
|
||||
step="0.1"
|
||||
style={{ width: '100%', textAlign: 'left', marginTop: 4 }}
|
||||
value={manual[key]}
|
||||
onChange={(e) => setManual({ ...manual, [key]: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
))}
|
||||
<FoodNutrientFields
|
||||
macros={manual}
|
||||
setMacros={setManual}
|
||||
extras={nutrientExtras}
|
||||
setExtras={setNutrientExtras}
|
||||
servingG={servingG}
|
||||
setServingG={setServingG}
|
||||
/>
|
||||
<div style={{ display: 'flex', gap: 8, marginTop: 10 }}>
|
||||
{allowRecipe && onCreateRecipe && (
|
||||
<button type="button" className="btn btn-secondary" disabled={creating} onClick={() => setCreateKind(null)}>Zurück</button>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary"
|
||||
style={{ flex: 1 }}
|
||||
disabled={creating}
|
||||
onClick={createManual}
|
||||
>
|
||||
{creating ? 'Speichere…' : 'Anlegen und zuordnen'}
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary btn-full"
|
||||
style={{ marginTop: 10 }}
|
||||
disabled={creating}
|
||||
onClick={createManual}
|
||||
>
|
||||
{creating ? 'Speichere…' : 'Anlegen und zuordnen'}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
import FoodSearchModal from './FoodSearchModal'
|
||||
import FoodNutrientFields, { foodCreatePayload } from './FoodNutrientFields'
|
||||
import FoodRecipeEditor, { RecipeForm } from './FoodRecipeEditor'
|
||||
|
||||
const PERIODS = [
|
||||
{ days: 14, label: '14 Tage' },
|
||||
@@ -72,41 +74,61 @@ function RecipePickModal({ recipes, sourceName, onPick, onClose }) {
|
||||
)
|
||||
}
|
||||
|
||||
function InlineCreate({ defaultName, disabled, onCreate }) {
|
||||
function InlineCreate({ defaultName, disabled, learned, allowRecipe, onCreateFood, onCreateRecipe }) {
|
||||
const [kind, setKind] = useState(allowRecipe ? null : 'food')
|
||||
const [name, setName] = useState(defaultName || '')
|
||||
const [kcal, setKcal] = useState('0')
|
||||
const [protein, setProtein] = useState('0')
|
||||
const [fat, setFat] = useState('0')
|
||||
const [carbs, setCarbs] = useState('0')
|
||||
const [macros, setMacros] = useState({ kcal: '0', protein_g: '0', fat_g: '0', carbs_g: '0' })
|
||||
const [extras, setExtras] = useState([])
|
||||
const [servingG, setServingG] = useState('100')
|
||||
if (!kind) {
|
||||
return (
|
||||
<div style={{ marginTop: 8, padding: 10, background: 'var(--surface2)', borderRadius: 8 }}>
|
||||
<p style={{ fontSize: 12, margin: '0 0 8px', color: 'var(--text2)' }}>Lebensmittel (ein Eintrag im Katalog) oder Rezept (mehrere Zutaten)?</p>
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8 }}>
|
||||
<button type="button" className="btn btn-primary" disabled={disabled} onClick={() => setKind('food')}>Lebensmittel</button>
|
||||
{allowRecipe && (
|
||||
<button type="button" className="btn btn-secondary" disabled={disabled} onClick={() => setKind('recipe')}>Rezept</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
if (kind === 'recipe') {
|
||||
return (
|
||||
<RecipeForm
|
||||
defaultName={defaultName}
|
||||
learned={learned}
|
||||
disabled={disabled}
|
||||
submitLabel="Rezept anlegen und zuordnen"
|
||||
onCancel={() => setKind(null)}
|
||||
onSave={onCreateRecipe}
|
||||
/>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<div style={{ marginTop: 8, padding: 10, background: 'var(--surface2)', borderRadius: 8 }}>
|
||||
<p style={{ fontSize: 12, margin: '0 0 8px', color: 'var(--text2)' }}>Neuer Katalogeintrag (Werte pro 100 g, Salz z. B. alles 0)</p>
|
||||
<p style={{ fontSize: 12, margin: '0 0 8px', color: 'var(--text2)' }}>Neuer Katalogeintrag. Salz: Makros 0. Öl/Supplement: Stoffe wie EPA dazusuchen.</p>
|
||||
<input className="form-input" style={{ width: '100%', textAlign: 'left', marginBottom: 8 }} value={name} onChange={(e) => setName(e.target.value)} placeholder="Name" />
|
||||
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 6 }}>
|
||||
{[['kcal', kcal, setKcal], ['Protein', protein, setProtein], ['Fett', fat, setFat], ['KH', carbs, setCarbs]].map(([label, val, set]) => (
|
||||
<label key={label} style={{ fontSize: 11, color: 'var(--text3)' }}>
|
||||
{label}
|
||||
<input className="form-input" type="number" min="0" step="0.1" style={{ width: '100%', textAlign: 'left', marginTop: 2 }} value={val} onChange={(e) => set(e.target.value)} />
|
||||
</label>
|
||||
))}
|
||||
<FoodNutrientFields
|
||||
macros={macros}
|
||||
setMacros={setMacros}
|
||||
extras={extras}
|
||||
setExtras={setExtras}
|
||||
servingG={servingG}
|
||||
setServingG={setServingG}
|
||||
/>
|
||||
<div style={{ display: 'flex', gap: 8, marginTop: 8 }}>
|
||||
<button type="button" className="btn btn-secondary" disabled={disabled} onClick={() => setKind(null)}>Zurück</button>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary"
|
||||
style={{ flex: 1 }}
|
||||
disabled={disabled || !name.trim()}
|
||||
onClick={() => onCreateFood(foodCreatePayload(name, macros, extras, servingG))}
|
||||
>
|
||||
Anlegen und zuordnen
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary btn-full"
|
||||
style={{ marginTop: 8 }}
|
||||
disabled={disabled || !name.trim()}
|
||||
onClick={() => onCreate({
|
||||
name_de: name.trim(),
|
||||
macros_per_100g: {
|
||||
kcal: parseFloat(String(kcal).replace(',', '.')) || 0,
|
||||
protein_g: parseFloat(String(protein).replace(',', '.')) || 0,
|
||||
fat_g: parseFloat(String(fat).replace(',', '.')) || 0,
|
||||
carbs_g: parseFloat(String(carbs).replace(',', '.')) || 0,
|
||||
},
|
||||
})}
|
||||
>
|
||||
Anlegen und zuordnen
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -243,13 +265,33 @@ export default function NutritionFoodMap({ onChanged, onMapped }) {
|
||||
}
|
||||
}
|
||||
|
||||
const createRecipeAndAssign = async (row, body) => {
|
||||
setSaving(row.source_name_raw)
|
||||
setError(null)
|
||||
try {
|
||||
const rec = await api.createNutritionRecipe({ ...body, source: 'manual' })
|
||||
await api.applyNutritionRecipe(rec.id, row.source_name_raw)
|
||||
setCreateFor(null)
|
||||
setSearchFor(null)
|
||||
setNotice(`Rezept angelegt. Offene Zutaten erscheinen in der Liste.`)
|
||||
await load()
|
||||
onMapped?.()
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
throw e
|
||||
} finally {
|
||||
setSaving(null)
|
||||
}
|
||||
}
|
||||
|
||||
const applyRecipe = async (sourceName, recipeId) => {
|
||||
setSaving(sourceName)
|
||||
setError(null)
|
||||
try {
|
||||
await api.applyNutritionRecipe(recipeId, sourceName)
|
||||
setRecipeFor(null)
|
||||
setUnmapped((list) => list.filter((x) => x.source_name_raw !== sourceName))
|
||||
setNotice('Rezept verknüpft. Offene Zutaten erscheinen in der Liste.')
|
||||
await load()
|
||||
onMapped?.()
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
@@ -331,7 +373,7 @@ export default function NutritionFoodMap({ onChanged, onMapped }) {
|
||||
<button type="button" className="btn btn-secondary btn-full" disabled={importing} onClick={() => listRef.current?.click()}>
|
||||
{importing ? 'Importiere Listen…' : 'FDDB-Listen / Rezepte importieren'}
|
||||
</button>
|
||||
{recipes.length > 0 && <p style={{ fontSize: 12, color: 'var(--text3)', marginTop: 8 }}>{recipes.length} eigene Listen geladen</p>}
|
||||
<FoodRecipeEditor recipes={recipes} learned={learned} onChanged={() => { load(); onChanged?.() }} />
|
||||
<input ref={bundleRef} type="file" accept=".json,application/json" style={{ display: 'none' }} onChange={(e) => { const f = e.target.files?.[0]; e.target.value = ''; if (f) importBundle(f) }} />
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginTop: 8 }}>
|
||||
<button type="button" className="btn btn-secondary" disabled={busy} onClick={exportBundle}>Zuordnungen & Listen exportieren</button>
|
||||
@@ -413,7 +455,7 @@ export default function NutritionFoodMap({ onChanged, onMapped }) {
|
||||
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginTop: 8 }}>
|
||||
<button type="button" className="btn btn-secondary" disabled={busyRow} onClick={() => setCreateFor(createFor === key ? null : key)}>
|
||||
{createFor === key ? 'Anlegen schließen' : 'Neu anlegen'}
|
||||
{createFor === key ? 'Anlegen schließen' : (u.kind === 'recipe_ingredient' ? 'Neu anlegen' : 'Lebensmittel oder Rezept')}
|
||||
</button>
|
||||
<button type="button" className="btn btn-secondary" disabled={busyRow} onClick={() => setSearchFor(u)}>Mehr suchen</button>
|
||||
{u.kind !== 'recipe_ingredient' && u.matching_recipe_id && (
|
||||
@@ -424,7 +466,14 @@ export default function NutritionFoodMap({ onChanged, onMapped }) {
|
||||
)}
|
||||
</div>
|
||||
{createFor === key && (
|
||||
<InlineCreate defaultName={suggestQuery(u.source_name_raw) || u.source_name_raw} disabled={busyRow} onCreate={(body) => createAndAssign(u, body)} />
|
||||
<InlineCreate
|
||||
defaultName={suggestQuery(u.source_name_raw) || u.source_name_raw}
|
||||
disabled={busyRow}
|
||||
learned={learned}
|
||||
allowRecipe={u.kind !== 'recipe_ingredient'}
|
||||
onCreateFood={(body) => createAndAssign(u, body)}
|
||||
onCreateRecipe={(body) => createRecipeAndAssign(u, body)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
@@ -459,8 +508,11 @@ export default function NutritionFoodMap({ onChanged, onMapped }) {
|
||||
title={`Suchen: ${searchFor.source_name_raw}`}
|
||||
initialQuery={suggestQuery(searchFor.source_name_raw)}
|
||||
quantityHint={searchFor.sample_quantity_raw || searchFor.source_name_raw}
|
||||
allowRecipe={searchFor.kind !== 'recipe_ingredient'}
|
||||
learned={learned}
|
||||
onClose={() => setSearchFor(null)}
|
||||
onSelect={(food, extras) => assign(searchFor, food.id, extras, food)}
|
||||
onCreateRecipe={searchFor.kind === 'recipe_ingredient' ? undefined : (body) => createRecipeAndAssign(searchFor, body)}
|
||||
/>
|
||||
)}
|
||||
{recipeFor && (
|
||||
|
||||
Reference in new issue
Block a user