From f09a72f90c049b5e1a8478f737062a24d9c7a3df Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Tue, 8 Oct 2024 14:56:00 +0700 Subject: cache localstorage --- src/lib/home/components/BannerSection.jsx | 34 ++++++++++++++++++++---- src/lib/home/components/PromotionProgram.jsx | 39 ++++++++++++++++++++++------ 2 files changed, 60 insertions(+), 13 deletions(-) (limited to 'src/lib/home/components') diff --git a/src/lib/home/components/BannerSection.jsx b/src/lib/home/components/BannerSection.jsx index f83c36fc..60d38f8f 100644 --- a/src/lib/home/components/BannerSection.jsx +++ b/src/lib/home/components/BannerSection.jsx @@ -1,18 +1,42 @@ import Link from '@/core/components/elements/Link/Link'; import Image from 'next/image'; +import { useEffect, useState } from 'react'; +import { bannerApi } from '../../../api/bannerApi'; const { useQuery } = require('react-query'); const { default: bannerSectionApi } = require('../api/bannerSectionApi'); const BannerSection = () => { - const fetchBannerSection = async () => await bannerSectionApi(); - const bannerSection = useQuery('bannerSection', fetchBannerSection); + const [data, setData] = useState(null); + const [shouldFetch, setShouldFetch] = useState(false); + + useEffect(() => { + const localData = localStorage.getItem('Homepage_bannerSection'); + if (localData) { + setData(JSON.parse(localData)); + }else{ + setShouldFetch(true); + } + }, []); + + // const fetchBannerSection = async () => await bannerSectionApi(); + const getBannerSection = useQuery('bannerSection', bannerApi({ type: 'home-banner' }), { + enabled: shouldFetch, + onSuccess: (data) => { + if (data) { + localStorage.setItem('Homepage_bannerSection', JSON.stringify(data)); + setData(data); + } + }, + }); + + const bannerSection = data; return ( - bannerSection.data && - bannerSection.data?.length > 0 && ( + bannerSection && + bannerSection?.length > 0 && (
- {bannerSection.data?.map((banner) => ( + {bannerSection?.map((banner) => ( { - const promotionProgram = useQuery( + const { isMobile, isDesktop } = useDevice(); + const [data, setData] = useState(null); + const [shouldFetch, setShouldFetch] = useState(false); + + useEffect(() => { + const localData = localStorage.getItem('Homepage_promotionProgram'); + if (localData) { + setData(JSON.parse(localData)); + }else{ + setShouldFetch(true); + } + },[]) + + const getPromotionProgram = useQuery( 'promotionProgram', - bannerApi({ type: 'banner-promotion' }) + bannerApi({ type: 'banner-promotion' }),{ + enabled: shouldFetch, + onSuccess: (data) => { + if (data) { + localStorage.setItem('Homepage_promotionProgram', JSON.stringify(data)); + setData(data); + } + } + } ); - const { isMobile, isDesktop } = useDevice(); - if (promotionProgram.isLoading) { + const promotionProgram = data + + if (getPromotionProgram?.isLoading && !data) { return ; } @@ -40,10 +63,10 @@ const BannerSection = () => { )}
{isDesktop && - promotionProgram.data && - promotionProgram.data?.length > 0 && ( + promotionProgram && + promotionProgram?.length > 0 && (
- {promotionProgram.data?.map((banner) => ( + {promotionProgram?.map((banner) => ( { {isMobile && ( - {promotionProgram.data?.map((banner) => ( + {promotionProgram?.map((banner) => (