({ getItems, updateItem })
| 19 | import { PageTitle, PageDescription, PageWrapper } from "styles/common"; |
| 20 | |
| 21 | const Inventory: React.FC<InventorySpecs.Props> = ({ getItems, updateItem }) => { |
| 22 | const [items, setItems] = React.useState<ItemType[]>([]); |
| 23 | const [filterText, setFilterText] = React.useState<string>(''); |
| 24 | const [categories, setCategories] = React.useState<Category[]>([]); |
| 25 | const [loading, setLoading] = React.useState<boolean>(true); |
| 26 | const { dispatch } = useSidebar(); |
| 27 | |
| 28 | React.useEffect(() => { |
| 29 | fetchItems(); |
| 30 | dispatch({ type: 'setTitle', value: 'Add Item' }); |
| 31 | dispatch({ type: 'setContent', value: SidebarContent() }); |
| 32 | |
| 33 | return function cleanup() { |
| 34 | dispatch({ type: 'reset' }) |
| 35 | } |
| 36 | }, []); |
| 37 | |
| 38 | function fetchItems() { |
| 39 | getItems() |
| 40 | .then(items => { |
| 41 | const categories = getCategories(items); |
| 42 | const rowItems = Object.assign([], items.map(i => ({ ...i, refresh: fetchItems }))); |
| 43 | setItems(rowItems); |
| 44 | setCategories(categories); |
| 45 | setLoading(false); |
| 46 | }) |
| 47 | .catch(err => console.warn(err)); |
| 48 | } |
| 49 | |
| 50 | function renderTables() { |
| 51 | const tables = categories.map(cat => { |
| 52 | let catItems = items.filter(i => i.categoryId === cat.id); |
| 53 | catItems = searchItems(catItems, filterText); |
| 54 | |
| 55 | if (!catItems.length) return null; |
| 56 | return <Table key={cat.id} category={cat} items={catItems} fetchItems={fetchItems} updateItem={updateItem}/> |
| 57 | }); |
| 58 | |
| 59 | return <div style={{ minWidth: '100%', overflowX: "scroll" }}>{tables}</div>; |
| 60 | } |
| 61 | |
| 62 | const renderEmptyList = () => ( |
| 63 | <MessageArea> |
| 64 | Get started by adding your inventory. |
| 65 | </MessageArea> |
| 66 | ); |
| 67 | |
| 68 | const SidebarContent = () => <ItemForm onSubmit={fetchItems}/>; |
| 69 | |
| 70 | const InventoryTable = !!categories.length ? renderTables() : renderEmptyList(); |
| 71 | return ( |
| 72 | <DocumentTitle title={`Packstack | Inventory`}> |
| 73 | <PageWrapper> |
| 74 | <PageTitle> |
| 75 | <h1>Inventory</h1> |
| 76 | </PageTitle> |
| 77 | <PageDescription> |
| 78 | <p> |
nothing calls this directly
no test coverage detected