import {useState} from 'react' import Link from 'next/link' import AdminToolbar from '~/components/admin/actionBar' import {Button as RButton} from '@rmwc/button' import {FormController, Input, IntegerInput, Button} from '~/components/form' import Table from '~/components/table' import axios from 'axios' import router from 'next/router' NewShipment.getInitialProps = async ({ctx: {axios}}) => { const {data: allItems} = await axios.get('/api/items') return {allItems} } export default function NewShipment({allItems}){ const [currentView, setView] = useState('description') const [description, setDescription] = useState() const [items, setItems] = useState([]) const [currentItem, setCurrentItem] = useState() const currentUUIDs = items.map(({item}) => item.uuid) const addableItems = allItems.filter(({uuid}) => !currentUUIDs.includes(uuid)) const handleRemoveItem = item => { setItems(items => items.filter(listItem => item.uuid !== listItem.item.uuid)) } const handleAddItem = ({count}) => { const newItem = { count, item: currentItem } setCurrentItem(null) setItems(items => [...items.filter(listItem => currentItem.uuid !== listItem.item.uuid), newItem]) setView('summary') } const saveShipment = async () => { const {data: shipment} = await axios.post('/api/shipments', { description, items: items.map(listItem => ({count: listItem.count, uuid: listItem.item.uuid})) }) router.push(`/admin/shipments/${shipment.uuid}`) } switch(currentView){ case 'description': return ( <> Cancel Shipment {setDescription(description); setView('summary')}}>

Set shipment description

value.length > 0} hint="Enter a shipment description" />
) case 'summary': return ( <>
setView('description')}>Edit Description setView('selectItem')}>Add Item
item.item.name}, {name: 'Count', extractor: item => item.count}, {name: '', extractor: ({item}) => ( )} ]} // Map in an id property so the table can use array.map rows={items.map(listItem => ({...listItem, id: listItem.item.uuid}))} /> ) case 'selectItem': return ( <>
setView('summary')}>Cancel Adding Item

Select Item

item.name}, {name: '', extractor: item => ( )} ]} rows={addableItems} /> ) case 'addItem': if(currentItem === null){ setView('selectItem') return null } const currentValue = items.find(listItem => currentItem.uuid === listItem.item.uuid)?.count return ( <>
setView('summary')}>Cancel Adding Item

{currentValue !== undefined ? 'Adjust Item' : 'Add Item'}

How many of “{currentItem.name}” are in this shipment?

) default: return ( <> Cancel Shipment

An error occurred trying to create this shipment. Please refresh the page.

) } }