import React from 'react' import Router from 'next/router' import Link from 'next/link' import axios from 'axios' import Head from 'next/head' import {DateTime} from 'luxon' import useCart, {useSetCart} from '~/hooks/useCart' import {FormController, Button} from '~/components/form' import Table from '~/components/table' export default function Cart(){ const cart = useCart() const setCart = useSetCart() const numItems = (cart?.items) ? cart.items.length : 0 cart?.items?.forEach(({item, count}) => { const stock = item.stock = {} stock.is_preorder = item.preorder_availability_date && count > item.number_in_stock stock.available = stock.is_preorder ? item.preorder_maximum - -Math.min(0, item.number_in_stock) : item.number_in_stock stock.has_enough = count <= stock.available }) const allInStock = !cart?.items?.some(item => !(item.item.number_in_stock > 0 || item.item.preorder_availability_date) ) const allHaveEnough = !cart?.items?.some(item => !item.item.stock.has_enough) const preorderItems = cart?.items?.filter(({item, count}) => count > item.number_in_stock && item.preorder_availability_date).map(({item}) => DateTime.fromISO(item.preorder_availability_date)) || [] preorderItems.sort((b, a) => a < b ? -1 : a > b ? 1 : 0) const latestPreorder = preorderItems[0]?.toLocaleString(DateTime.DATE_FULL) const handleRemove = id => async ev => { if(ev) ev.preventDefault() const {data} = await axios.post(`/api/cart/remove/${id}`) setCart(data) } // We can't let the form controller do this as it shouldn't actually pass a body const handleCreateTransaction = async () => { await axios.put(`/api/orders`) Router.push('/store/checkout') } return ( <>
Note: Your cart contains one or more pre-order items. If you place an order with these items in your cart, it will not ship until at least {latestPreorder}
)}