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 && (
{activeTab}
{titlePromotion?.map((item, index) => ( ))}
{parentPromotions && parentPromotions?.map((item, index) => (
{item.products ? ( ) : ( )}
))}
) ) } export default HomePage