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 (
<>
Set shipment description
value.length > 0} hint="Enter a shipment description" />