diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-03-18 10:17:16 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-03-18 10:17:16 +0700 |
| commit | 62efd1afc02422545579c529cd954d0c157889ee (patch) | |
| tree | 09fa6a45b1b0f31af4e019ac6c4437721774e1cf /src/lib/product/components | |
| parent | ff9e6c43f451ed933b0cb8d35623bf3b6a1de1c4 (diff) | |
navbar user dropdown, add to cart
Diffstat (limited to 'src/lib/product/components')
| -rw-r--r-- | src/lib/product/components/Product.jsx | 4 | ||||
| -rw-r--r-- | src/lib/product/components/ProductDesktop.jsx | 36 |
2 files changed, 36 insertions, 4 deletions
diff --git a/src/lib/product/components/Product.jsx b/src/lib/product/components/Product.jsx index d1586ef9..9b41f5c3 100644 --- a/src/lib/product/components/Product.jsx +++ b/src/lib/product/components/Product.jsx @@ -18,10 +18,10 @@ const Product = ({ product }) => { } const data = { product_id: product.id } await createOrDeleteWishlistApi({ data }) - if (wishlist.data.productTotal > 0) { + if (wishlist?.data?.productTotal > 0) { toast.success('Berhasil menghapus dari wishlist') } else { - toast.success('Berhasil menambahkan ke wishlist') + toast.error('Terjadi kesalahan internal, gagal menambahkan ke wishlist') } wishlist.refetch() } diff --git a/src/lib/product/components/ProductDesktop.jsx b/src/lib/product/components/ProductDesktop.jsx index 1272237b..f37d900c 100644 --- a/src/lib/product/components/ProductDesktop.jsx +++ b/src/lib/product/components/ProductDesktop.jsx @@ -6,6 +6,8 @@ import { HeartIcon } from '@heroicons/react/24/outline' import { Fragment, useEffect, useRef, useState } from 'react' import LazyLoad from 'react-lazy-load' import ProductSimilar from './ProductSimilar' +import { toast } from 'react-hot-toast' +import { updateItemCart } from '@/core/utils/cart' const ProductDesktop = ({ product, wishlist, toggleWishlist }) => { const [variantQuantity, setVariantQuantity] = useState(null) @@ -34,6 +36,28 @@ const ProductDesktop = ({ product, wishlist, toggleWishlist }) => { } } + const validAction = (variantId) => { + let isValid = true + if ( + !variantQuantity[variantId] || + variantQuantity[variantId] < 1 || + isNaN(parseInt(variantQuantity[variantId])) + ) { + toast.error('Jumlah barang minimal 1') + isValid = false + } + return isValid + } + + const handleAddToCart = (variantId) => { + if (!validAction(variantId)) return + updateItemCart({ + productId: variantId, + quantity: variantQuantity[variantId] + }) + toast.success('Berhasil menambahkan ke keranjang') + } + const productSimilarQuery = [ product?.name.replace(/[()/"&]/g, ''), `fq=-product_id:${product.id}`, @@ -168,8 +192,16 @@ const ProductDesktop = ({ product, wishlist, toggleWishlist }) => { /> </td> <td className='flex gap-x-3'> - <button className='flex-1 py-2 btn-yellow'>Keranjang</button> - <button className='flex-1 py-2 btn-solid-red'>Beli</button> + <button + type='button' + onClick={() => handleAddToCart(variant.id)} + className='flex-1 py-2 btn-yellow' + > + Keranjang + </button> + <button type='button' className='flex-1 py-2 btn-solid-red'> + Beli + </button> </td> </tr> ))} |
