From d324d01bb9413a023a158a7bd15ba36bb7beb9bc Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Fri, 29 Nov 2024 14:38:24 +0700 Subject: update performance --- src/lib/home/components/CategoryDynamic.jsx | 176 +++++++++++++++------------ src/lib/home/components/PromotionProgram.jsx | 47 ++----- src/pages/index.jsx | 13 +- 3 files changed, 111 insertions(+), 125 deletions(-) (limited to 'src') diff --git a/src/lib/home/components/CategoryDynamic.jsx b/src/lib/home/components/CategoryDynamic.jsx index cc4f42b7..d72fe1f1 100644 --- a/src/lib/home/components/CategoryDynamic.jsx +++ b/src/lib/home/components/CategoryDynamic.jsx @@ -9,6 +9,7 @@ import 'swiper/css'; import 'swiper/css/navigation'; import 'swiper/css/pagination'; import { Pagination } from 'swiper'; +import { LazyLoadComponent } from 'react-lazy-load-image-component'; const CategoryDynamic = () => { const [categoryManagement, setCategoryManagement] = useState([]); @@ -22,12 +23,17 @@ const CategoryDynamic = () => { if (data) { setCategoryManagement(data); } - setIsLoading(false); }; fetchCategoryData(); }, []); + useEffect(() => { + if (categoryManagement?.length > 0) { + setIsLoading(false); + } + }, [categoryManagement]); + const swiperBanner = { modules: [Pagination], classNames: 'mySwiper', @@ -44,95 +50,103 @@ const CategoryDynamic = () => { {categoryManagement && categoryManagement.map((category) => ( -
-
-

- {category.name} -

- - Lihat Semua - -
+ +
+
+

+ {category.name} +

+ + Lihat Semua + +
- - {category?.categories?.map((subCategory) => ( - -
-
-
- -
-

- {subCategory?.name} -

- - Lihat Semua - -
-
-
- {subCategory.child_frontend_id_i.map( - (childCategory) => ( -
+ + {category?.categories?.map((subCategory) => ( + + +
+
+
+ +
+

+ {subCategory?.name} +

- -
-

- {childCategory.name} -

-
+ Lihat Semua
- ) - )} +
+
+ {subCategory.child_frontend_id_i.map( + (childCategory) => ( + +
+ + +
+

+ {childCategory.name} +

+
+ +
+
+ ) + )} +
+
-
-
- - ))} - -
+ + + ))} + +
+ ))}
diff --git a/src/lib/home/components/PromotionProgram.jsx b/src/lib/home/components/PromotionProgram.jsx index 562fa138..fc23bf78 100644 --- a/src/lib/home/components/PromotionProgram.jsx +++ b/src/lib/home/components/PromotionProgram.jsx @@ -4,56 +4,35 @@ import { bannerApi } from '@/api/bannerApi'; import useDevice from '@/core/hooks/useDevice'; import { Swiper, SwiperSlide } from 'swiper/react'; import BannerPromoSkeleton from '../components/Skeleton/BannerPromoSkeleton'; + import { useEffect, useState } from 'react'; const { useQuery } = require('react-query'); const BannerSection = () => { const { isMobile, isDesktop } = useDevice(); const [data, setData] = useState(null); const [shouldFetch, setShouldFetch] = useState(false); + const [ isLoading, setIsLoading] = useState(true); useEffect(() => { const fetchData = async () => { - const res = await fetch(`/api/hero-banner?type=banner-promotion`); - const { data } = await res.json(); - if (data) { - setData(data); + try { + const res = await fetch(`/api/hero-banner?type=banner-promotion`); + const { data } = await res.json(); + if (data) { + setData(data); + } + } catch (e) { + console.log(e); + } finally { + setIsLoading(false); } }; fetchData(); }, []); - // useEffect(() => { - // const localData = localStorage.getItem('Homepage_promotionProgram'); - // if (localData) { - // setData(JSON.parse(localData)); - // } else { - // setShouldFetch(true); - // } - // }, []); - - // const getPromotionProgram = useQuery( - // 'promotionProgram', - // bannerApi({ type: 'banner-promotion' }), - // { - // enabled: shouldFetch, - // onSuccess: (data) => { - // if (data) { - // localStorage.setItem( - // 'Homepage_promotionProgram', - // JSON.stringify(data) - // ); - // setData(data); - // } - // }, - // } - // ); - const promotionProgram = data; - // if (getPromotionProgram?.isLoading && !data) { - // return ; - // } - if (!data) { + if (isLoading) { return ; } diff --git a/src/pages/index.jsx b/src/pages/index.jsx index 2ec1231a..e685b382 100644 --- a/src/pages/index.jsx +++ b/src/pages/index.jsx @@ -16,7 +16,7 @@ import CategoryPilihan from '../lib/home/components/CategoryPilihan'; // import { getAuth } from '~/libs/auth'; const BasicLayout = dynamic(() => - import('@/core/components/layouts/BasicLayout'),{ssr: false} + import('@/core/components/layouts/BasicLayout') ); const HeroBanner = dynamic(() => import('@/components/ui/HeroBanner'), { loading: () => , @@ -40,24 +40,17 @@ const PreferredBrand = dynamic( const FlashSale = dynamic( () => import('@/lib/flashSale/components/FlashSale'), - { - loading: () => , - } ); const ProgramPromotion = dynamic( - () => import('@/lib/home/components/PromotionProgram'), - { - loading: () => , - } + () => import('@/lib/home/components/PromotionProgram') ); const BannerSection = dynamic(() => import('@/lib/home/components/BannerSection') ); const CategoryHomeId = dynamic( - () => import('@/lib/home/components/CategoryHomeId'), - { ssr: false } + () => import('@/lib/home/components/CategoryHomeId') ); const CategoryDynamic = dynamic(() => -- cgit v1.2.3