From a3d4259a70ca24dadf1e86d392efc05818d9327e Mon Sep 17 00:00:00 2001
From: "HATEC\\SPVDEV001" <tri.susilo@altama.co.id>
Date: Wed, 26 Jul 2023 13:21:07 +0700
Subject: fixing jika beli langsung

---
 .../product/components/Product/ProductDesktop.jsx  | 49 ++++++++++++----------
 1 file changed, 28 insertions(+), 21 deletions(-)

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

diff --git a/src/lib/product/components/Product/ProductDesktop.jsx b/src/lib/product/components/Product/ProductDesktop.jsx
index 2a523503..9be65bfb 100644
--- a/src/lib/product/components/Product/ProductDesktop.jsx
+++ b/src/lib/product/components/Product/ProductDesktop.jsx
@@ -63,7 +63,6 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => {
     variantQuantityRefs.current[variantId] = element
   }
 
-
   const validQuantity = (quantity) => {
     let isValid = true
     if (!quantity || quantity < 1 || isNaN(parseInt(quantity))) {
@@ -73,38 +72,46 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => {
     return isValid
   }
 
-  const handleAddToCart = (variantId) => {
-    if (!auth) {
-      router.push(`/login?next=/shop/product/${slug}`)
-      return
-    }
-    const quantity = variantQuantityRefs.current[variantId].value
-
-    if (!validQuantity(quantity)) return
-    if(product.variants.length > 1){
+  const updateCart = (variantId, quantity) => {
+    if (product.variants.length > 1) {
       let variantIndex = product.variants.findIndex((varian) => varian.id == variantId)
       updateItemCart({
         productId: variantId,
         quantity,
         programLineId: product.variants[variantIndex].programActive,
-        selected: true
+        selected: true,
+        source: 'buy'
       })
-    }else{
+    } else {
       updateItemCart({
         productId: variantId,
         quantity,
         programLineId: promotionActiveId,
-        selected: true
+        selected: true,
+        source: 'buy'
       })
     }
-   
+  }
+
+  const handleAddToCart = (variantId) => {
+    if (!auth) {
+      router.push(`/login?next=/shop/product/${slug}`)
+      return
+    }
+    const quantity = variantQuantityRefs.current[variantId].value
+
+    if (!validQuantity(quantity)) return
+
+    updateCart(variantId, quantity)
     setAddCartAlert(true)
   }
 
   const handleBuy = (variant) => {
     const quantity = variantQuantityRefs.current[variant].value
     if (!validQuantity(quantity)) return
-    router.push(`/shop/checkout?productId=${variant}&quantity=${quantity}`)
+
+    updateCart(variant, quantity)
+    router.push(`/shop/checkout?source=buy`)
   }
 
   const variantSectionRef = useRef(null)
@@ -351,11 +358,11 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => {
                   <a
                     href={whatsappUrl('product', {
                       name: product.name,
-                      manufacture: product.manufacture?.name ,
+                      manufacture: product.manufacture?.name,
                       url: createSlug('/shop/product/', product.name, product.id, true)
                     })}
                     className='text-danger-500 underline'
-                    rel="noopener noreferrer"
+                    rel='noopener noreferrer'
                     target='_blank'
                   >
                     klik disini
@@ -460,8 +467,8 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => {
                               url: createSlug('/shop/product/', product.name, product.id, true)
                             })}
                             className='text-danger-500 font-medium'
-                            target="_blank"
-                            rel="noreferrer noopener"
+                            target='_blank'
+                            rel='noreferrer noopener'
                           >
                             Tanya Admin
                           </a>
@@ -526,11 +533,11 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => {
                             <a
                               href={whatsappUrl('product', {
                                 name: variant.name,
-                                manufacture: product.manufacture?.name ,
+                                manufacture: product.manufacture?.name,
                                 url: createSlug('/shop/product/', product.name, product.id, true)
                               })}
                               className='text-red_r-11'
-                              rel="noopener noreferrer"
+                              rel='noopener noreferrer'
                               target='_blank'
                             >
                               Call for price
-- 
cgit v1.2.3

