import React, {useState} from "react"; import {Dropdown, Input, Select} from "semantic-ui-react"; import {Button, Confirm, Header, Icon, Loader, Menu, Modal, Segment} from "../Theme"; import {PageContainer, useTitle} from "../Common"; import {collectLocations, useCatalog, useInventories} from "../../hooks/customHooks"; import {InventoryTable} from "./InventoryTable"; import {InventoryItemsMobile} from "./InventoryItemsMobile"; import {InventorySummary} from "./InventorySummary"; import {FieldSchemaEditor} from "./FieldSchemaEditor"; import {CatalogEditor} from "./CatalogEditor"; import {Media} from "../../contexts/contexts"; const INVENTORY_TYPES = [ {key: 'food', value: 'food', text: 'Food Storage'}, {key: 'fuel', value: 'fuel', text: 'Fuel'}, {key: 'tool', value: 'tool', text: 'Tools'}, ]; function NewInventoryModal({open, onClose, onCreate}) { const [name, setName] = useState(''); const [type, setType] = useState('food'); const create = async () => { const inventory = await onCreate(name, type); if (inventory) { setName(''); setType('food'); onClose(); } }; return New Inventory setName(e.target.value)} placeholder='Food Storage' style={{marginBottom: '1em'}}/> Type:{' '} setType(data.value)}/> The type seeds a starting set of fields — you can customize them afterward. Cancel Create ; } export function InventoryRoute() { useTitle('Inventory'); const {inventories, persistInventory, addInventory, removeInventory} = useInventories(); const {catalog, persistCatalog} = useCatalog(); const [slug, setSlug] = useState(null); const [tab, setTab] = useState('items'); const [newOpen, setNewOpen] = useState(false); const [catalogOpen, setCatalogOpen] = useState(false); const [editFieldsOpen, setEditFieldsOpen] = useState(false); const [renaming, setRenaming] = useState(false); const [renameValue, setRenameValue] = useState(''); const [confirmDelete, setConfirmDelete] = useState(false); // Default to the first inventory once loaded. React.useEffect(() => { if (slug == null && inventories && inventories.length > 0) { setSlug(inventories[0].slug); } }, [inventories, slug]); const current = inventories?.find(i => i.slug === slug); const fields = current ? current.fields : []; const items = current ? current.items : []; // Location suggestions are pooled across every inventory. const locations = collectLocations(inventories); const onCreate = async (name, type) => { const inventory = await addInventory(name, type); if (inventory) { setSlug(inventory.slug); } return inventory; }; const doRename = async () => { await persistInventory(slug, {name: renameValue}); setRenaming(false); }; const doDelete = async () => { await removeInventory(slug); setConfirmDelete(false); setSlug(null); }; if (inventories === null) { return ; } const inventoryOptions = inventories.map(i => ({key: i.slug, value: i.slug, text: i.name})); return Inventory setSlug(data.value)} style={{minWidth: '14em'}} /> setNewOpen(true)} aria-label='New inventory'> setCatalogOpen(true)} aria-label='Food catalog'> Catalog {current && <> { setRenameValue(current.name); setRenaming(true); }} aria-label='Rename inventory'> setEditFieldsOpen(true)} aria-label='Customize fields'> Fields setConfirmDelete(true)} aria-label='Delete inventory'> >} {current ? <> setTab('items')}/> setTab('summary')}/> {tab === 'items' ? <> {/* Portrait mobile: condensed, read-only. Rotate to landscape (tablet+) for the full editor. */} persistInventory(slug, {items: newItems})}/> > : } setEditFieldsOpen(false)} onSave={newFields => persistInventory(slug, {fields: newFields})}/> > : Create an inventory to get started.} setNewOpen(false)} onCreate={onCreate}/> setCatalogOpen(false)} onSave={persistCatalog}/> setRenaming(false)} closeIcon size='tiny'> Rename Inventory setRenameValue(e.target.value)}/> setRenaming(false)}>Cancel Save setConfirmDelete(false)} onConfirm={doDelete} /> ; }
The type seeds a starting set of fields — you can customize them afterward.
Create an inventory to get started.