diff --git a/components/cart/CartItem/CartItem.module.css b/components/cart/CartItem/CartItem.module.css new file mode 100644 index 000000000..b6a8858b1 --- /dev/null +++ b/components/cart/CartItem/CartItem.module.css @@ -0,0 +1,9 @@ +.quantity { + appearance: textfield; + @apply w-6 border-gray-300 border mx-3 rounded text-center text-sm; +} + +.quantity::-webkit-outer-spin-button, +.quantity::-webkit-inner-spin-button { + @apply appearance-none m-0; +} diff --git a/components/cart/CartItem/CartItem.tsx b/components/cart/CartItem/CartItem.tsx index 692217979..87da31bd3 100644 --- a/components/cart/CartItem/CartItem.tsx +++ b/components/cart/CartItem/CartItem.tsx @@ -1,6 +1,9 @@ import { Trash } from '@components/icon' import { useCommerce } from '@lib/bigcommerce' +import useUpdateItem from '@lib/bigcommerce/cart/use-update-item' +import { useEffect, useState } from 'react' import formatVariantPrice from 'utils/format-item-price' +import styles from './CartItem.module.css' const CartItem = ({ item, @@ -10,12 +13,35 @@ const CartItem = ({ currencyCode: string }) => { const { locale } = useCommerce() + const updateItem = useUpdateItem() + const [quantity, setQuantity] = useState(item.quantity) const { price } = formatVariantPrice({ listPrice: item.extended_list_price, salePrice: item.extended_sale_price, currencyCode, locale, }) + const handleBlur = async () => { + const val = Number(quantity) + + if (val !== item.quantity) { + const data = await updateItem({ + itemId: item.id, + item: { + productId: item.product_id, + variantId: item.variant_id, + quantity: val, + }, + }) + } + } + + useEffect(() => { + // Reset the quantity state if the item quantity changes + if (item.quantity !== quantity) { + setQuantity(item.quantity) + } + }, [item.quantity]) console.log('ITEM', item) @@ -27,8 +53,17 @@ const CartItem = ({