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>
This commit is contained in:
1 parent
9a0767c8ac
commit
e452d9a8fe
13 files changed
+134
-45
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
import { useAuth } from '../context/AuthContext'
|
||||
import FoodNutrientFields, { foodCreatePayload } from './FoodNutrientFields'
|
||||
|
||||
function numStr(v) {
|
||||
@@ -9,17 +10,25 @@ function numStr(v) {
|
||||
}
|
||||
|
||||
export default function FoodEditDialog({ foodId, onClose, onSaved, zIndex, mode }) {
|
||||
const { isAdmin: sessionAdmin } = useAuth() || {}
|
||||
const isCreate = !foodId
|
||||
const isAdmin = mode === 'admin'
|
||||
const createAsAdmin = mode === 'admin'
|
||||
const [name, setName] = useState('')
|
||||
const [macros, setMacros] = useState({ kcal: '0', protein_g: '0', fat_g: '0', carbs_g: '0' })
|
||||
const [extras, setExtras] = useState([])
|
||||
const [servingG, setServingG] = useState('100')
|
||||
const [catalogKind, setCatalogKind] = useState(null)
|
||||
const [editable, setEditable] = useState(false)
|
||||
const [loading, setLoading] = useState(!isCreate)
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [error, setError] = useState(null)
|
||||
|
||||
const saveAsAdmin = isCreate
|
||||
? createAsAdmin
|
||||
: Boolean(sessionAdmin && catalogKind && catalogKind !== 'official_bls' && (
|
||||
catalogKind === 'manual_admin' || mode === 'admin'
|
||||
))
|
||||
|
||||
useEffect(() => {
|
||||
if (isCreate) {
|
||||
setEditable(true)
|
||||
@@ -27,10 +36,13 @@ export default function FoodEditDialog({ foodId, onClose, onSaved, zIndex, mode
|
||||
return
|
||||
}
|
||||
let alive = true
|
||||
const load = isAdmin ? api.adminBlsFoodDetail(foodId, true) : api.getBlsFood(foodId)
|
||||
const load = (sessionAdmin || mode === 'admin')
|
||||
? api.adminBlsFoodDetail(foodId, true).catch(() => api.getBlsFood(foodId))
|
||||
: api.getBlsFood(foodId)
|
||||
load.then((d) => {
|
||||
if (!alive) return
|
||||
setName(d.name_de || '')
|
||||
setCatalogKind(d.catalog_kind || null)
|
||||
const m = d.macros_per_100g || {}
|
||||
setMacros({
|
||||
kcal: numStr(m.kcal),
|
||||
@@ -38,13 +50,15 @@ export default function FoodEditDialog({ foodId, onClose, onSaved, zIndex, mode
|
||||
fat_g: numStr(m.fat_g),
|
||||
carbs_g: numStr(m.carbs_g),
|
||||
})
|
||||
setExtras((d.attributes || []).map((row) => ({
|
||||
setExtras((d.attributes || []).filter((row) => row?.attr).map((row) => ({
|
||||
attr: row.attr,
|
||||
value: numStr(row.value),
|
||||
inputUnit: row.attr?.unit || 'g',
|
||||
})))
|
||||
setServingG(numStr(d.serving_g || 100) === '0' ? '100' : numStr(d.serving_g || 100))
|
||||
const canEdit = Boolean(d.editable) || (isAdmin && d.catalog_kind && d.catalog_kind !== 'official_bls')
|
||||
const canEdit = Boolean(d.editable) || (
|
||||
sessionAdmin && d.catalog_kind && d.catalog_kind !== 'official_bls'
|
||||
)
|
||||
setEditable(canEdit)
|
||||
setLoading(false)
|
||||
}).catch((e) => {
|
||||
@@ -53,7 +67,7 @@ export default function FoodEditDialog({ foodId, onClose, onSaved, zIndex, mode
|
||||
setLoading(false)
|
||||
})
|
||||
return () => { alive = false }
|
||||
}, [foodId, isCreate, isAdmin])
|
||||
}, [foodId, isCreate, sessionAdmin, mode])
|
||||
|
||||
const save = async () => {
|
||||
if (!name.trim()) {
|
||||
@@ -65,7 +79,7 @@ export default function FoodEditDialog({ foodId, onClose, onSaved, zIndex, mode
|
||||
try {
|
||||
const body = foodCreatePayload(name, macros, extras, servingG)
|
||||
let saved
|
||||
if (isAdmin) {
|
||||
if (saveAsAdmin) {
|
||||
saved = isCreate ? await api.adminCreateManualFood(body) : await api.adminUpdateManualFood(foodId, body)
|
||||
} else {
|
||||
saved = isCreate ? await api.createUserFood(body) : await api.updateUserFood(foodId, body)
|
||||
@@ -79,6 +93,10 @@ export default function FoodEditDialog({ foodId, onClose, onSaved, zIndex, mode
|
||||
}
|
||||
}
|
||||
|
||||
const title = isCreate
|
||||
? (createAsAdmin ? 'Zentrales Lebensmittel anlegen' : 'Lebensmittel anlegen')
|
||||
: (catalogKind === 'manual_admin' ? 'Zentrales Lebensmittel bearbeiten' : 'Lebensmittel bearbeiten')
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
@@ -98,11 +116,7 @@ export default function FoodEditDialog({ foodId, onClose, onSaved, zIndex, mode
|
||||
}}
|
||||
>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '14px 16px', borderBottom: '1px solid var(--border)' }}>
|
||||
<h2 className="card-title" style={{ margin: 0, fontSize: 16 }}>
|
||||
{isCreate
|
||||
? (isAdmin ? 'Zentrales Lebensmittel anlegen' : 'Lebensmittel anlegen')
|
||||
: (isAdmin ? 'Zentrales Lebensmittel bearbeiten' : 'Lebensmittel bearbeiten')}
|
||||
</h2>
|
||||
<h2 className="card-title" style={{ margin: 0, fontSize: 16 }}>{title}</h2>
|
||||
<button type="button" className="btn btn-secondary" disabled={saving} onClick={onClose}>Schließen</button>
|
||||
</div>
|
||||
<div style={{ overflowY: 'auto', padding: '12px 16px 16px', flex: 1 }}>
|
||||
@@ -110,7 +124,9 @@ export default function FoodEditDialog({ foodId, onClose, onSaved, zIndex, mode
|
||||
{error && <div style={{ color: 'var(--danger)', fontSize: 13, marginBottom: 10 }}>{error}</div>}
|
||||
{!loading && !editable && (
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)' }}>
|
||||
Offizielle BLS-Einträge sind fest. Nur selbst angelegte{isAdmin ? ' zentrale' : ''} Lebensmittel lassen sich ändern.
|
||||
{catalogKind === 'manual_admin'
|
||||
? 'Zentrale Lebensmittel ändert nur ein Admin.'
|
||||
: 'Offizielle BLS-Einträge sind fest. Nur selbst angelegte Lebensmittel lassen sich ändern.'}
|
||||
</p>
|
||||
)}
|
||||
{!loading && editable && (
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
import { useAuth } from '../context/AuthContext'
|
||||
import FoodSearchModal from './FoodSearchModal'
|
||||
import FoodEditDialog from './FoodEditDialog'
|
||||
import { foodVisibilityLabel } from '../utils/foodVisibility'
|
||||
@@ -113,6 +114,7 @@ export default function FoodRecipeDialog({
|
||||
onSave,
|
||||
onClose,
|
||||
}) {
|
||||
const { isAdmin } = useAuth()
|
||||
const [name, setName] = useState(defaultName || '')
|
||||
const [portions, setPortions] = useState(defaultPortions != null ? String(defaultPortions) : '1')
|
||||
const [ings, setIngs] = useState(
|
||||
@@ -494,7 +496,7 @@ export default function FoodRecipeDialog({
|
||||
<span style={{ fontSize: 12, color: 'var(--text3)' }}>{foodVisibilityLabel(h.catalog_kind, h.visibility)}</span>
|
||||
</span>
|
||||
</button>
|
||||
{h.catalog_kind === 'manual_user' && (
|
||||
{(h.catalog_kind === 'manual_user' || (isAdmin && h.catalog_kind === 'manual_admin')) && (
|
||||
<button type="button" className="btn btn-secondary" style={{ flexShrink: 0 }} onClick={() => setEditFoodId(h.id)}>
|
||||
Bearbeiten
|
||||
</button>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
import { useAuth } from '../context/AuthContext'
|
||||
import FoodNutrientFields, { foodCreatePayload } from './FoodNutrientFields'
|
||||
import FoodEditDialog from './FoodEditDialog'
|
||||
import { foodVisibilitySuffix } from '../utils/foodVisibility'
|
||||
@@ -28,6 +29,7 @@ function detectUnit(...texts) {
|
||||
}
|
||||
|
||||
export default function FoodSearchModal({ title, initialQuery, quantityHint, allowRecipe, onSelect, onWantRecipe, onClose, zIndex }) {
|
||||
const { isAdmin } = useAuth()
|
||||
const [q, setQ] = useState(initialQuery || '')
|
||||
const [hits, setHits] = useState([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
@@ -191,7 +193,7 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, all
|
||||
<p style={{ fontSize: 13, color: 'var(--text3)' }}>Kein Treffer im Katalog.</p>
|
||||
)}
|
||||
{hits.map((h) => {
|
||||
const manual = h.catalog_kind && h.catalog_kind !== 'official_bls' && h.catalog_kind !== 'manual_admin'
|
||||
const manual = h.catalog_kind === 'manual_user' || (isAdmin && h.catalog_kind === 'manual_admin')
|
||||
return (
|
||||
<div key={h.id} style={{ display: 'flex', gap: 8, marginTop: 8, alignItems: 'stretch' }}>
|
||||
<button
|
||||
|
||||
@@ -56,7 +56,8 @@ export default function MyFoodsPanel() {
|
||||
<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.
|
||||
<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>}
|
||||
@@ -109,10 +110,21 @@ export default function MyFoodsPanel() {
|
||||
)}
|
||||
{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' }}>
|
||||
<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"
|
||||
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>
|
||||
)}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
import { useAuth } from '../context/AuthContext'
|
||||
import FoodSearchModal from './FoodSearchModal'
|
||||
import FoodNutrientFields, { foodCreatePayload } from './FoodNutrientFields'
|
||||
import FoodRecipeDialog from './FoodRecipeDialog'
|
||||
@@ -130,6 +131,7 @@ function InlineCreate({ defaultName, disabled, allowRecipe, onCreateFood, onWant
|
||||
}
|
||||
|
||||
export default function NutritionFoodMap({ onChanged, onMapped, onManageLists, onCreatedList, onOpenMyFoods }) {
|
||||
const { isAdmin } = useAuth()
|
||||
const [unmapped, setUnmapped] = useState([])
|
||||
const [learned, setLearned] = useState([])
|
||||
const [recipes, setRecipes] = useState([])
|
||||
@@ -525,7 +527,7 @@ export default function NutritionFoodMap({ onChanged, onMapped, onManageLists, o
|
||||
</div>
|
||||
</div>
|
||||
<div style={{ display: 'flex', gap: 8, flexShrink: 0 }}>
|
||||
{m.catalog_kind === 'manual_user' && m.food_id && (
|
||||
{m.food_id && (m.catalog_kind === 'manual_user' || (isAdmin && m.catalog_kind === 'manual_admin')) && (
|
||||
<button type="button" className="btn btn-secondary" onClick={() => setEditFoodId(m.food_id)}>Bearbeiten</button>
|
||||
)}
|
||||
<button type="button" className="btn btn-secondary" onClick={() => remove(m.id)}>Löschen</button>
|
||||
|
||||
@@ -2,6 +2,25 @@ import { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
import FoodEditDialog from '../components/FoodEditDialog'
|
||||
|
||||
function FoodRow({ row, onEdit }) {
|
||||
return (
|
||||
<div style={{ display: 'flex', gap: 8, marginTop: 6 }}>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-secondary"
|
||||
style={{ flex: 1, textAlign: 'left' }}
|
||||
onClick={() => onEdit(row)}
|
||||
>
|
||||
{row.bls_code ? `${row.bls_code} · ` : ''}{row.name_de}
|
||||
{row.catalog_kind === 'manual_admin' ? ' · zentral (alle)' : row.catalog_kind === 'manual_user' ? ' · persönlich' : ''}
|
||||
</button>
|
||||
{row.catalog_kind && row.catalog_kind !== 'official_bls' && (
|
||||
<button type="button" className="btn btn-primary" onClick={() => onEdit(row)}>Bearbeiten</button>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function AdminBlsFoodsPage() {
|
||||
const [tab, setTab] = useState('search')
|
||||
const [q, setQ] = useState('')
|
||||
@@ -49,6 +68,14 @@ export default function AdminBlsFoodsPage() {
|
||||
}
|
||||
|
||||
const shownManual = useMemo(() => manualRows, [manualRows])
|
||||
const centralRows = useMemo(
|
||||
() => shownManual.filter((r) => r.catalog_kind === 'manual_admin'),
|
||||
[shownManual],
|
||||
)
|
||||
const personalRows = useMemo(
|
||||
() => shownManual.filter((r) => r.catalog_kind === 'manual_user'),
|
||||
[shownManual],
|
||||
)
|
||||
|
||||
const openDetail = (id) => {
|
||||
api.adminBlsFoodDetail(id).then(setDetail).catch((e) => setError(e.message))
|
||||
@@ -100,7 +127,7 @@ export default function AdminBlsFoodsPage() {
|
||||
{tab === 'manual' && (
|
||||
<>
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', margin: '0 0 10px' }}>
|
||||
Zentral (für alle Nutzer) und persönlich (Nutzer-eigene). Zeile oder Bearbeiten öffnet den Editor.
|
||||
Zwei Listen: zentral für alle Nutzer, darunter persönliche Einträge (sichtbar, weil du Admin bist). Beide sind hier änderbar.
|
||||
</p>
|
||||
<div style={{ marginBottom: 10 }}>
|
||||
<button type="button" className="btn btn-primary" onClick={() => setCreating(true)}>Zentral anlegen</button>
|
||||
@@ -113,7 +140,9 @@ export default function AdminBlsFoodsPage() {
|
||||
placeholder="In manuellen Einträgen suchen…"
|
||||
/>
|
||||
<p style={{ fontSize: 13, margin: '0 0 8px' }}>
|
||||
{loadingManual ? 'Lade…' : `${shownManual.length} Einträge`}
|
||||
{loadingManual
|
||||
? 'Lade…'
|
||||
: `${centralRows.length} zentral, ${personalRows.length} persönlich`}
|
||||
{!loadingManual && shownManual.length >= 200 ? ' (Anzeige begrenzt auf 200)' : ''}
|
||||
</p>
|
||||
</>
|
||||
@@ -126,22 +155,23 @@ export default function AdminBlsFoodsPage() {
|
||||
<p className="muted">Keine manuellen Lebensmittel{manualQ.trim() ? ` zu „${manualQ.trim()}“` : ''}.</p>
|
||||
)}
|
||||
|
||||
{list.map((r) => (
|
||||
<div key={r.id} style={{ display: 'flex', gap: 8, marginTop: 6 }}>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-secondary"
|
||||
style={{ flex: 1, textAlign: 'left' }}
|
||||
onClick={() => openEdit(r)}
|
||||
>
|
||||
{r.bls_code ? `${r.bls_code} · ` : ''}{r.name_de}
|
||||
{r.catalog_kind === 'manual_admin' ? ' · zentral (alle)' : r.catalog_kind === 'manual_user' ? ' · persönlich' : ''}
|
||||
</button>
|
||||
{r.catalog_kind && r.catalog_kind !== 'official_bls' && (
|
||||
<button type="button" className="btn btn-primary" onClick={() => openEdit(r)}>Bearbeiten</button>
|
||||
)}
|
||||
</div>
|
||||
{tab === 'search' && list.map((r) => (
|
||||
<FoodRow key={r.id} row={r} onEdit={openEdit} />
|
||||
))}
|
||||
{tab === 'manual' && !loadingManual && shownManual.length > 0 && (
|
||||
<>
|
||||
<h3 style={{ fontSize: 14, margin: '16px 0 8px' }}>Zentral für alle ({centralRows.length})</h3>
|
||||
{centralRows.length === 0 && <p className="muted">Noch keine zentralen Einträge.</p>}
|
||||
{centralRows.map((r) => (
|
||||
<FoodRow key={r.id} row={r} onEdit={openEdit} />
|
||||
))}
|
||||
<h3 style={{ fontSize: 14, margin: '20px 0 8px' }}>Persönlich ({personalRows.length})</h3>
|
||||
{personalRows.length === 0 && <p className="muted">Keine persönlichen Einträge in dieser Auswahl.</p>}
|
||||
{personalRows.map((r) => (
|
||||
<FoodRow key={r.id} row={r} onEdit={openEdit} />
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
{detail && (
|
||||
<div style={{ marginTop: 16 }}>
|
||||
<h3>{detail.bls_code ? `${detail.bls_code} · ` : ''}{detail.name_de}</h3>
|
||||
|
||||
Reference in new issue
Block a user