feat: Listen-Tab und sichtbare Zutatenlisten-Verwaltung
Listen anlegen, bearbeiten und importieren liegt unter Ernährung. Tagebuchzeilen werden auch mit Mengen-Prefix verknüpft. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3b46630a37
commit
b37005be01
12 files changed
+307
-73
No files matched your search
@@ -1,76 +1,170 @@
|
||||
import { useState } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
import FoodRecipeDialog from './FoodRecipeDialog'
|
||||
|
||||
export default function FoodRecipeEditor({ recipes, learned, onChanged }) {
|
||||
const [open, setOpen] = useState(false)
|
||||
export default function FoodRecipeEditor({ learned: learnedProp, highlightId, onChanged }) {
|
||||
const [recipes, setRecipes] = useState([])
|
||||
const [learned, setLearned] = useState(learnedProp || [])
|
||||
const [q, setQ] = useState('')
|
||||
const [visible, setVisible] = useState(40)
|
||||
const [editing, setEditing] = useState(null)
|
||||
const [error, setError] = useState(null)
|
||||
const [notice, setNotice] = useState(null)
|
||||
const [importing, setImporting] = useState(false)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const listRef = useRef(null)
|
||||
const highlightRef = useRef(null)
|
||||
const openedHighlight = useRef(null)
|
||||
|
||||
const load = async () => {
|
||||
try {
|
||||
const [r, m] = await Promise.all([
|
||||
api.listNutritionRecipes(true),
|
||||
learnedProp ? Promise.resolve(learnedProp) : api.listMyFoodMappings().catch(() => []),
|
||||
])
|
||||
setRecipes(Array.isArray(r) ? r : [])
|
||||
setLearned(Array.isArray(m) ? m : (learnedProp || []))
|
||||
setError(null)
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => { load() }, [])
|
||||
useEffect(() => { if (learnedProp) setLearned(learnedProp) }, [learnedProp])
|
||||
|
||||
const startNew = () => {
|
||||
setEditing('new')
|
||||
setError(null)
|
||||
setOpen(true)
|
||||
}
|
||||
|
||||
const startEdit = async (r) => {
|
||||
setError(null)
|
||||
try {
|
||||
const full = (r.ingredients && r.ingredients.length) || r.ingredient_count === 0
|
||||
const full = (r.ingredients && r.ingredients.length)
|
||||
? r
|
||||
: await api.getNutritionRecipe(r.id)
|
||||
setEditing(full)
|
||||
setOpen(true)
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!highlightId || !recipes.length) return
|
||||
highlightRef.current?.scrollIntoView({ block: 'nearest', behavior: 'smooth' })
|
||||
if (openedHighlight.current === highlightId) return
|
||||
const r = recipes.find((x) => x.id === highlightId)
|
||||
if (!r) return
|
||||
openedHighlight.current = highlightId
|
||||
startEdit(r)
|
||||
}, [highlightId, recipes])
|
||||
|
||||
const filtered = recipes.filter((r) => {
|
||||
const term = q.trim().toLowerCase()
|
||||
if (!term) return true
|
||||
return `${r.name_raw || ''} ${r.name_normalized || ''}`.toLowerCase().includes(term)
|
||||
})
|
||||
const shown = filtered.slice(0, visible)
|
||||
|
||||
const save = async (body) => {
|
||||
if (editing === 'new') await api.createNutritionRecipe(body)
|
||||
else await api.updateNutritionRecipe(editing.id, body)
|
||||
setEditing(null)
|
||||
await load()
|
||||
onChanged?.()
|
||||
}
|
||||
|
||||
const remove = async (id) => {
|
||||
if (!confirm('Liste wirklich löschen?')) return
|
||||
if (!confirm('Liste wirklich löschen? Tagebuchzeilen fallen dann wieder unter Zuordnen.')) return
|
||||
try {
|
||||
await api.deleteNutritionRecipe(id)
|
||||
if (editing && editing !== 'new' && editing.id === id) setEditing(null)
|
||||
await load()
|
||||
onChanged?.()
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
const importLists = async (file) => {
|
||||
if (!file) return
|
||||
setImporting(true)
|
||||
setError(null)
|
||||
try {
|
||||
const res = await api.importFddbLists(file)
|
||||
await load()
|
||||
onChanged?.()
|
||||
const pending = res.rebuild_pending ? ' Tageswerte werden im Hintergrund nachgerechnet.' : ''
|
||||
setNotice(`${res.recipes} Listen importiert, ${res.items_linked || 0} Tagebuchzeilen verknüpft.${pending}`)
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
} finally {
|
||||
setImporting(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={{ marginTop: 16 }}>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 8 }}>
|
||||
<h3 style={{ fontSize: 14, margin: 0 }}>Eigene Listen / Kombinationen ({recipes.length})</h3>
|
||||
<button type="button" className="btn btn-secondary" style={{ fontSize: 12 }} onClick={() => { setOpen((v) => !v); if (open) setEditing(null) }}>
|
||||
{open ? 'Einklappen' : 'Anzeigen'}
|
||||
<div className="card section-gap">
|
||||
<div className="card-title">Zutatenlisten</div>
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', lineHeight: 1.6, marginBottom: 12 }}>
|
||||
Roh-Kombinationen (Müsli, Bowl, Smoothie) — nicht gekochte Gerichte.
|
||||
Import aus FDDB oder selbst anlegen. Zutaten danach unter Zuordnen mappen.
|
||||
</p>
|
||||
{error && <div style={{ color: 'var(--danger)', fontSize: 13, marginBottom: 10 }}>{error}</div>}
|
||||
{notice && <div style={{ fontSize: 13, color: 'var(--accent-dark)', marginBottom: 10 }}>{notice}</div>}
|
||||
|
||||
<input ref={listRef} type="file" accept=".csv,text/csv" style={{ display: 'none' }} onChange={(e) => { const f = e.target.files?.[0]; e.target.value = ''; if (f) importLists(f) }} />
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginBottom: 12 }}>
|
||||
<button type="button" className="btn btn-primary" onClick={startNew}>Liste anlegen</button>
|
||||
<button type="button" className="btn btn-secondary" disabled={importing} onClick={() => listRef.current?.click()}>
|
||||
{importing ? 'Importiere…' : 'FDDB-Listen importieren'}
|
||||
</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}>Liste 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.ingredient_count ?? (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>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<p style={{ fontSize: 13, margin: '0 0 8px' }}>{loading ? 'Lade Listen…' : `${recipes.length} Listen`}</p>
|
||||
{recipes.length > 8 && (
|
||||
<input
|
||||
className="form-input"
|
||||
style={{ width: '100%', textAlign: 'left', marginBottom: 8 }}
|
||||
placeholder="Liste suchen…"
|
||||
value={q}
|
||||
onChange={(e) => { setQ(e.target.value); setVisible(40) }}
|
||||
/>
|
||||
)}
|
||||
{!loading && recipes.length === 0 && (
|
||||
<p className="muted">Noch keine Listen. FDDB-Export (`lists_*.csv`) importieren oder eine neue anlegen.</p>
|
||||
)}
|
||||
{shown.map((r) => (
|
||||
<div
|
||||
key={r.id}
|
||||
ref={r.id === highlightId ? highlightRef : undefined}
|
||||
style={{
|
||||
borderTop: '1px solid var(--border)',
|
||||
padding: '10px 0',
|
||||
background: r.id === highlightId ? 'var(--surface2)' : undefined,
|
||||
borderRadius: r.id === highlightId ? 8 : 0,
|
||||
}}
|
||||
>
|
||||
<div style={{ fontWeight: 600 }}>{r.name_raw}</div>
|
||||
<div style={{ fontSize: 12, color: 'var(--text3)' }}>
|
||||
{r.ingredient_count ?? (r.ingredients || []).length} Zutaten · {r.portions || 1} Portionen
|
||||
{r.source === 'fddb_list' ? ' · FDDB' : 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>
|
||||
))}
|
||||
{visible < filtered.length && (
|
||||
<button type="button" className="btn btn-secondary btn-full" style={{ marginTop: 8 }} onClick={() => setVisible((n) => n + 40)}>
|
||||
Weitere {Math.min(40, filtered.length - visible)} zeigen
|
||||
</button>
|
||||
)}
|
||||
|
||||
{editing === 'new' && (
|
||||
<FoodRecipeDialog
|
||||
title="Liste anlegen"
|
||||
|
||||
@@ -2,7 +2,6 @@ import { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
import FoodSearchModal from './FoodSearchModal'
|
||||
import FoodNutrientFields, { foodCreatePayload } from './FoodNutrientFields'
|
||||
import FoodRecipeEditor from './FoodRecipeEditor'
|
||||
import FoodRecipeDialog from './FoodRecipeDialog'
|
||||
|
||||
const PERIODS = [
|
||||
@@ -28,7 +27,8 @@ function suggestQuery(raw) {
|
||||
}
|
||||
|
||||
function RecipePickModal({ recipes, sourceName, onPick, onClose }) {
|
||||
const [q, setQ] = useState(suggestQuery(sourceName))
|
||||
const [q, setQ] = useState('')
|
||||
const hint = suggestQuery(sourceName)
|
||||
const filtered = recipes.filter((r) => {
|
||||
const hay = `${r.name_raw || ''} ${r.name_normalized || ''}`.toLowerCase()
|
||||
return !q.trim() || hay.includes(q.trim().toLowerCase())
|
||||
@@ -53,14 +53,19 @@ function RecipePickModal({ recipes, sourceName, onPick, onClose }) {
|
||||
}}
|
||||
>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '14px 16px', borderBottom: '1px solid var(--border)' }}>
|
||||
<h2 className="card-title" style={{ margin: 0, fontSize: 16 }}>Eigene Liste wählen</h2>
|
||||
<h2 className="card-title" style={{ margin: 0, fontSize: 16 }}>Liste verknüpfen</h2>
|
||||
<button type="button" className="btn btn-secondary" onClick={onClose}>Schließen</button>
|
||||
</div>
|
||||
<div style={{ padding: '12px 16px' }}>
|
||||
<input className="form-input" style={{ width: '100%', textAlign: 'left' }} autoFocus placeholder="Listenname filtern" value={q} onChange={(e) => setQ(e.target.value)} />
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', lineHeight: 1.5, margin: '0 0 10px' }}>
|
||||
Diese Tagebuchzeile{hint ? ` („${hint}“)` : ''} einer bestehenden Zutatenliste zuordnen.
|
||||
Der Eintrag verschwindet danach hier; die Zutaten der Liste ordnest du einzeln zu.
|
||||
</p>
|
||||
<input className="form-input" style={{ width: '100%', textAlign: 'left' }} autoFocus placeholder="Alle Listen durchsuchen…" value={q} onChange={(e) => setQ(e.target.value)} />
|
||||
</div>
|
||||
<div style={{ overflowY: 'auto', padding: '0 16px 16px', flex: 1 }}>
|
||||
{filtered.length === 0 && <p style={{ fontSize: 13, color: 'var(--text3)' }}>Keine passende Liste.</p>}
|
||||
{recipes.length === 0 && <p style={{ fontSize: 13, color: 'var(--text3)' }}>Noch keine Listen. Unter „Listen“ anlegen oder FDDB importieren.</p>}
|
||||
{recipes.length > 0 && filtered.length === 0 && <p style={{ fontSize: 13, color: 'var(--text3)' }}>Keine Liste passt zum Filter.</p>}
|
||||
{filtered.map((r) => (
|
||||
<button key={r.id} type="button" className="btn btn-secondary btn-full" style={{ marginTop: 8, justifyContent: 'flex-start', textAlign: 'left', height: 'auto', padding: '10px 12px' }} onClick={() => onPick(r.id)}>
|
||||
<span>
|
||||
@@ -122,7 +127,7 @@ function InlineCreate({ defaultName, disabled, allowRecipe, onCreateFood, onWant
|
||||
)
|
||||
}
|
||||
|
||||
export default function NutritionFoodMap({ onChanged, onMapped }) {
|
||||
export default function NutritionFoodMap({ onChanged, onMapped, onManageLists, onCreatedList }) {
|
||||
const [unmapped, setUnmapped] = useState([])
|
||||
const [learned, setLearned] = useState([])
|
||||
const [recipes, setRecipes] = useState([])
|
||||
@@ -263,8 +268,10 @@ export default function NutritionFoodMap({ onChanged, onMapped }) {
|
||||
await api.applyNutritionRecipe(rec.id, row.source_name_raw)
|
||||
setCreateFor(null)
|
||||
setSearchFor(null)
|
||||
setNotice('Liste angelegt. Offene Zutaten erscheinen unten zum Zuordnen.')
|
||||
await load()
|
||||
setUnmapped((list) => list.filter((x) => x.source_name_normalized !== row.source_name_normalized))
|
||||
setNotice('Liste angelegt und mit der Tagebuchzeile verknüpft. Zutaten erscheinen unter Zuordnen, die Liste unter Listen.')
|
||||
onCreatedList?.(rec)
|
||||
if (!onCreatedList) await load()
|
||||
onMapped?.()
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
@@ -280,7 +287,8 @@ export default function NutritionFoodMap({ onChanged, onMapped }) {
|
||||
try {
|
||||
await api.applyNutritionRecipe(recipeId, sourceName)
|
||||
setRecipeFor(null)
|
||||
setNotice('Liste verknüpft. Offene Zutaten erscheinen unten zum Zuordnen.')
|
||||
setUnmapped((list) => list.filter((x) => x.source_name_raw !== sourceName && x.source_name_normalized !== sourceName))
|
||||
setNotice('Liste verknüpft. Offene Zutaten der Liste erscheinen hier zum Zuordnen.')
|
||||
await load()
|
||||
onMapped?.()
|
||||
} catch (e) {
|
||||
@@ -361,10 +369,15 @@ export default function NutritionFoodMap({ onChanged, onMapped }) {
|
||||
{notice && <div style={{ fontSize: 13, color: 'var(--accent-dark)', marginBottom: 10 }}>{notice}</div>}
|
||||
|
||||
<input ref={listRef} type="file" accept=".csv,text/csv" style={{ display: 'none' }} onChange={(e) => { const f = e.target.files?.[0]; e.target.value = ''; if (f) importLists(f) }} />
|
||||
<button type="button" className="btn btn-secondary btn-full" disabled={importing} onClick={() => listRef.current?.click()}>
|
||||
{importing ? 'Importiere Listen…' : 'FDDB-Listen importieren'}
|
||||
</button>
|
||||
<FoodRecipeEditor recipes={recipes} learned={learned} onChanged={() => { load(); onChanged?.() }} />
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginBottom: 8 }}>
|
||||
<button type="button" className="btn btn-primary" onClick={() => onManageLists?.()}>Listen verwalten</button>
|
||||
<button type="button" className="btn btn-secondary" disabled={importing} onClick={() => listRef.current?.click()}>
|
||||
{importing ? 'Importiere Listen…' : 'FDDB-Listen importieren'}
|
||||
</button>
|
||||
</div>
|
||||
<p style={{ fontSize: 12, color: 'var(--text3)', margin: '0 0 12px' }}>
|
||||
{recipes.length ? `${recipes.length} Listen vorhanden.` : 'Noch keine Listen.'} Anlegen, bearbeiten und FDDB-Import unter Listen.
|
||||
</p>
|
||||
<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>
|
||||
@@ -414,9 +427,15 @@ export default function NutritionFoodMap({ onChanged, onMapped }) {
|
||||
<div style={{ fontWeight: 600 }}>{suggestQuery(u.source_name_raw) || u.source_name_raw}</div>
|
||||
<div style={{ fontSize: 12, color: 'var(--text3)' }}>
|
||||
{u.kind === 'recipe_ingredient' ? 'Listenzutat' : `${u.count}×`}
|
||||
{u.matching_recipe_id ? ` · Liste${u.matching_recipe_name ? `: ${u.matching_recipe_name}` : ''}` : ''}
|
||||
{u.variant_count > 1 ? ` · ${u.variant_count} Mengen` : ''}
|
||||
{u.last_date ? ` · zuletzt ${formatDay(u.last_date)}` : ''}
|
||||
</div>
|
||||
{u.kind !== 'recipe_ingredient' && u.matching_recipe_id && (
|
||||
<div style={{ fontSize: 12, color: 'var(--accent-dark)', marginTop: 4 }}>
|
||||
Entspricht einer Zutatenliste — verknüpfen, dann die Zutaten zuordnen.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{(u.ambiguous || suggestions.length > 1) && (
|
||||
<span style={{ fontSize: 11, padding: '2px 8px', borderRadius: 999, background: 'var(--surface2)', color: 'var(--danger)', whiteSpace: 'nowrap' }}>
|
||||
@@ -450,10 +469,13 @@ export default function NutritionFoodMap({ onChanged, onMapped }) {
|
||||
</button>
|
||||
<button type="button" className="btn btn-secondary" disabled={busyRow} onClick={() => setSearchFor(u)}>Mehr suchen</button>
|
||||
{u.kind !== 'recipe_ingredient' && u.matching_recipe_id && (
|
||||
<button type="button" className="btn btn-secondary" disabled={busyRow} onClick={() => applyRecipe(u.source_name_raw, u.matching_recipe_id)}>Als Liste</button>
|
||||
<button type="button" className="btn btn-primary" disabled={busyRow} onClick={() => applyRecipe(u.source_name_raw, u.matching_recipe_id)}>Als Liste verknüpfen</button>
|
||||
)}
|
||||
{u.kind !== 'recipe_ingredient' && recipes.length > 0 && (
|
||||
<button type="button" className="btn btn-secondary" disabled={busyRow} onClick={() => setRecipeFor(u)}>Liste wählen</button>
|
||||
{u.kind !== 'recipe_ingredient' && u.matching_recipe_id && (
|
||||
<button type="button" className="btn btn-secondary" disabled={busyRow} onClick={() => onManageLists?.(u.matching_recipe_id)}>Liste öffnen</button>
|
||||
)}
|
||||
{u.kind !== 'recipe_ingredient' && (
|
||||
<button type="button" className="btn btn-secondary" disabled={busyRow} onClick={() => setRecipeFor(u)}>Andere Liste wählen</button>
|
||||
)}
|
||||
</div>
|
||||
{createFor === key && (
|
||||
|
||||
Reference in new issue
Block a user