feat: Stoff-Bezeichnungen editieren und BLS-Glossar fuellen
Kryptische Schluessel bleiben Identitaet; Namen/Einheit sind aenderbar. Nur ENERCC/PROT625/FAT/CHO schreiben die Makro-Haupttabelle. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
e452d9a8fe
commit
94ddf84262
13 files changed
+586
-29
No files matched your search
@@ -1,22 +1,105 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
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' },
|
||||
]
|
||||
|
||||
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 (!term) return true
|
||||
const hay = `${row.attr_key} ${row.name_de || ''} ${row.name_en || ''} ${row.unit || ''}`.toLowerCase()
|
||||
return hay.includes(term)
|
||||
}
|
||||
|
||||
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 [editId, setEditId] = useState(null)
|
||||
const [draft, setDraft] = useState({ name_de: '', unit: '', category: '' })
|
||||
const [error, setError] = useState(null)
|
||||
const [notice, setNotice] = useState(null)
|
||||
const [busy, setBusy] = useState(false)
|
||||
|
||||
const load = () => api.adminBlsAttributes().then(setRows).catch((e) => setError(e.message))
|
||||
useEffect(() => { load() }, [])
|
||||
|
||||
const term = q.trim().toLowerCase()
|
||||
const shown = useMemo(() => rows.filter((r) => matches(r, term, filter)), [rows, term, filter])
|
||||
const missing = rows.filter((r) => r.needs_label).length
|
||||
const macros = rows.filter((r) => r.is_macro)
|
||||
|
||||
const startEdit = (r) => {
|
||||
setEditId(r.id)
|
||||
setDraft({ name_de: r.name_de || '', unit: r.unit || '', category: r.category || '' })
|
||||
setError(null)
|
||||
}
|
||||
|
||||
const saveEdit = async (id) => {
|
||||
setBusy(true)
|
||||
try {
|
||||
const updated = await api.adminUpdateBlsAttribute(id, draft)
|
||||
setRows((list) => list.map((r) => (r.id === id ? { ...r, ...updated } : r)))
|
||||
setEditId(null)
|
||||
setNotice('Bezeichnung gespeichert. Der Schlüssel bleibt unverändert.')
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
const fillLabels = async () => {
|
||||
setBusy(true)
|
||||
setError(null)
|
||||
try {
|
||||
const res = await api.adminFillBlsAttributeLabels()
|
||||
await load()
|
||||
setNotice(`${res.updated ?? 0} Bezeichnungen aus dem BLS-4.0-Glossar ergänzt.`)
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="card">
|
||||
<h1 className="page-title">Stoffe & Attribute</h1>
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)' }}>
|
||||
Offizielle BLS-Codes kommen aus dem Import. Erweiterungen (Histamin, Gluten, …) hier anlegen — ohne Migration.
|
||||
<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.
|
||||
</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>}
|
||||
<div className="form-row">
|
||||
{notice && <p style={{ color: 'var(--accent-dark)', fontSize: 13 }}>{notice}</p>}
|
||||
|
||||
<div className="form-row" style={{ marginBottom: 12 }}>
|
||||
<input className="form-input" placeholder="KEY" value={form.attr_key} onChange={(e) => setForm({ ...form, attr_key: e.target.value })} />
|
||||
<input className="form-input" placeholder="Name DE" value={form.name_de} onChange={(e) => setForm({ ...form, name_de: e.target.value })} />
|
||||
<input className="form-input" placeholder="Einheit" value={form.unit} onChange={(e) => setForm({ ...form, unit: e.target.value })} />
|
||||
@@ -26,17 +109,80 @@ export default function AdminFoodAttributesPage() {
|
||||
<option value="text">Text</option>
|
||||
<option value="enum">Auswahl</option>
|
||||
</select>
|
||||
<button type="button" className="btn btn-primary" onClick={async () => {
|
||||
try {
|
||||
await api.adminCreateBlsAttribute(form)
|
||||
setForm({ attr_key: '', name_de: '', unit: '', data_type: 'num_per_100g' })
|
||||
load()
|
||||
} catch (e) { setError(e.message) }
|
||||
}}>Anlegen</button>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary"
|
||||
disabled={busy}
|
||||
onClick={async () => {
|
||||
try {
|
||||
await api.adminCreateBlsAttribute(form)
|
||||
setForm({ attr_key: '', name_de: '', unit: '', data_type: 'num_per_100g' })
|
||||
load()
|
||||
} catch (e) { setError(e.message) }
|
||||
}}
|
||||
>
|
||||
Anlegen
|
||||
</button>
|
||||
</div>
|
||||
{rows.map((r) => (
|
||||
<div key={r.id} style={{ fontSize: 13, padding: '6px 0', borderTop: '1px solid var(--border)' }}>
|
||||
<strong>{r.attr_key}</strong> · {r.name_de} · {r.data_type} · {r.origin}
|
||||
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginBottom: 10 }}>
|
||||
{FILTERS.map((f) => (
|
||||
<button
|
||||
key={f.id}
|
||||
type="button"
|
||||
className={'btn ' + (filter === f.id ? 'btn-primary' : 'btn-secondary')}
|
||||
style={{ fontSize: 12 }}
|
||||
onClick={() => setFilter(f.id)}
|
||||
>
|
||||
{f.label}{f.id === 'needs' && missing ? ` (${missing})` : ''}
|
||||
</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>
|
||||
</div>
|
||||
<input
|
||||
className="form-input"
|
||||
style={{ width: '100%', textAlign: 'left', marginBottom: 10 }}
|
||||
placeholder="Stoffe suchen (Name oder Schlüssel)…"
|
||||
value={q}
|
||||
onChange={(e) => setQ(e.target.value)}
|
||||
/>
|
||||
<p style={{ fontSize: 13, margin: '0 0 8px' }}>{shown.length} von {rows.length} Stoffen</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' : ''}
|
||||
</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>
|
||||
))}
|
||||
</div>
|
||||
|
||||
@@ -311,6 +311,8 @@ export const api = {
|
||||
adminUpdateManualFood: (id, d) => req(`/admin/bls/foods/manual/${encodeURIComponent(id)}`, jput(d)),
|
||||
adminBlsAttributes: () => req('/admin/bls/attributes'),
|
||||
adminCreateBlsAttribute: (d) => req('/admin/bls/attributes', json(d)),
|
||||
adminUpdateBlsAttribute: (id, d) => req(`/admin/bls/attributes/${id}`, jput(d)),
|
||||
adminFillBlsAttributeLabels: () => req('/admin/bls/attributes/fill-labels', { method: 'POST' }),
|
||||
adminListFoodMappings: (globalOnly) => req(`/admin/food-mappings${globalOnly?'?global_only=true':''}`),
|
||||
adminCreateFoodMapping: (d) => req('/admin/food-mappings', json(d)),
|
||||
adminUpdateFoodMapping: (id, d) => req(`/admin/food-mappings/${id}`, jput(d)),
|
||||
|
||||
Reference in new issue
Block a user