import { ChangeEvent, useEffect, useState } from 'react' import cn from 'classnames' import Image from 'next/image' import Link from 'next/link' import { Trash, Plus, Minus } from '@components/icons' import usePrice from '@bigcommerce/storefront-data-hooks/use-price' import useUpdateItem from '@bigcommerce/storefront-data-hooks/cart/use-update-item' import useRemoveItem from '@bigcommerce/storefront-data-hooks/cart/use-remove-item' import s from './CartItem.module.css' const CartItem = ({ item, currencyCode, }: { item: any currencyCode: string }) => { const { price } = usePrice({ amount: item.extended_sale_price, baseAmount: item.extended_list_price, currencyCode, }) const updateItem = useUpdateItem(item) const removeItem = useRemoveItem() const [quantity, setQuantity] = useState(item.quantity) const [removing, setRemoving] = useState(false) const updateQuantity = async (val: number) => { await updateItem({ quantity: val }) } const handleQuantity = (e: ChangeEvent) => { const val = Number(e.target.value) if (Number.isInteger(val) && val >= 0) { setQuantity(e.target.value) } } const handleBlur = () => { const val = Number(quantity) if (val !== item.quantity) { updateQuantity(val) } } const increaseQuantity = (n = 1) => { const val = Number(quantity) + n if (Number.isInteger(val) && val >= 0) { setQuantity(val) updateQuantity(val) } } const handleRemove = async () => { setRemoving(true) try { // If this action succeeds then there's no need to do `setRemoving(true)` // because the component will be removed from the view await removeItem({ id: item.id }) } catch (error) { setRemoving(false) } } useEffect(() => { // Reset the quantity state if the item quantity changes if (item.quantity !== Number(quantity)) { setQuantity(item.quantity) } }, [item.quantity]) return (
  • Product Image
    {/** TODO: Replace this. No `path` found at Cart */} {item.name}
    {price}
  • ) } export default CartItem