Files
mitai-jinkendo/frontend/src/components/MyFoodsPanel.jsx
T
LarsandCursor e452d9a8fe
Deploy Development / deploy (push) Successful in 1m7s
Build Test / pytest-backend (push) Successful in 5s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 23s
fix: zentrale Lebensmittel fuer Admins aenderbar
Persoenliche Liste bleibt beim Owner; zentrale Eintraege oeffnen den Admin-Editor. Admin-Katalog trennt beide Listen, persoenliche bleiben sichtbar.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-14 13:01:41 +02:00

149 lines
6.4 KiB
React

import { useEffect, useMemo, useState } from 'react'
import { api } from '../utils/api'
import { useAuth } from '../context/AuthContext'
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 { isAdmin } = useAuth()
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 [editMode, setEditMode] = useState('user')
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, hier änderbar. <strong>Zentral:</strong> für alle Nutzer
{isAdmin ? ' — als Admin hier ebenfalls änderbar' : ' (nur Admin ändert)'}.
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' }}>
<button
type="button"
onClick={() => { setEditMode('user'); setEditId(f.id) }}
style={{ background: 'none', border: 0, padding: 0, textAlign: 'left', color: 'inherit', cursor: 'pointer' }}
>
<div style={{ fontWeight: 600 }}>{f.name_de}</div>
<div style={{ fontSize: 12, color: 'var(--text3)' }}>nur du</div>
</button>
<button type="button" className="btn btn-primary" style={{ fontSize: 12 }} onClick={() => { setEditMode('user'); 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={{ display: 'flex', justifyContent: 'space-between', gap: 8, padding: '10px 0', borderTop: '1px solid var(--border)', alignItems: 'center' }}>
{isAdmin ? (
<button
type="button"
onClick={() => { setEditMode('admin'); setEditId(f.id) }}
style={{ background: 'none', border: 0, padding: 0, textAlign: 'left', color: 'inherit', cursor: 'pointer' }}
>
<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>
</button>
) : (
<div>
<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>
)}
{isAdmin && (
<button type="button" className="btn btn-primary" style={{ fontSize: 12 }} onClick={() => { setEditMode('admin'); setEditId(f.id) }}>Bearbeiten</button>
)}
</div>
))}
{(creating || editId) && (
<FoodEditDialog
mode={creating ? 'user' : editMode}
foodId={creating ? null : editId}
onClose={() => { setCreating(false); setEditId(null) }}
onSaved={(res) => {
setCreating(false)
setEditId(null)
afterSave(res)
}}
/>
)}
</div>
)
}