From ecaa66653e57e369b97674ff239177244f8f83ff Mon Sep 17 00:00:00 2001
From: trisusilo48 <tri.susilo@altama.co.id>
Date: Mon, 22 Jul 2024 11:13:59 +0700
Subject: voucher in product card

---
 src/lib/product/components/ProductCard.jsx | 152 +++++++++++++++++------------
 1 file changed, 92 insertions(+), 60 deletions(-)

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

diff --git a/src/lib/product/components/ProductCard.jsx b/src/lib/product/components/ProductCard.jsx
index 98732407..818dbbcf 100644
--- a/src/lib/product/components/ProductCard.jsx
+++ b/src/lib/product/components/ProductCard.jsx
@@ -14,7 +14,15 @@ import useUtmSource from '~/hooks/useUtmSource';
 const ProductCard = ({ product, simpleTitle, variant = 'vertical' }) => {
   const router = useRouter();
   const utmSource = useUtmSource();
+  const [discount, setDiscount] = useState(0);
 
+  let voucherPastiHemat = 0;
+
+  if (product?.voucherPastiHemat.length > 0) {
+    const stringVoucher = product?.voucherPastiHemat[0];
+    const validJsonString = stringVoucher.replace(/'/g, '"');
+    voucherPastiHemat = JSON.parse(validJsonString);
+  }
 
   const callForPriceWhatsapp = whatsappUrl('product', {
     name: product.name,
@@ -38,42 +46,59 @@ const ProductCard = ({ product, simpleTitle, variant = 'vertical' }) => {
     ),
   };
 
+  const hitungDiscountVoucher = () => {
+    let countDiscount = 0;
+    if (voucherPastiHemat.discount_type === 'percentage') {
+      countDiscount = product?.lowestPrice.priceDiscount * (voucherPastiHemat.discount_amount / 100);
+      if (voucherPastiHemat.max_discount > 0 && countDiscount > voucherPastiHemat.max_discount) {
+        countDiscount = voucherPastiHemat.max_discount;
+      }
+    } else {
+      countDiscount = voucherPastiHemat.discount_amount;
+    }
+
+    setDiscount(countDiscount);
+  };
+
+  useEffect(() => {
+    hitungDiscountVoucher();
+  }, []);
+
   if (variant == 'vertical') {
     return (
-      <div className='rounded shadow-sm border border-gray_r-4 bg-white h-[300px] md:h-[350px]'>
+      <div className='rounded shadow-sm border border-gray_r-4 bg-white h-[310px] md:h-[380px]'>
         <Link href={URL.product} className='border-b border-gray_r-4 relative'>
-        <div className="relative">
-          <Image
-            src={image}
-            alt={product?.name}
-            className="gambarA w-full object-contain object-center h-36 sm:h-48"
-          />
-          <div className="absolute top-0 right-0 flex  mt-3">
-            <div className="gambarB ">
-              {product?.isSni && (
-                <ImageNext
-                  src="/images/sni-logo.png"
-                  alt="SNI Logo"
-                  className="w-4 h-5 object-contain object-top   sm:h-6"
-                  width={50}
-                  height={50}
-                />
-              )}
-            </div>
-            <div className="gambarC  ">
-              {product?.isTkdn && (
-                <ImageNext
-                  src="/images/TKDN.png"
-                  alt="TKDN"
-                  className="w-11 h-6 object-contain object-top  ml-1 mr-1 sm:h-6"
-                  width={50}
-                  height={50}
-                />
-              )}
+          <div className='relative'>
+            <Image
+              src={image}
+              alt={product?.name}
+              className='gambarA w-full object-contain object-center h-36 sm:h-48'
+            />
+            <div className='absolute top-0 right-0 flex  mt-3'>
+              <div className='gambarB '>
+                {product?.isSni && (
+                  <ImageNext
+                    src='/images/sni-logo.png'
+                    alt='SNI Logo'
+                    className='w-4 h-5 object-contain object-top   sm:h-6'
+                    width={50}
+                    height={50}
+                  />
+                )}
+              </div>
+              <div className='gambarC  '>
+                {product?.isTkdn && (
+                  <ImageNext
+                    src='/images/TKDN.png'
+                    alt='TKDN'
+                    className='w-11 h-6 object-contain object-top  ml-1 mr-1 sm:h-6'
+                    width={50}
+                    height={50}
+                  />
+                )}
+              </div>
             </div>
           </div>
-        </div>
-
 
           {router.pathname != '/' && product?.flashSale?.id > 0 && (
             <div className='absolute bottom-0 w-full grid'>
@@ -178,6 +203,13 @@ const ProductCard = ({ product, simpleTitle, variant = 'vertical' }) => {
               )}
             </div>
           )}
+          {discount > 0 && (
+            <div className='flex gap-x-1 mb-1 text-sm'>
+              <div className='inline-flex items-center rounded-md bg-green-50 px-2 py-1 text-xs font-medium text-green-700 ring-1 ring-inset ring-green-600/20'>
+                Hemat : {currencyFormat(discount)}
+              </div>
+            </div>
+          )}
 
           <div className='flex w-full items-center gap-x-1 '>
             {product?.stockTotal > 0 && (
@@ -200,37 +232,37 @@ const ProductCard = ({ product, simpleTitle, variant = 'vertical' }) => {
       <div className='flex bg-white'>
         <div className='w-4/12'>
           <Link href={URL.product} className='relative'>
-          <div className="relative">
-            <Image
-              src={image}
-              alt={product?.name}
-              className="gambarA w-full object-contain object-center h-36 sm:h-48"
-            />
-            <div className="absolute top-0 right-0 flex  mt-3">
-              <div className="gambarB ">
-                {product?.isSni && (
-                  <ImageNext
-                    src="/images/sni-logo.png"
-                    alt="SNI Logo"
-                    className="w-4 h-5 object-contain object-top   sm:h-6"
-                    width={50}
-                    height={50}
-                  />
-                )}
-              </div>
-              <div className="gambarC  ">
-                {product?.isTkdn && (
-                  <ImageNext
-                    src="/images/TKDN.png"
-                    alt="TKDN"
-                    className="w-11 h-6 object-contain object-top  ml-1 sm:h-6"
-                    width={50}
-                    height={50}
-                  />
-                )}
+            <div className='relative'>
+              <Image
+                src={image}
+                alt={product?.name}
+                className='gambarA w-full object-contain object-center h-36 sm:h-48'
+              />
+              <div className='absolute top-0 right-0 flex  mt-3'>
+                <div className='gambarB '>
+                  {product?.isSni && (
+                    <ImageNext
+                      src='/images/sni-logo.png'
+                      alt='SNI Logo'
+                      className='w-4 h-5 object-contain object-top   sm:h-6'
+                      width={50}
+                      height={50}
+                    />
+                  )}
+                </div>
+                <div className='gambarC  '>
+                  {product?.isTkdn && (
+                    <ImageNext
+                      src='/images/TKDN.png'
+                      alt='TKDN'
+                      className='w-11 h-6 object-contain object-top  ml-1 sm:h-6'
+                      width={50}
+                      height={50}
+                    />
+                  )}
+                </div>
               </div>
             </div>
-        </div>
             {product.variantTotal > 1 && (
               <div className='absolute badge-gray bottom-1.5 left-1.5'>
                 {product.variantTotal} Varian
-- 
cgit v1.2.3

