From ba70a6d4ca828b08be4b8a27d3abb3868d85668f Mon Sep 17 00:00:00 2001
From: "HATEC\\SPVDEV001" <tri.susilo@altama.co.id>
Date: Fri, 4 Aug 2023 09:34:45 +0700
Subject: fixing validasi variant qty

---
 src/lib/product/components/Product/ProductDesktop.jsx | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 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 7e1d0d3b..3b21db6b 100644
--- a/src/lib/product/components/Product/ProductDesktop.jsx
+++ b/src/lib/product/components/Product/ProductDesktop.jsx
@@ -54,9 +54,10 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => {
   const setVariantQuantityRef = (variantId) => (element) => {
     if (element) {
       let variantIndex = product.variants.findIndex((varian) => varian.id == variantId)
-      product.variants[variantIndex].quantity = element.value
+      product.variants[variantIndex].quantity = element?.value
     }
     variantQuantityRefs.current[variantId] = element
+    
   }
 
   const validQuantity = (quantity) => {
@@ -106,6 +107,15 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => {
     setAddCartAlert(true)
   }
 
+  const handleQuantityChange = (variantId) => (event) => {
+    const { value } = event.target;
+    const variantIndex = product.variants.findIndex((variant) => variant.id === variantId);
+    if (variantIndex !== -1) {
+      product.variants[variantIndex].quantity = parseInt(value, 10); // Pastikan untuk mengubah ke tipe number jika diperlukan
+      // Lakukan sesuatu jika nilai quantity diubah
+    }
+  };
+
   const handleBuy = (variant) => {
     const quantity = variantQuantityRefs.current[variant].value
     if (!validQuantity(quantity)) return
@@ -579,7 +589,7 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => {
                           type='number'
                           className='form-input w-16 py-2 text-center bg-gray_r-1'
                           ref={setVariantQuantityRef(variant.id)}
-                          onChange={setVariantQuantityRef(variant.id)}
+                          onChange={handleQuantityChange(variant.id)}
                           defaultValue={1}
                         />
                       </td>
-- 
cgit v1.2.3

