summaryrefslogtreecommitdiff
path: root/src-migrate/modules/promo/components/PromoList.jsx
blob: 7187e9d7da0b14c7443200bddffe37ec2bdd8d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import React, { useEffect, useState } 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';
import usePromoStore from './promoStore'; // Sesuaikan dengan path yang sesuai
import { ChevronRightIcon } from '@heroicons/react/24/outline';

const PromoList = ({ selectedPromo }) => {
  const {
    title,
    slug,
    promoItems,
    promoData,
    isLoading,
    setTitle,
    setSlug,
    setPromoItems,
    setPromoData,
    setIsLoading,
  } = usePromoStore();

  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, setTitle, setSlug]);

  useEffect(() => {
    const fetchPromotions = async () => {
      setIsLoading(true);
      try {
        const items = await fetchPromoItemsSolr(`type_value_s:${slug}`, 0, 10); 
        setPromoItems(items);

        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);
      setPromoItems([]);
      setPromoData([]);
      fetchPromotions();
    }
  }, [slug, setPromoItems, setPromoData, setIsLoading]);

 

  const navigateToPromoPage = () => {
    // Fungsi untuk menavigasikan pengguna ke halaman promo yang sesuai dengan slug
    window.location.href = '/shop/promo/' + slug;
  };

  return (
    <div className='h-[420px]'>
      <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>
          ))}
         <SwiperSlide>
              <div className='rounded-lg flex items-center justify-center flex-col cursor-pointer  h-96 text-red-500 font-bold border-2 min-w-[380px] max-w-[380px] mb-[20px] sm:min-w-[330px] md:min-w-[360px] lg:min-w-[380px] xl:min-w-[380px]' onClick={navigateToPromoPage}>
                <ChevronRightIcon className="w-48 h-48 ml-2 font-normal text-red-500 hover:text-red-400 transition duration-300 transform hover:scale-125" />
                <span className=''>
                  Lihat lebih banyak
                </span>

                {/* <button className="mt-4 text-black font-bold py-2 px-4 rounded min-w-[380px] max-w-[380px] mb-[20px] sm:min-w-[330px] md:min-w-[360px] lg:min-w-[380px] xl:min-w-[380px]" onClick={navigateToPromoPage}>
                </button> */}
              </div>
            </SwiperSlide> 
          
        </Swiper>
      )}
    </div>
  );
};

export default PromoList;
// border-radius: 0.5rem;
//     border-width: 1px;
//     --tw-border-opacity: 1;
//     border-color: rgb(219 219 219 / var(--tw-border-opacity));