From dc204e9cdbcf7faf81dba825db608be2c918589d Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Tue, 9 Jul 2024 16:31:30 +0700
Subject: <iman? add button reject & logic

---
 src/lib/variant/components/VariantCard.jsx | 107 ++++++++++++++++++++++++++---
 1 file changed, 96 insertions(+), 11 deletions(-)

(limited to 'src/lib/variant/components/VariantCard.jsx')

diff --git a/src/lib/variant/components/VariantCard.jsx b/src/lib/variant/components/VariantCard.jsx
index 9f1b5733..c629c987 100644
--- a/src/lib/variant/components/VariantCard.jsx
+++ b/src/lib/variant/components/VariantCard.jsx
@@ -1,16 +1,25 @@
 import { useRouter } from 'next/router'
 import { toast } from 'react-hot-toast'
-
+import useAuth from '@/core/hooks/useAuth';
 import Image from '@/core/components/elements/Image/Image'
 import Link from '@/core/components/elements/Link/Link'
 import { createSlug } from '@/core/utils/slug'
 import currencyFormat from '@/core/utils/currencyFormat'
 import { updateItemCart } from '@/core/utils/cart'
 import whatsappUrl from '@/core/utils/whatsappUrl'
+import {useState } from 'react';
+import rejectProductApi from '../../../lib/transaction/api/rejectProductApi'
+// import {useTransaction} from 'C:\Users\Indoteknik\next-indoteknik\src\lib\transaction\hooks\useTransaction.js'
+import useTransaction from '../../../lib/transaction/hooks/useTransaction';
 
 const VariantCard = ({ product, openOnClick = true, buyMore = false }) => {
   const router = useRouter()
-
+  const id = router.query.id
+  const auth = useAuth();
+  const { transaction } = useTransaction({id});
+  const [isModalOpen, setIsModalOpen] = useState(false);
+  const [selectedProduct, setSelectedProduct] = useState(null);
+  const [reason, setReason] = useState('');
   const addItemToCart = () => {
     toast.success('Berhasil menambahkan ke keranjang', { duration: 1500 })
     updateItemCart({
@@ -19,11 +28,42 @@ const VariantCard = ({ product, openOnClick = true, buyMore = false }) => {
     })
     return
   }
-
+  
   const checkoutItem = () => {
     router.push(`/shop/checkout?product_id=${product.id}&qty=${product.quantity}`)
   }
-
+  
+  const openModal = (product) => {
+    setSelectedProduct(product);
+    setIsModalOpen(true);
+  };
+  
+  const closeModal = () => {
+    setIsModalOpen(false);
+    setSelectedProduct(null);
+    setReason('');
+  };
+  
+  const handleRejectProduct = async () => {
+    try {
+      if (!reason.trim()) {
+        toast.error('Masukkan alasan terlebih dahulu');
+        return;
+      }else{
+        let idSo = transaction?.data.id
+        let idProduct = selectedProduct.id
+        await rejectProductApi({ idSo, idProduct, reason}); 
+        closeModal();
+        toast.success("Produk berhasil di reject")
+        setTimeout(() => {
+          window.location.reload();
+        }, 1500);
+      }
+    } catch (error) {
+      toast.error('Gagal reject produk. Silakan coba lagi.');
+    }
+  };
+  
   const Card = () => (
     <div className='flex gap-x-3'>
       <div className='w-4/12 flex items-center gap-x-2'>
@@ -91,14 +131,59 @@ const VariantCard = ({ product, openOnClick = true, buyMore = false }) => {
             >
               Tambah Keranjang
             </button>
-            <button
-              type='button'
-              onClick={checkoutItem}
-              className='btn-solid-red py-2 px-3 text-caption-1'
-            >
-              Beli Lagi
-            </button>
+            {/* {auth?.feature.soApproval && (auth.webRole == 2 || auth.webRole == 3) && (transaction.data.isReaject == false) && ( */}
+            {auth?.feature.soApproval && (auth.webRole == 2 || auth.webRole == 3) && (router.asPath.includes("/my/quotations/")) && (
+                      
+                          <button
+                            className="bg-red-500 text-white py-1 px-3 rounded"
+                            onClick={() => openModal(product)}
+                          >
+                            Reject
+                          </button>
+                      
+                      )}
+                      {/* {transaction.data.isReaject && (
+                  
+                          <button
+                            className="bg-gray-400 text-white py-1 px-3 rounded"
+                            onClick={() => openModal(product)}
+                            disabled
+                          >
+                            Direject
+                          </button>
+                     
+                      )} */}
+                      {isModalOpen && (
+                        <div className='fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center'>
+                          <div className='bg-white p-4 rounded w-96
+                            ease-in-out opacity-100
+                            transform transition-transform duration-300  scale-100'>
+                            <h2 className='text-lg mb-2'>Berikan Alasan</h2>
+                            <textarea
+                              value={reason}
+                              onChange={(e) => setReason(e.target.value)}
+                              className='w-full p-2 border rounded'
+                              rows='4'
+                            ></textarea>
+                            <div className='mt-4 flex justify-end'>
+                              <button
+                                className='bg-gray-300 text-black py-1 px-3 rounded mr-2'
+                                onClick={closeModal}
+                              >
+                                Batal
+                              </button>
+                              <button
+                                className='bg-red-500 text-white py-1 px-3 rounded'
+                                onClick={handleRejectProduct}
+                              >
+                                Reject
+                              </button>
+                            </div>
+                          </div>
+                        </div>
+                      )}
           </div>
+          
         )}
       </>
     )
-- 
cgit v1.2.3


From e3d0de0ba20eee9d53e95c5525907b099d2b5b04 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Wed, 10 Jul 2024 14:20:29 +0700
Subject: <iman> update reaject button & component position

---
 src/lib/variant/components/VariantCard.jsx | 31 ++++++++++--------------------
 1 file changed, 10 insertions(+), 21 deletions(-)

(limited to 'src/lib/variant/components/VariantCard.jsx')

diff --git a/src/lib/variant/components/VariantCard.jsx b/src/lib/variant/components/VariantCard.jsx
index c629c987..5d95ee70 100644
--- a/src/lib/variant/components/VariantCard.jsx
+++ b/src/lib/variant/components/VariantCard.jsx
@@ -122,7 +122,7 @@ const VariantCard = ({ product, openOnClick = true, buyMore = false }) => {
         <Link href={createSlug('/shop/product/', product.parent.name, product.parent.id)}>
           <Card />
         </Link>
-        {buyMore && (
+        {buyMore  && (!transaction?.data?.productsRejectLine.some(pr => pr.id === product.id)) && (
           <div className='flex justify-end gap-x-2 mb-2'>
             <button
               type='button'
@@ -133,26 +133,15 @@ const VariantCard = ({ product, openOnClick = true, buyMore = false }) => {
             </button>
             {/* {auth?.feature.soApproval && (auth.webRole == 2 || auth.webRole == 3) && (transaction.data.isReaject == false) && ( */}
             {auth?.feature.soApproval && (auth.webRole == 2 || auth.webRole == 3) && (router.asPath.includes("/my/quotations/")) && (
-                      
-                          <button
-                            className="bg-red-500 text-white py-1 px-3 rounded"
-                            onClick={() => openModal(product)}
-                          >
-                            Reject
-                          </button>
-                      
-                      )}
-                      {/* {transaction.data.isReaject && (
-                  
-                          <button
-                            className="bg-gray-400 text-white py-1 px-3 rounded"
-                            onClick={() => openModal(product)}
-                            disabled
-                          >
-                            Direject
-                          </button>
-                     
-                      )} */}
+                      !transaction?.data?.productsRejectLine.some(pr => pr.id === product.id) && (
+                        <button
+                          className="bg-red-500 text-white py-1 px-3 rounded"
+                          onClick={() => openModal(product)}
+                        >
+                          Reject
+                        </button>
+                      )
+                    )}
                       {isModalOpen && (
                         <div className='fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center'>
                           <div className='bg-white p-4 rounded w-96
-- 
cgit v1.2.3

