diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-03-07 09:18:53 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-03-07 09:18:53 +0700 |
| commit | 6320efdce499d96796e1727d9065d2b1c1c00c53 (patch) | |
| tree | 8ead8b4e2b67329e33ead17be290f49a5b972396 | |
| parent | 23cd1216c5a38fb0f79d34378aa4df0e37fc0782 (diff) | |
fix button buy on product detail
| -rw-r--r-- | src/lib/checkout/components/Checkout.jsx | 27 | ||||
| -rw-r--r-- | src/lib/product/components/Product.jsx | 18 |
2 files changed, 29 insertions, 16 deletions
diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index b4fd33ff..ef0b1d54 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -64,19 +64,26 @@ const Checkout = () => { useEffect(() => { const loadProducts = async () => { - const cart = getCart() - const variantIds = _.filter(cart, (o) => o.selected == true) - .map((o) => o.productId) - .join(',') + let variantIds = '' + let { query } = router + if (query?.productId) { + variantIds = query.productId + } else { + const cart = getCart() + variantIds = _.filter(cart, (o) => o.selected == true) + .map((o) => o.productId) + .join(',') + } + const dataProducts = await CartApi({ variantIds }) const dataProductsQuantity = _.map(dataProducts, (o) => ({ ...o, - quantity: getItemCart({ productId: o.id }).quantity + quantity: query.quantity ? query.quantity : getItemCart({ productId: o.id }).quantity })) setProducts(dataProductsQuantity) } loadProducts() - }, []) + }, [router]) useEffect(() => { if (products) { @@ -128,7 +135,7 @@ const Checkout = () => { toast.error('Gagal melakukan transaksi, terjadi kesalahan internal') return } - + for (const product of products) deleteItemCart({ productId: product.id }) if (paymentMethod == 'midtrans') { const payment = await axios.post( @@ -296,12 +303,6 @@ const Checkout = () => { {isLoading ? 'Loading...' : 'Bayar'} </button> </div> - - <Script - async - src='https://app.sandbox.midtrans.com/snap/snap.js' - data-client-key={process.env.NEXT_PUBLIC_MIDTRANS_CLIENT_KEY} - /> </> ) } diff --git a/src/lib/product/components/Product.jsx b/src/lib/product/components/Product.jsx index 9e33316c..ae476b80 100644 --- a/src/lib/product/components/Product.jsx +++ b/src/lib/product/components/Product.jsx @@ -74,15 +74,21 @@ const Product = ({ product }) => { } }, [informationTab]) - const handleClickCart = () => { + const validAction = () => { + let isValid = true if (!selectedVariant) { toast.error('Pilih varian terlebih dahulu') - return + isValid = false } if (!quantity || quantity < 1 || isNaN(parseInt(quantity))) { toast.error('Jumlah barang minimal 1') - return + isValid = false } + return isValid + } + + const handleClickCart = () => { + if (!validAction()) return updateItemCart({ productId: activeVariant.id, quantity @@ -90,6 +96,11 @@ const Product = ({ product }) => { toast.success('Berhasil menambahkan ke keranjang') } + const handleClickBuy = () => { + if (!validAction()) return + router.push(`/shop/checkout?productId=${activeVariant.id}&quantity=${quantity}`) + } + const toggleWishlist = async () => { if (!auth) { router.push('/login') @@ -197,6 +208,7 @@ const Product = ({ product }) => { <button type='button' className='btn-solid-red flex-1' + onClick={handleClickBuy} > Beli </button> |
