From 3ada88f0faf901e05bd56ecff8c4bcb209c06787 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Fri, 21 Jun 2024 15:31:12 +0700
Subject: <iman> update all-promotion

---
 src-migrate/modules/promo/components/PromoList.jsx | 98 ++++++++++++++++++----
 1 file changed, 83 insertions(+), 15 deletions(-)

(limited to 'src-migrate/modules/promo/components/PromoList.jsx')

diff --git a/src-migrate/modules/promo/components/PromoList.jsx b/src-migrate/modules/promo/components/PromoList.jsx
index ed758fbc..fb4b6ee3 100644
--- a/src-migrate/modules/promo/components/PromoList.jsx
+++ b/src-migrate/modules/promo/components/PromoList.jsx
@@ -1,24 +1,92 @@
-import Image from 'next/image';
-import Link from '@/core/components/elements/Link/Link';
-import ProductPromoCard from '../../../../src-migrate/modules/product-promo/components/Card'
+import React, { useState, useEffect } from 'react';
+import ProductPromoCard from '../../../../src-migrate/modules/product-promo/components/Card';
+import { fetchPromoItemsSolr } from '../../../../src/api/promoApi';
+import { Swiper, SwiperSlide } from 'swiper/react';
+import useDevice from '@/core/hooks/useDevice';
+import LogoSpinner from '../../../../src/core/components/elements/Spinner/LogoSpinner'
 
 const PromoList = ({ selectedPromo }) => {
-  let title = '';
-  
-  if (selectedPromo === 'Bundling') {
-    title = 'Kombinasi Kilat Pilihan Kami!';
-  } else if (selectedPromo === 'Loading') {
-    title = 'Belanja Borong Pilihan Kami!';
-  } else if (selectedPromo === 'Merchandise') {
-    title = 'Gratis Merchandise Spesial Indoteknik';
-  }
+  const [title, setTitle] = useState('');
+  const [slug, setSlug] = useState('');
+  const [promoItems, setPromoItems] = useState([]);
+  const [promoData, setPromoData] = useState([]);
+  const [isLoading, setIsLoading] = useState(true);
+  const { isMobile } = useDevice();
+
+  useEffect(() => {
+    if (selectedPromo === 'Bundling') {
+      setTitle('Kombinasi Kilat Pilihan Kami!');
+      setSlug('bundling');
+    } else if (selectedPromo === 'Loading') {
+      setTitle('Belanja Borong Pilihan Kami!');
+      setSlug('discount_loading');
+    } else if (selectedPromo === 'Merchandise') {
+      setTitle('Gratis Merchandise Spesial Indoteknik');
+      setSlug('merchandise');
+    }
+  }, [selectedPromo]);
+
+  useEffect(() => {
+    const fetchPromotions = async () => {
+      setIsLoading(true);
+      try {
+        const items = await fetchPromoItemsSolr(`type_value_s:${slug}`, 0, 50);
+        setPromoItems(items);
+
+        console.log("selectedPromo",selectedPromo)
+        console.log("slug",slug)
+        console.log("items",items)
+        console.log("promoItems",promoItems)
+
+        const promoDataPromises = items.map(async (item) => {
+          try {
+            const response = await fetchPromoItemsSolr(`id:${item.id}`, 0, 10);
+            return response;
+          } catch (fetchError) {
+            return [];
+          }
+        });
+
+        const promoDataArray = await Promise.all(promoDataPromises);
+        const mergedPromoData = promoDataArray.reduce((accumulator, currentValue) => accumulator.concat(currentValue), []);
+        setPromoData(mergedPromoData);
+      } catch (error) {
+        console.error('Error fetching promo items:', error);
+      } finally {
+        setIsLoading(false);
+      }
+    };
+
+    if (slug) {
+      setIsLoading(true);
+      // Reset the promoItems and promoData when slug changes
+      setPromoItems([]);
+      setPromoData([]);
+      fetchPromotions();
+    }
+  }, [slug]);
 
   return (
     <div>
-      <h1 className='text-h-sm md:text-h-lg font-semibold mb-4'>{title}</h1>
-      <div>DISINI CARD {selectedPromo}</div>
+      <h1 className='text-h-sm md:text-h-lg  font-semibold py-4'>{title}</h1>
+      {isLoading ? (
+        <div className="loading-spinner flex justify-center">
+          <LogoSpinner width={48} height={48} />
+        </div>
+      ) : (
+        
+        <Swiper slidesPerView={isMobile ? 1.5 : 3.25} spaceBetween={12} freeMode>
+          {promoData?.map((promotion) => (
+            <SwiperSlide key={promotion.id}>
+              <div className="min-w-[380px] max-w-[380px] mb-[20px] sm:min-w-[330px] md:min-w-[360px] lg:min-w-[380px] xl:min-w-[380px]">
+                <ProductPromoCard promotion={promotion} />
+              </div>
+            </SwiperSlide>
+          ))}
+        </Swiper>
+      )}
     </div>
   );
-}
+};
 
 export default PromoList;
-- 
cgit v1.2.3

