You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.8 KiB
JavaScript

import React from 'react'
import Link from 'next/link'
import useCart from '~/hooks/useCart'
import styles from './style.module.css'
5 years ago
import axios from 'axios'
5 years ago
export default function Card({item}) {
const [cart, setCart] = useCart()
const numInCart = cart?.items?.find(i => i.item.uuid === item.uuid)?.count || 0
let featuredImage = item.images.filter(i=>i.featured)[0]
if(!featuredImage) featuredImage = item.images[0]
5 years ago
const addToCart = async ev => {
if(ev) ev.preventDefault()
const {data: newCart} = await axios.post(`/api/cart/add/${item.uuid}`, {count: 1})
setCart(newCart)
}
return (
<div className={styles.card}>
<h3><Link href={`/store/item/${item.urlslug}`}><a>{item.name}</a></Link></h3>
5 years ago
{featuredImage && (
<div className={styles['card-image']}>
<img src={`/api/images/${featuredImage.uuid}/thumb`} />
</div>
)}
<div className={styles['card-text']}>{item.description}</div>
<ul className={styles['card-details'] + (item.number_in_stock > 0 ? '' : ' ' + styles['out-of-stock'])}>
<li className={styles['number-in-stock']}>
{
item.number_in_stock > 0
? `${item.number_in_stock} pair${item.number_in_stock > 1 ? 's':''} in stock`
: 'Currently out of stock'
}
</li>
5 years ago
<li><Link href={`/store/item/${item.urlslug}`}><a>Details</a></Link></li>
{
item.number_in_stock > 0 && (
<li>
5 years ago
<button className="buttonLink" onClick={addToCart}>Add to Cart</button>
</li>
)
}
5 years ago
{numInCart > 0 && (
<li>
({numInCart} in <Link href="/store/cart"><a>cart</a></Link>)
</li>
)}
</ul>
</div>
)
}