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
@@ -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