import React, {useReducer} from 'react'
import Router from 'next/router'
import axios from 'axios'
import ActionBar from '~/components/admin/actionBar'
import Table from '~/components/table'
import {Button} from '~/components/form'
ManualOrder.getInitialProps = async ({ctx}) => {
const {data: items} = await ctx.axios.get('/api/items?showUnpublished=true')
return {items}
}
export default function ManualOrder({items}) {
const [itemState, dispatch] = useReducer(itemCountReducer, {})
const totalNumber = Object.values(itemState).reduce((a,b) => (b+a), 0)
const add = (item) => () => dispatch({type: 'add', item: item.uuid})
const subtract = (item) => () => dispatch({type: 'subtract', item: item.uuid})
const remove = (item) => () => dispatch({type: 'remove', item: item.uuid})
const clear = () => dispatch({type: 'clear'})
async function createOrder(){
const items = Object.keys(itemState)
.map(uuid => ({ uuid, count: itemState[uuid] }))
const {data: order} = await axios.put(`/api/orders/manual`, {items})
Router.push(`/admin/orders/new/${order.uuid}/address`)
}
return (
<>