import React, {useState} from "react"; import {Button, IconButton, Modal, Select, Table, Text, TextInput} from "../ui"; import {ALL_UNITS} from "./units"; const UNIT_OPTIONS = [{value: '', label: '—'}, ...ALL_UNITS.map(u => ({value: u, label: u}))]; const COLUMNS = [ {key: 'name', label: 'Name'}, {key: 'category', label: 'Category'}, {key: 'subcategory', label: 'Subcategory'}, {key: 'item_size', label: 'Size'}, {key: 'item_size_unit', label: 'Unit'}, {key: 'calories', label: 'kcal'}, ]; function blankEntry() { return {name: '', category: '', subcategory: '', item_size: '', item_size_unit: '', calories: ''}; } /** * Manage the shared food catalog: a scrollable, editable table (add/edit/delete) saved as a whole list. Each * entry's `calories` is the total for one `item_size` package — the value pre-filled onto inventory items. */ export function CatalogEditor({catalog, open, onClose, onSave}) { const [draft, setDraft] = useState([]); React.useEffect(() => { if (open) { setDraft((catalog || []).map(e => ({...e}))); } }, [open, catalog]); const update = (index, key, value) => setDraft(prev => prev.map((e, i) => i === index ? {...e, [key]: value} : e)); const remove = (index) => setDraft(prev => prev.filter((_, i) => i !== index)); const add = () => setDraft(prev => [blankEntry(), ...prev]); const save = async () => { // Drop fully-empty rows. const cleaned = draft.filter(e => (e.name || '').trim()); await onSave(cleaned); onClose(); }; return Food Catalog Known items used to autocomplete and pre-fill the entry form. kcal is the total calories for one package of the given size. {COLUMNS.map(c => {c.label})} {draft.map((entry, index) => { const aria = (col) => `${entry.name || 'new item'} ${col}`; return update(index, 'name', e.currentTarget.value)}/> update(index, 'category', e.currentTarget.value)}/> update(index, 'subcategory', e.currentTarget.value)}/> update(index, 'item_size', e.currentTarget.value)}/>
; }