|
|
@ -1,12 +1,16 @@
|
|
|
|
import React from 'react'
|
|
|
|
import React from 'react'
|
|
|
|
|
|
|
|
import Link from 'next/link'
|
|
|
|
import axios from 'axios'
|
|
|
|
import axios from 'axios'
|
|
|
|
import Head from 'next/head'
|
|
|
|
import Head from 'next/head'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import useCart from '../../hooks/useCart'
|
|
|
|
import {FormController, Input, Button} from '~/components/form'
|
|
|
|
import {FormController, Input, Button} from '~/components/form'
|
|
|
|
import Table from '~/components/table'
|
|
|
|
import Table from '~/components/table'
|
|
|
|
|
|
|
|
|
|
|
|
export default function Cart({cart, setCart}){
|
|
|
|
export default function Cart(){
|
|
|
|
|
|
|
|
const [cart, setCart] = useCart()
|
|
|
|
const numItems = (cart?.items) ? cart.items.length : 0
|
|
|
|
const numItems = (cart?.items) ? cart.items.length : 0
|
|
|
|
|
|
|
|
const allInStock = !cart?.items.some(item => !item.number_in_stock || item.number_in_stock < 1)
|
|
|
|
|
|
|
|
|
|
|
|
const handleRemove = id => async ev => {
|
|
|
|
const handleRemove = id => async ev => {
|
|
|
|
if(ev) ev.preventDefault()
|
|
|
|
if(ev) ev.preventDefault()
|
|
|
@ -28,7 +32,12 @@ export default function Cart({cart, setCart}){
|
|
|
|
numItems > 0
|
|
|
|
numItems > 0
|
|
|
|
?<Table
|
|
|
|
?<Table
|
|
|
|
columns={[
|
|
|
|
columns={[
|
|
|
|
{name: 'Item', extractor: row => row.item.name},
|
|
|
|
{name: 'Item', extractor: row => (
|
|
|
|
|
|
|
|
<>
|
|
|
|
|
|
|
|
<Link href={`/store/item/${row.item.urlslug}`}><a>{row.item.name}</a></Link>
|
|
|
|
|
|
|
|
{(!row.item.number_in_stock || row.item.number_in_stock < 1) && <strong style={{marginLeft: '6px'}}>Out of stock</strong>}
|
|
|
|
|
|
|
|
</>
|
|
|
|
|
|
|
|
)},
|
|
|
|
{name: 'Quantity in Cart', extractor: row => row.count},
|
|
|
|
{name: 'Quantity in Cart', extractor: row => row.count},
|
|
|
|
{name: 'Price Each', extractor: row => '$' + (row.item.price_cents / 100).toFixed(2)},
|
|
|
|
{name: 'Price Each', extractor: row => '$' + (row.item.price_cents / 100).toFixed(2)},
|
|
|
|
{name: 'Total Price', extractor: row => '$' + (row.count * row.item.price_cents / 100).toFixed(2)},
|
|
|
|
{name: 'Total Price', extractor: row => '$' + (row.count * row.item.price_cents / 100).toFixed(2)},
|
|
|
@ -63,7 +72,7 @@ export default function Cart({cart, setCart}){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
<FormController>
|
|
|
|
<FormController>
|
|
|
|
<Button enabled={!!(cart?.items?.length)} type="submit">Proceed to Checkout</Button>
|
|
|
|
<Button enabled={!!(cart?.items?.length) && allInStock} type="submit">Proceed to Checkout</Button>
|
|
|
|
</FormController>
|
|
|
|
</FormController>
|
|
|
|
</>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
)
|
|
|
|