feat: Stoffe nach BLS-Gruppen sortieren und aufklappbar listen
Energie und Makros zuerst, restliche Naehrstoffgruppen zuklappbar, damit die Liste bedienbar bleibt. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
94ddf84262
commit
44e3e216ab
6 files changed
+230
-95
No files matched your search
@@ -1,29 +1,55 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
|
||||
const FILTERS = [
|
||||
{ id: 'all', label: 'Alle' },
|
||||
{ id: 'macro', label: 'Makros' },
|
||||
{ id: 'needs', label: 'Ohne Bezeichnung' },
|
||||
{ id: 'official', label: 'BLS' },
|
||||
{ id: 'extension', label: 'Erweiterung' },
|
||||
const GROUP_OPTIONS = [
|
||||
{ id: 'energy', label: 'Energie' },
|
||||
{ id: 'macro', label: 'Makronährstoffe' },
|
||||
{ id: 'carbs', label: 'Kohlenhydrate' },
|
||||
{ id: 'fiber', label: 'Ballaststoffe' },
|
||||
{ id: 'fatty_acids', label: 'Fettsäuren' },
|
||||
{ id: 'amino_acids', label: 'Aminosäuren' },
|
||||
{ id: 'vitamins', label: 'Vitamine' },
|
||||
{ id: 'minerals', label: 'Mineralstoffe' },
|
||||
{ id: 'organic_acids', label: 'Organische Säuren' },
|
||||
{ id: 'polyols', label: 'Zuckeralkohole' },
|
||||
{ id: 'other', label: 'Sonstige' },
|
||||
{ id: 'extension', label: 'Eigene Erweiterungen' },
|
||||
{ id: 'unknown', label: 'Ohne Gruppe' },
|
||||
]
|
||||
|
||||
const DEFAULT_OPEN = new Set(['energy', 'macro'])
|
||||
|
||||
function matches(row, term, filter) {
|
||||
if (filter === 'macro' && !row.is_macro) return false
|
||||
if (filter === 'needs' && !row.needs_label) return false
|
||||
if (filter === 'official' && row.origin !== 'official_bls') return false
|
||||
if (filter === 'extension' && row.origin === 'official_bls') return false
|
||||
if (filter !== 'all' && filter !== 'needs' && row.group_key !== filter) return false
|
||||
if (!term) return true
|
||||
const hay = `${row.attr_key} ${row.name_de || ''} ${row.name_en || ''} ${row.unit || ''}`.toLowerCase()
|
||||
const hay = `${row.attr_key} ${row.name_de || ''} ${row.name_en || ''} ${row.unit || ''} ${row.group_label || ''}`.toLowerCase()
|
||||
return hay.includes(term)
|
||||
}
|
||||
|
||||
function groupRows(rows) {
|
||||
const byKey = new Map()
|
||||
for (const row of rows) {
|
||||
const key = row.group_key || 'unknown'
|
||||
if (!byKey.has(key)) {
|
||||
byKey.set(key, {
|
||||
key,
|
||||
label: row.group_label || 'Ohne Gruppe',
|
||||
rank: row.group_rank ?? 99,
|
||||
items: [],
|
||||
})
|
||||
}
|
||||
byKey.get(key).items.push(row)
|
||||
}
|
||||
return [...byKey.values()].sort((a, b) => a.rank - b.rank)
|
||||
}
|
||||
|
||||
export default function AdminFoodAttributesPage() {
|
||||
const [rows, setRows] = useState([])
|
||||
const [form, setForm] = useState({ attr_key: '', name_de: '', unit: '', data_type: 'num_per_100g' })
|
||||
const [q, setQ] = useState('')
|
||||
const [filter, setFilter] = useState('all')
|
||||
const [open, setOpen] = useState(() => new Set(DEFAULT_OPEN))
|
||||
const [editId, setEditId] = useState(null)
|
||||
const [draft, setDraft] = useState({ name_de: '', unit: '', category: '' })
|
||||
const [error, setError] = useState(null)
|
||||
@@ -35,13 +61,37 @@ export default function AdminFoodAttributesPage() {
|
||||
|
||||
const term = q.trim().toLowerCase()
|
||||
const shown = useMemo(() => rows.filter((r) => matches(r, term, filter)), [rows, term, filter])
|
||||
const groups = useMemo(() => groupRows(shown), [shown])
|
||||
const missing = rows.filter((r) => r.needs_label).length
|
||||
const macros = rows.filter((r) => r.is_macro)
|
||||
const groupCounts = useMemo(() => {
|
||||
const counts = {}
|
||||
for (const r of rows) {
|
||||
const key = r.group_key || 'unknown'
|
||||
counts[key] = (counts[key] || 0) + 1
|
||||
}
|
||||
return counts
|
||||
}, [rows])
|
||||
|
||||
useEffect(() => {
|
||||
if (term || filter !== 'all') {
|
||||
setOpen(new Set(groups.map((g) => g.key)))
|
||||
}
|
||||
}, [term, filter, groups])
|
||||
|
||||
const toggle = (key) => {
|
||||
setOpen((prev) => {
|
||||
const next = new Set(prev)
|
||||
if (next.has(key)) next.delete(key)
|
||||
else next.add(key)
|
||||
return next
|
||||
})
|
||||
}
|
||||
|
||||
const startEdit = (r) => {
|
||||
setEditId(r.id)
|
||||
setDraft({ name_de: r.name_de || '', unit: r.unit || '', category: r.category || '' })
|
||||
setDraft({ name_de: r.name_de || '', unit: r.unit || '', category: r.group_key || r.category || '' })
|
||||
setError(null)
|
||||
setOpen((prev) => new Set(prev).add(r.group_key || 'unknown'))
|
||||
}
|
||||
|
||||
const saveEdit = async (id) => {
|
||||
@@ -64,7 +114,7 @@ export default function AdminFoodAttributesPage() {
|
||||
try {
|
||||
const res = await api.adminFillBlsAttributeLabels()
|
||||
await load()
|
||||
setNotice(`${res.updated ?? 0} Bezeichnungen aus dem BLS-4.0-Glossar ergänzt.`)
|
||||
setNotice(`${res.updated ?? 0} Bezeichnungen/Gruppen aus dem BLS-4.0-Glossar ergänzt.`)
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
} finally {
|
||||
@@ -76,26 +126,9 @@ export default function AdminFoodAttributesPage() {
|
||||
<div className="card">
|
||||
<h1 className="page-title">Stoffe & Attribute</h1>
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', lineHeight: 1.55, margin: '0 0 12px' }}>
|
||||
Jeder Stoff hat einen festen <strong>Schlüssel</strong> (BLS/EuroFIR, z. B. ENERCC).
|
||||
Die deutsche Bezeichnung ist nur Anzeige — sie darf hier geändert werden, der Schlüssel nicht.
|
||||
Gruppiert nach BLS-Nährstoffgruppen. Innerhalb der Gruppe: zuerst die vier Makros für die Haupttabelle, dann Name.
|
||||
Schlüssel bleibt fest, Bezeichnung und Gruppe sind änderbar.
|
||||
</p>
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', lineHeight: 1.55, margin: '0 0 12px' }}>
|
||||
<strong>Weg in die Makro-Haupttabelle:</strong> Am Lebensmittel liegen Werte je 100 g in
|
||||
{' '}<code>food_attribute_values</code>. Beim Gegessen-Eintrag zählt die Menge in Gramm.
|
||||
Nur vier Schlüssel werden nach <code>nutrition_log</code> geschrieben:
|
||||
Energie <code>ENERCC</code> → kcal, Protein <code>PROT625</code> → protein_g,
|
||||
Fett <code>FAT</code> → fat_g, Kohlenhydrate <code>CHO</code> → carbs_g
|
||||
(Wert × Menge / 100). Alle anderen Stoffe (EPA, Ballaststoffe, Vitamine, …) landen in
|
||||
{' '}<code>nutrition_daily_nutrients</code>, nicht in den vier Makro-Spalten.
|
||||
Fehlt einer der vier Werte am zugeordneten Lebensmittel, gilt die Zeile als ungemappt
|
||||
und es bleiben die FDDB-Makros des Tages.
|
||||
</p>
|
||||
{macros.length > 0 && (
|
||||
<p style={{ fontSize: 13, margin: '0 0 12px' }}>
|
||||
Makros in dieser Liste:{' '}
|
||||
{macros.map((m) => `${m.attr_key} (${m.name_de || 'ohne Name'})`).join(' · ')}
|
||||
</p>
|
||||
)}
|
||||
{error && <p style={{ color: 'var(--danger)' }}>{error}</p>}
|
||||
{notice && <p style={{ color: 'var(--accent-dark)', fontSize: 13 }}>{notice}</p>}
|
||||
|
||||
@@ -126,65 +159,98 @@ export default function AdminFoodAttributesPage() {
|
||||
</div>
|
||||
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginBottom: 10 }}>
|
||||
{FILTERS.map((f) => (
|
||||
<button type="button" className={'btn ' + (filter === 'all' ? 'btn-primary' : 'btn-secondary')} style={{ fontSize: 12 }} onClick={() => { setFilter('all'); setOpen(new Set(DEFAULT_OPEN)) }}>
|
||||
Alle ({rows.length})
|
||||
</button>
|
||||
{GROUP_OPTIONS.filter((g) => groupCounts[g.id]).map((g) => (
|
||||
<button
|
||||
key={f.id}
|
||||
key={g.id}
|
||||
type="button"
|
||||
className={'btn ' + (filter === f.id ? 'btn-primary' : 'btn-secondary')}
|
||||
className={'btn ' + (filter === g.id ? 'btn-primary' : 'btn-secondary')}
|
||||
style={{ fontSize: 12 }}
|
||||
onClick={() => setFilter(f.id)}
|
||||
onClick={() => setFilter(g.id)}
|
||||
>
|
||||
{f.label}{f.id === 'needs' && missing ? ` (${missing})` : ''}
|
||||
{g.label} ({groupCounts[g.id]})
|
||||
</button>
|
||||
))}
|
||||
<button type="button" className="btn btn-secondary" style={{ fontSize: 12 }} disabled={busy || missing === 0} onClick={fillLabels}>
|
||||
Leere Namen aus BLS-Glossar füllen
|
||||
<button type="button" className={'btn ' + (filter === 'needs' ? 'btn-primary' : 'btn-secondary')} style={{ fontSize: 12 }} onClick={() => setFilter('needs')}>
|
||||
Ohne Bezeichnung{missing ? ` (${missing})` : ''}
|
||||
</button>
|
||||
<button type="button" className="btn btn-secondary" style={{ fontSize: 12 }} onClick={() => setOpen(new Set(groups.map((g) => g.key)))}>
|
||||
Alle aufklappen
|
||||
</button>
|
||||
<button type="button" className="btn btn-secondary" style={{ fontSize: 12 }} onClick={() => setOpen(new Set())}>
|
||||
Alle zuklappen
|
||||
</button>
|
||||
<button type="button" className="btn btn-secondary" style={{ fontSize: 12 }} disabled={busy} onClick={fillLabels}>
|
||||
Namen/Gruppen aus Glossar füllen
|
||||
</button>
|
||||
</div>
|
||||
<input
|
||||
className="form-input"
|
||||
style={{ width: '100%', textAlign: 'left', marginBottom: 10 }}
|
||||
placeholder="Stoffe suchen (Name oder Schlüssel)…"
|
||||
placeholder="In allen Gruppen suchen…"
|
||||
value={q}
|
||||
onChange={(e) => setQ(e.target.value)}
|
||||
/>
|
||||
<p style={{ fontSize: 13, margin: '0 0 8px' }}>{shown.length} von {rows.length} Stoffen</p>
|
||||
<p style={{ fontSize: 13, margin: '0 0 8px' }}>{shown.length} von {rows.length} Stoffen · {groups.length} Gruppen</p>
|
||||
|
||||
{shown.map((r) => (
|
||||
<div key={r.id} style={{ padding: '8px 0', borderTop: '1px solid var(--border)' }}>
|
||||
{editId === r.id ? (
|
||||
<div>
|
||||
<div style={{ fontSize: 12, color: 'var(--text3)', marginBottom: 6 }}>
|
||||
Schlüssel {r.attr_key} · {r.origin}{r.is_macro ? ' · Makro → nutrition_log' : ''}
|
||||
{groups.map((g) => {
|
||||
const opened = open.has(g.key)
|
||||
return (
|
||||
<section key={g.key} style={{ marginBottom: 8, border: '1px solid var(--border)', borderRadius: 10, overflow: 'hidden' }}>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => toggle(g.key)}
|
||||
style={{
|
||||
width: '100%', textAlign: 'left', display: 'flex', justifyContent: 'space-between',
|
||||
gap: 8, padding: '10px 12px', background: 'var(--surface2)', border: 0,
|
||||
cursor: 'pointer', fontFamily: 'inherit', color: 'inherit',
|
||||
}}
|
||||
>
|
||||
<span style={{ fontWeight: 600, fontSize: 14 }}>{g.label}</span>
|
||||
<span style={{ fontSize: 12, color: 'var(--text3)' }}>{g.items.length} · {opened ? 'zuklappen' : 'aufklappen'}</span>
|
||||
</button>
|
||||
{opened && g.items.map((r) => (
|
||||
<div key={r.id} style={{ padding: '8px 12px', borderTop: '1px solid var(--border)' }}>
|
||||
{editId === r.id ? (
|
||||
<div>
|
||||
<div style={{ fontSize: 12, color: 'var(--text3)', marginBottom: 6 }}>
|
||||
Schlüssel {r.attr_key} · {r.origin}{r.is_macro ? ' · Makro → nutrition_log' : ''}
|
||||
</div>
|
||||
<div className="form-row">
|
||||
<input className="form-input" value={draft.name_de} onChange={(e) => setDraft({ ...draft, name_de: e.target.value })} placeholder="Name DE" />
|
||||
<input className="form-input" value={draft.unit} onChange={(e) => setDraft({ ...draft, unit: e.target.value })} placeholder="Einheit" />
|
||||
<select className="form-input" value={draft.category} onChange={(e) => setDraft({ ...draft, category: e.target.value })}>
|
||||
{GROUP_OPTIONS.map((opt) => (
|
||||
<option key={opt.id} value={opt.id}>{opt.label}</option>
|
||||
))}
|
||||
</select>
|
||||
<button type="button" className="btn btn-primary" disabled={busy} onClick={() => saveEdit(r.id)}>Speichern</button>
|
||||
<button type="button" className="btn btn-secondary" disabled={busy} onClick={() => setEditId(null)}>Abbrechen</button>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', gap: 8, alignItems: 'center' }}>
|
||||
<div>
|
||||
<div style={{ fontWeight: 600 }}>
|
||||
{r.needs_label ? r.attr_key : (r.name_de || r.attr_key)}
|
||||
{r.is_macro ? <span style={{ marginLeft: 8, fontSize: 11, color: 'var(--accent-dark)' }}>Makro</span> : null}
|
||||
</div>
|
||||
<div style={{ fontSize: 12, color: 'var(--text3)' }}>
|
||||
{r.attr_key}
|
||||
{r.needs_label ? ' · Bezeichnung fehlt' : ''}
|
||||
{r.unit ? ` · ${r.unit}` : ''}
|
||||
</div>
|
||||
</div>
|
||||
<button type="button" className="btn btn-primary" style={{ fontSize: 12 }} onClick={() => startEdit(r)}>Bearbeiten</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="form-row">
|
||||
<input className="form-input" value={draft.name_de} onChange={(e) => setDraft({ ...draft, name_de: e.target.value })} placeholder="Name DE" />
|
||||
<input className="form-input" value={draft.unit} onChange={(e) => setDraft({ ...draft, unit: e.target.value })} placeholder="Einheit" />
|
||||
<input className="form-input" value={draft.category} onChange={(e) => setDraft({ ...draft, category: e.target.value })} placeholder="Gruppe" />
|
||||
<button type="button" className="btn btn-primary" disabled={busy} onClick={() => saveEdit(r.id)}>Speichern</button>
|
||||
<button type="button" className="btn btn-secondary" disabled={busy} onClick={() => setEditId(null)}>Abbrechen</button>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', gap: 8, alignItems: 'center' }}>
|
||||
<div>
|
||||
<div style={{ fontWeight: 600 }}>
|
||||
{r.needs_label ? r.attr_key : (r.name_de || r.attr_key)}
|
||||
{r.is_macro ? <span style={{ marginLeft: 8, fontSize: 11, color: 'var(--accent-dark)' }}>Makro</span> : null}
|
||||
</div>
|
||||
<div style={{ fontSize: 12, color: 'var(--text3)' }}>
|
||||
{r.attr_key}
|
||||
{r.needs_label ? ' · Bezeichnung fehlt (nur Schlüssel)' : ''}
|
||||
{r.unit ? ` · ${r.unit}` : ''}
|
||||
{` · ${r.data_type}`}
|
||||
{` · ${r.origin}`}
|
||||
</div>
|
||||
</div>
|
||||
<button type="button" className="btn btn-primary" style={{ fontSize: 12 }} onClick={() => startEdit(r)}>Bearbeiten</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
))}
|
||||
</section>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user