From 6320efdce499d96796e1727d9065d2b1c1c00c53 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Tue, 7 Mar 2023 09:18:53 +0700
Subject: fix button buy on product detail

---
 src/lib/product/components/Product.jsx | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

(limited to 'src/lib/product/components/Product.jsx')

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>
-- 
cgit v1.2.3

