feat: Eigene Stoffe am Lebensmittel anlegen
Deploy Development / deploy (push) Successful in 1m8s
Build Test / pytest-backend (push) Successful in 5s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 22s

EPA-Chips und mg/µg vom Etikett; fehlende Parameter ohne Admin.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-09-13 15:47:49 +02:00
1 parent a5adff6196
commit d35087ad84
9 files changed
+246 -29

No files matched your search

+129 -21
View File
@@ -1,16 +1,35 @@
import { useEffect, useRef, useState } from 'react'
import { api } from '../utils/api'
const MASS = { g: 1, mg: 0.001, 'µg': 0.000001, ug: 0.000001, mcg: 0.000001 }
const QUICK_KEYS = ['EPA', 'DHA', 'DPA', 'OMEGA3']
function parseNum(raw) {
const n = parseFloat(String(raw ?? '').replace(',', '.'))
return Number.isFinite(n) ? n : 0
}
function attrUnit(attr) {
const u = (attr?.unit || 'g').trim()
if (u === 'ug' || u === 'mcg') return 'µg'
return u
}
export function convertMass(value, fromUnit, toUnit) {
const from = MASS[(fromUnit || 'g').toLowerCase()]
const to = MASS[(toUnit || 'g').toLowerCase()]
if (!from || !to) return value
return value * (from / to)
}
export function foodCreatePayload(name, macros, extras, servingG) {
const attributes = {}
for (const row of extras || []) {
if (!row?.attr?.attr_key || row.value === '' || row.value == null) continue
attributes[row.attr.attr_key] = parseNum(row.value)
const n = parseNum(row.value)
const target = attrUnit(row.attr)
const entered = row.inputUnit || target
attributes[row.attr.attr_key] = convertMass(n, entered, target)
}
const serving = parseNum(servingG)
return {
@@ -29,10 +48,20 @@ export function foodCreatePayload(name, macros, extras, servingG) {
export default function FoodNutrientFields({ macros, setMacros, extras, setExtras, servingG, setServingG }) {
const [q, setQ] = useState('')
const [hits, setHits] = useState([])
const [quick, setQuick] = useState([])
const [loading, setLoading] = useState(false)
const [newName, setNewName] = useState('')
const [newUnit, setNewUnit] = useState('g')
const [creating, setCreating] = useState(false)
const [createErr, setCreateErr] = useState(null)
const timer = useRef(null)
useEffect(() => () => clearTimeout(timer.current), [])
useEffect(() => {
api.listFoodAttributes('', 20, QUICK_KEYS.join(',')).then((rows) => {
setQuick(Array.isArray(rows) ? rows : [])
}).catch(() => {})
}, [])
const search = (term) => {
clearTimeout(timer.current)
@@ -55,7 +84,9 @@ export default function FoodNutrientFields({ macros, setMacros, extras, setExtra
const add = (attr) => {
setExtras((list) => (
list.some((r) => r.attr.attr_key === attr.attr_key) ? list : [...list, { attr, value: '' }]
list.some((r) => r.attr.attr_key === attr.attr_key)
? list
: [...list, { attr, value: '', inputUnit: attrUnit(attr) }]
))
setQ('')
setHits([])
@@ -65,10 +96,36 @@ export default function FoodNutrientFields({ macros, setMacros, extras, setExtra
setExtras((list) => list.map((r) => (r.attr.attr_key === key ? { ...r, value } : r)))
}
const setInputUnit = (key, unit) => {
setExtras((list) => list.map((r) => (r.attr.attr_key === key ? { ...r, inputUnit: unit } : r)))
}
const createAttr = async () => {
const name = (newName || q).trim()
if (!name) {
setCreateErr('Name des Stoffs fehlt')
return
}
setCreating(true)
setCreateErr(null)
try {
const attr = await api.createFoodAttribute({ name_de: name, unit: newUnit })
add(attr)
setNewName('')
} catch (e) {
setCreateErr(e.message)
} finally {
setCreating(false)
}
}
const unusedQuick = quick.filter((a) => !extras.some((r) => r.attr.attr_key === a.attr_key))
return (
<div>
<p style={{ fontSize: 12, color: 'var(--text3)', margin: '0 0 8px' }}>
Zahlen in der Einheit des Stoffs. Bei Etikett „pro 8 ml / 8 g“ die Portionsgröße setzen — gespeichert wird immer pro 100 g.
Etikettwerte so eintragen, wie sie dastehen. Portionsgröße setzen (5 ml Öl ≈ 5 g). Gespeichert wird immer pro 100 g.
Ein Öl/Supplement ist ein Lebensmittel, keine Liste — Listen können keine Listen enthalten.
</p>
<label style={{ display: 'block', fontSize: 12, color: 'var(--text2)', marginBottom: 8 }}>
Angabe gilt für{' '}
@@ -81,7 +138,7 @@ export default function FoodNutrientFields({ macros, setMacros, extras, setExtra
value={servingG}
onChange={(e) => setServingG(e.target.value)}
/>
{' '}g (100 = pro 100 g)
{' '}g (100 = pro 100 g; 5 ml Öl meist 5)
</label>
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 }}>
{[['kcal', 'kcal'], ['protein_g', 'Protein g'], ['fat_g', 'Fett g'], ['carbs_g', 'Kohlenhydrate g']].map(([key, label]) => (
@@ -99,11 +156,20 @@ export default function FoodNutrientFields({ macros, setMacros, extras, setExtra
</label>
))}
</div>
<p style={{ fontSize: 12, fontWeight: 600, margin: '12px 0 6px' }}>Weitere Stoffe (EPA, DHA, Ballaststoffe, …)</p>
<p style={{ fontSize: 12, fontWeight: 600, margin: '12px 0 6px' }}>Weitere Stoffe</p>
{unusedQuick.length > 0 && (
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, marginBottom: 8 }}>
{unusedQuick.map((a) => (
<button key={a.attr_key} type="button" className="btn btn-secondary" style={{ fontSize: 12 }} onClick={() => add(a)}>
{a.attr_key}
</button>
))}
</div>
)}
<input
className="form-input"
style={{ width: '100%', textAlign: 'left' }}
placeholder="Stoff suchen, z. B. EPA oder Omega"
placeholder="Stoff suchen, z. B. EPA, Omega, Vitamin D"
value={q}
onChange={(e) => { setQ(e.target.value); search(e.target.value) }}
/>
@@ -122,21 +188,63 @@ export default function FoodNutrientFields({ macros, setMacros, extras, setExtra
</span>
</button>
))}
{extras.map((row) => (
<label key={row.attr.attr_key} style={{ display: 'block', fontSize: 12, color: 'var(--text2)', marginTop: 8 }}>
{row.attr.name_de}{row.attr.unit ? ` (${row.attr.unit})` : ''}
<input
className="form-input"
type="number"
min="0"
step="0.001"
style={{ width: '100%', textAlign: 'left', marginTop: 4 }}
value={row.value}
onChange={(e) => setValue(row.attr.attr_key, e.target.value)}
placeholder="Wert in der Einheit oben"
/>
</label>
))}
<div style={{ marginTop: 10, padding: 10, background: 'var(--surface2)', borderRadius: 8 }}>
<p style={{ fontSize: 12, color: 'var(--text2)', margin: '0 0 6px' }}>Stoff nicht gefunden? Neu anlegen (wird für alle Lebensmittel nutzbar).</p>
<input
className="form-input"
style={{ width: '100%', textAlign: 'left', marginBottom: 6 }}
placeholder="Name, z. B. Vitamin D3"
value={newName}
onChange={(e) => setNewName(e.target.value)}
/>
<div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
<select className="form-input" style={{ width: 90, textAlign: 'left' }} value={newUnit} onChange={(e) => setNewUnit(e.target.value)}>
<option value="g">g</option>
<option value="mg">mg</option>
<option value="µg">µg</option>
</select>
<button type="button" className="btn btn-secondary" disabled={creating} onClick={createAttr}>
{creating ? '…' : 'Parameter anlegen'}
</button>
</div>
{createErr && <p style={{ color: 'var(--danger)', fontSize: 12, margin: '6px 0 0' }}>{createErr}</p>}
</div>
{extras.map((row) => {
const target = attrUnit(row.attr)
const entered = row.inputUnit || target
const mass = MASS[target.toLowerCase()]
return (
<label key={row.attr.attr_key} style={{ display: 'block', fontSize: 12, color: 'var(--text2)', marginTop: 8 }}>
{row.attr.name_de} (Katalog: {target})
<span style={{ display: 'flex', gap: 6, marginTop: 4 }}>
<input
className="form-input"
type="number"
min="0"
step="0.001"
style={{ flex: 1, textAlign: 'left' }}
value={row.value}
onChange={(e) => setValue(row.attr.attr_key, e.target.value)}
placeholder="Wert vom Etikett"
/>
{mass ? (
<select
className="form-input"
style={{ width: 72, textAlign: 'left' }}
value={entered}
onChange={(e) => setInputUnit(row.attr.attr_key, e.target.value)}
>
<option value="g">g</option>
<option value="mg">mg</option>
<option value="µg">µg</option>
</select>
) : (
<span style={{ alignSelf: 'center' }}>{target}</span>
)}
</span>
</label>
)
})}
</div>
)
}