feat: Eigene und zentrale Lebensmittel bearbeiten, mit Sichtbarkeit im Mapping
Nutzer können persönliche Foods anlegen und ändern; der Admin legt zentrale Einträge für alle an. Suche und Namens-Mapping berücksichtigen persönlich, zentral und BLS. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
d35087ad84
commit
8b1e1da5d8
30 files changed
+1195
-120
No files matched your search
@@ -0,0 +1,123 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
import FoodEditDialog from './FoodEditDialog'
|
||||
import { foodVisibilitySuffix } from '../utils/foodVisibility'
|
||||
|
||||
function filterByName(list, term) {
|
||||
if (!term) return list
|
||||
return list.filter((f) => (f.name_de || '').toLowerCase().includes(term))
|
||||
}
|
||||
|
||||
export default function MyFoodsPanel() {
|
||||
const [foods, setFoods] = useState([])
|
||||
const [shared, setShared] = useState([])
|
||||
const [q, setQ] = useState('')
|
||||
const [visible, setVisible] = useState(60)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState(null)
|
||||
const [notice, setNotice] = useState(null)
|
||||
const [editId, setEditId] = useState(null)
|
||||
const [creating, setCreating] = useState(false)
|
||||
|
||||
const load = async () => {
|
||||
try {
|
||||
const [mine, central] = await Promise.all([
|
||||
api.listMyFoods(),
|
||||
api.listSharedFoods().catch(() => []),
|
||||
])
|
||||
setFoods(Array.isArray(mine) ? mine : [])
|
||||
setShared(Array.isArray(central) ? central : [])
|
||||
setError(null)
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => { load() }, [])
|
||||
|
||||
const term = q.trim().toLowerCase()
|
||||
const filteredMine = useMemo(() => filterByName(foods, term), [foods, term])
|
||||
const filteredShared = useMemo(() => filterByName(shared, term), [shared, term])
|
||||
const shownMine = filteredMine.slice(0, visible)
|
||||
|
||||
const afterSave = (res) => {
|
||||
load()
|
||||
const n = res?.rebuild_pending
|
||||
if (n) setNotice(`Gespeichert. Tageswerte für ${n} Tag${n === 1 ? '' : 'e'} werden nachgerechnet.`)
|
||||
else setNotice('Lebensmittel gespeichert.')
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="card section-gap">
|
||||
<div className="card-title">Lebensmittel</div>
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', lineHeight: 1.6, marginBottom: 12 }}>
|
||||
<strong>Persönlich:</strong> nur für dich. <strong>Zentral:</strong> vom Admin für alle Nutzer.
|
||||
BLS-Katalog suchst du beim Zuordnen. Ein Öl/Supplement bleibt ein Lebensmittel, keine Liste.
|
||||
</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>}
|
||||
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginBottom: 12 }}>
|
||||
<button type="button" className="btn btn-primary" onClick={() => setCreating(true)}>Persönliches Lebensmittel anlegen</button>
|
||||
</div>
|
||||
|
||||
<input
|
||||
className="form-input"
|
||||
style={{ width: '100%', textAlign: 'left', marginBottom: 8 }}
|
||||
placeholder="Persönliche und zentrale Einträge suchen…"
|
||||
value={q}
|
||||
onChange={(e) => { setQ(e.target.value); setVisible(60) }}
|
||||
/>
|
||||
|
||||
<h3 style={{ fontSize: 14, margin: '16px 0 8px' }}>Persönlich ({loading ? '…' : filteredMine.length})</h3>
|
||||
{!loading && foods.length === 0 && (
|
||||
<p className="muted">Noch keine eigenen Einträge. Hier anlegen oder beim Zuordnen über „Neu anlegen“.</p>
|
||||
)}
|
||||
{!loading && foods.length > 0 && filteredMine.length === 0 && (
|
||||
<p className="muted">Kein persönlicher Treffer für „{q.trim()}“.</p>
|
||||
)}
|
||||
{shownMine.map((f) => (
|
||||
<div key={f.id} style={{ display: 'flex', justifyContent: 'space-between', gap: 8, padding: '10px 0', borderTop: '1px solid var(--border)', alignItems: 'center' }}>
|
||||
<div>
|
||||
<div style={{ fontWeight: 600 }}>{f.name_de}</div>
|
||||
<div style={{ fontSize: 12, color: 'var(--text3)' }}>nur du</div>
|
||||
</div>
|
||||
<button type="button" className="btn btn-secondary" style={{ fontSize: 12 }} onClick={() => setEditId(f.id)}>Bearbeiten</button>
|
||||
</div>
|
||||
))}
|
||||
{visible < filteredMine.length && (
|
||||
<button type="button" className="btn btn-secondary btn-full" style={{ marginTop: 8 }} onClick={() => setVisible((n) => n + 60)}>
|
||||
Weitere {Math.min(60, filteredMine.length - visible)} zeigen
|
||||
</button>
|
||||
)}
|
||||
|
||||
<h3 style={{ fontSize: 14, margin: '20px 0 8px' }}>Zentral für alle ({loading ? '…' : filteredShared.length})</h3>
|
||||
{!loading && shared.length === 0 && (
|
||||
<p className="muted">Noch keine zentralen Einträge vom Admin.</p>
|
||||
)}
|
||||
{!loading && shared.length > 0 && filteredShared.length === 0 && (
|
||||
<p className="muted">Kein zentraler Treffer für „{q.trim()}“.</p>
|
||||
)}
|
||||
{filteredShared.map((f) => (
|
||||
<div key={f.id} style={{ padding: '10px 0', borderTop: '1px solid var(--border)' }}>
|
||||
<div style={{ fontWeight: 600 }}>{f.name_de}</div>
|
||||
<div style={{ fontSize: 12, color: 'var(--text3)' }}>für alle Nutzer{foodVisibilitySuffix(f.catalog_kind, f.visibility)}</div>
|
||||
</div>
|
||||
))}
|
||||
|
||||
{(creating || editId) && (
|
||||
<FoodEditDialog
|
||||
foodId={creating ? null : editId}
|
||||
onClose={() => { setCreating(false); setEditId(null) }}
|
||||
onSaved={(res) => {
|
||||
setCreating(false)
|
||||
setEditId(null)
|
||||
afterSave(res)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user