summaryrefslogtreecommitdiff
path: root/src/lib/promotinProgram/components/HomePage.jsx
blob: c0968161512f2ca45ae740a9ef8d64f4de4b4602 (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
import React, { use, useEffect, useState } from 'react'
import { Swiper, SwiperSlide } from 'swiper/react'
import 'swiper/swiper.min.css'
import Image from '@/core/components/elements/Image/Image'
import useDevice from '@/core/hooks/useDevice'
import odooApi from '@/core/api/odooApi'
import { LazyLoadComponent } from 'react-lazy-load-image-component'
import ProductSlider from '@/lib/product/components/ProductSlider'
import { PopularProductSkeleton } from '@/components/skeleton/PopularProductSkeleton'

const { useQuery } = require('react-query')
const { getPromotionHome } = require('../api/homepageApi')
const { getProductPromotionHome } = require('../api/homepageApi')

const HomePage = () => {
  const [activeTab, setActiveTab] = useState(null)
  const [activeId, setActiveId] = useState(null)
  const [activeBanner, setActiveBanner] = useState(null)
  const [parentPromotions, setparentPromotions] = useState(null)

  const { data: titlePromotion } = useQuery('titlePromotion', getPromotionHome)
  const { data: productPromotion, refetch: productPromotionRefetch } = useQuery(
    ['productPromotion', activeId],
    async () => {
      if (!activeId) return null
      return await getProductPromotionHome({ id: activeId })
    }
  )

  useEffect(() => {
    if (titlePromotion && titlePromotion.length > 0) {
      setActiveTab(titlePromotion[0].name)
      setActiveId(titlePromotion[0].id)
      setparentPromotions(titlePromotion)
      setActiveBanner(titlePromotion[0].banner)
      productPromotionRefetch()
    }
  }, [titlePromotion, productPromotionRefetch])

  useEffect(() => {
    if (productPromotion) {
      setparentPromotions((parentPromotions) => {
        return parentPromotions.map((title) => {
          if (title.id === activeId && title.products === undefined) {
            return {
              ...title,
              products: productPromotion
            }
          }
          return title
        })
      })
    }
  }, [productPromotion, activeId])

  const { isMobile, isDesktop } = useDevice()
  const handleTabClick = (id, label, banner) => {
    setActiveTab(label)
    setActiveId(id)
    setActiveBanner(banner)
  }

  return (
    activeBanner && (
      <div className='px-4 sm:px-0'>
        <div className='flex justify-between items-center mb-4'>
          <div className='font-medium sm:text-h-lg'>{activeTab}</div>
        </div>
        <div className='mb-4'>
          <Image src={activeBanner} alt='' className='h-full w-full object-contain object-center' />
        </div>
        <Swiper slidesPerView={isMobile ? 3.5 : 7.5} spaceBetween={isMobile ? 12 : 20}>
          {titlePromotion?.map((item, index) => (
            <SwiperSlide key={index}>
              <button
                className={`py-1 px-2 rounded border flex justify-center items-center h-30 ${
                  activeTab === item.name ? 'border-red-500' : 'border-gray_r-6'
                }`}
                onClick={() => handleTabClick(item.id, item.name, item.banner)}
              >
                {' '}
                <Image
                  src={item.icon}
                  alt=''
                  className='h-full w-full object-contain object-center'
                />
              </button>
            </SwiperSlide>
          ))}
        </Swiper>
        <div className='mt-4 relative  min-h-[150px]'>
          {parentPromotions &&
            parentPromotions?.map((item, index) => (
              <div
                key={index}
                className={`${activeId === item.id ? 'block' : 'hidden'}  rounded-md`}
              >
                {item.products ? (
                  <ProductSlider
                    key={index}
                    products={{
                      products: item.products
                    }}
                  />
                ) : (
                  <PopularProductSkeleton />
                )}
              </div>
            ))}
        </div>
      </div>
    )
  )
}

export default HomePage