fix: Listen in der Tagesuebersicht und Katalog-Makros uebernehmen
Tagebuchzeilen mit Liste gelten als zugeordnet. Berechnete Zutaten-Makros lassen sich am Tag in nutrition_log schreiben. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
44e3e216ab
commit
95453d548a
10 files changed
+221
-28
No files matched your search
@@ -197,25 +197,63 @@ function EntryForm({ onSaved }) {
|
||||
}
|
||||
|
||||
// ── Data Tab (Editable Entry List) ───────────────────────────────────────────
|
||||
function DayItems({ date }) {
|
||||
function itemAssignment(it) {
|
||||
if (it.food_name_de) return { ok: true, text: `→ ${it.food_name_de}` }
|
||||
if (it.recipe_name_de || it.recipe_id) {
|
||||
const name = it.recipe_name_de || 'Liste'
|
||||
if (it.recipe_complete) return { ok: true, text: `→ Liste: ${name}` }
|
||||
return { ok: false, text: `→ Liste: ${name} (Zutaten noch offen)` }
|
||||
}
|
||||
return { ok: false, text: '· noch nicht zugeordnet' }
|
||||
}
|
||||
|
||||
function formatMacros(m) {
|
||||
if (!m) return '—'
|
||||
return `${Math.round(m.kcal || 0)} kcal · P ${Math.round(m.protein_g || 0)}g · F ${Math.round(m.fat_g || 0)}g · KH ${Math.round(m.carbs_g || 0)}g`
|
||||
}
|
||||
|
||||
function DayItems({ date, onChanged }) {
|
||||
const [items, setItems] = useState(null)
|
||||
const [macros, setMacros] = useState(null)
|
||||
const [open, setOpen] = useState(false)
|
||||
const [error, setError] = useState(null)
|
||||
const [applying, setApplying] = useState(false)
|
||||
|
||||
const load = async () => {
|
||||
const [nextItems, nextMacros] = await Promise.all([
|
||||
nutritionApi.listNutritionItems(date),
|
||||
nutritionApi.getNutritionDayMacros(date).catch(() => null),
|
||||
])
|
||||
setItems(nextItems)
|
||||
setMacros(nextMacros)
|
||||
}
|
||||
|
||||
const toggle = async () => {
|
||||
if (open) {
|
||||
setOpen(false)
|
||||
return
|
||||
}
|
||||
if (!items) {
|
||||
try {
|
||||
setItems(await nutritionApi.listNutritionItems(date))
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
return
|
||||
}
|
||||
try {
|
||||
setError(null)
|
||||
await load()
|
||||
setOpen(true)
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
const applyCatalog = async () => {
|
||||
setApplying(true)
|
||||
setError(null)
|
||||
try {
|
||||
const next = await nutritionApi.applyNutritionDayCatalogMacros(date)
|
||||
setMacros(next)
|
||||
onChanged?.()
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
} finally {
|
||||
setApplying(false)
|
||||
}
|
||||
setOpen(true)
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -225,16 +263,54 @@ function DayItems({ date }) {
|
||||
</button>
|
||||
{error && <p style={{ color: 'var(--danger)', fontSize: 12, margin: '6px 0 0' }}>{error}</p>}
|
||||
{open && Array.isArray(items) && (
|
||||
<ul style={{ margin: '8px 0 0', paddingLeft: 18, fontSize: 13, color: 'var(--text1)' }}>
|
||||
{items.length === 0 && <li style={{ color: 'var(--text3)' }}>Keine Zeilen für diesen Tag</li>}
|
||||
{items.map((it) => (
|
||||
<li key={it.id}>
|
||||
{it.source_name_raw}
|
||||
{it.quantity_raw ? ` · ${it.quantity_raw}` : ''}
|
||||
{it.food_name_de ? ` → ${it.food_name_de}` : ' · noch nicht zugeordnet'}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<>
|
||||
<ul style={{ margin: '8px 0 0', paddingLeft: 18, fontSize: 13, color: 'var(--text1)' }}>
|
||||
{items.length === 0 && <li style={{ color: 'var(--text3)' }}>Keine Zeilen für diesen Tag</li>}
|
||||
{items.map((it) => {
|
||||
const asg = itemAssignment(it)
|
||||
return (
|
||||
<li key={it.id}>
|
||||
{it.source_name_raw}
|
||||
{it.quantity_raw ? ` · ${it.quantity_raw}` : ''}
|
||||
{' '}
|
||||
<span style={{ color: asg.ok ? 'var(--accent-dark)' : 'var(--text3)' }}>{asg.text}</span>
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
{macros && (
|
||||
<div style={{ marginTop: 10, padding: '8px 10px', background: 'var(--surface2)', borderRadius: 8, fontSize: 12 }}>
|
||||
<div>Aktuell in der Tagesübersicht: {formatMacros(macros.existing)}</div>
|
||||
<div style={{ marginTop: 4 }}>
|
||||
Aus Zuordnung berechnet
|
||||
{macros.catalog_origin ? ` (${macros.catalog_origin})` : ''}:
|
||||
{' '}{formatMacros(macros.catalog)}
|
||||
{macros.unmapped_item_count
|
||||
? ` · ${macros.unmapped_item_count} Zeile${macros.unmapped_item_count === 1 ? '' : 'n'} noch ohne vollständige Zuordnung (dort FDDB)`
|
||||
: ''}
|
||||
</div>
|
||||
{macros.can_apply && macros.differs && (
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary"
|
||||
style={{ marginTop: 8, fontSize: 12 }}
|
||||
disabled={applying}
|
||||
onClick={applyCatalog}
|
||||
>
|
||||
{applying ? 'Übernehme…' : 'Berechnete Makros in den Tag übernehmen'}
|
||||
</button>
|
||||
)}
|
||||
{macros.can_apply && !macros.differs && (
|
||||
<div style={{ marginTop: 6, color: 'var(--accent-dark)' }}>Tagesmakros entsprechen bereits der Zuordnung.</div>
|
||||
)}
|
||||
{!macros.can_apply && (
|
||||
<div style={{ marginTop: 6, color: 'var(--text3)' }}>
|
||||
Noch keine berechenbaren Katalog-Makros. Liste verknüpfen und alle Zutaten zuordnen.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
@@ -376,7 +452,7 @@ function DataTab({ entries, onUpdate }) {
|
||||
</div>
|
||||
)}
|
||||
{e.item_count > 0 ? (
|
||||
<DayItems date={e.date} />
|
||||
<DayItems date={e.date} onChanged={onUpdate} />
|
||||
) : (
|
||||
<div style={{fontSize:12, color:'var(--text3)', marginTop:6}}>Keine einzelnen Lebensmittelzeilen</div>
|
||||
)}
|
||||
|
||||
@@ -281,6 +281,8 @@ export const api = {
|
||||
return readJsonResponse(r)
|
||||
},
|
||||
listNutritionMarks: () => req('/nutrition/marks'),
|
||||
getNutritionDayMacros: (date) => req(`/nutrition/days/${encodeURIComponent(date)}/macros`),
|
||||
applyNutritionDayCatalogMacros: (date) => req(`/nutrition/days/${encodeURIComponent(date)}/apply-catalog-macros`, { method: 'POST' }),
|
||||
putNutritionDayMark: (date, d) => req(`/nutrition/days/${date}/mark`, jput(d)),
|
||||
deleteNutritionDayMark: (date) => req(`/nutrition/days/${date}/mark`, {method:'DELETE'}),
|
||||
resolveNutritionConflicts: (decisions) => req('/nutrition/import-conflicts/resolve', json({decisions})),
|
||||
|
||||
Reference in new issue
Block a user