From c472261f34388a0b76c3e21fec494b8d5f304715 Mon Sep 17 00:00:00 2001 From: Mqdd Date: Wed, 26 Nov 2025 13:47:31 +0700 Subject: popular product now ssr --- src/api/productApi.js | 21 ++- src/components/ui/PopularProduct.jsx | 110 ++++++++-------- src/pages/index.jsx | 248 +++++++++++++++++++---------------- 3 files changed, 213 insertions(+), 166 deletions(-) diff --git a/src/api/productApi.js b/src/api/productApi.js index dc96a77e..b5f47bcf 100644 --- a/src/api/productApi.js +++ b/src/api/productApi.js @@ -1,5 +1,6 @@ import axios from 'axios'; +// CLIENT MODE → untuk useQuery export const popularProductApi = () => { return async () => { const today = new Date(); @@ -7,9 +8,25 @@ export const popularProductApi = () => { (today - new Date(today.getFullYear(), 0, 0)) / 86400000 ); const page = (dayOfYear % 24) + 1; - const dataPopularProducts = await axios( + + const res = await axios( `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?q=*&page=${page}&orderBy=stock&priceFrom=1` ); - return dataPopularProducts.data.response; + return res.data.response; }; }; + +// SERVER MODE → untuk SSR +export async function popularProductApiSSR() { + const today = new Date(); + const dayOfYear = Math.floor( + (today - new Date(today.getFullYear(), 0, 0)) / 86400000 + ); + const page = (dayOfYear % 24) + 1; + + const res = await axios( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?q=*&page=${page}&orderBy=stock&priceFrom=1` + ); + + return res.data.response || null; +} diff --git a/src/components/ui/PopularProduct.jsx b/src/components/ui/PopularProduct.jsx index 92b2a1b6..0740798d 100644 --- a/src/components/ui/PopularProduct.jsx +++ b/src/components/ui/PopularProduct.jsx @@ -1,61 +1,63 @@ -import { popularProductApi } from '@/api/productApi' -import MobileView from '@/core/components/views/MobileView' -import ProductSlider from '@/lib/product/components/ProductSlider' -import { useQuery } from 'react-query' -import { PopularProductSkeleton } from '../skeleton/PopularProductSkeleton' -import DesktopView from '@/core/components/views/DesktopView' -import ProductCard from '@/lib/product/components/ProductCard' -import Link from '@/core/components/elements/Link/Link' +import MobileView from '@/core/components/views/MobileView'; +import DesktopView from '@/core/components/views/DesktopView'; +import ProductSlider from '@/lib/product/components/ProductSlider'; +import ProductCard from '@/lib/product/components/ProductCard'; +import Link from '@/core/components/elements/Link/Link'; +import { PopularProductSkeleton } from '../skeleton/PopularProductSkeleton'; +import { useQuery } from 'react-query'; +import { popularProductApi } from '@/api/productApi'; -const PopularProduct = () => { - const popularProduct = useQuery('popularProduct', popularProductApi()) +export default function PopularProduct({ initialData }) { + const query = useQuery( + 'popularProducts', + popularProductApi(), + { + initialData: initialData ? { products: initialData.products } : undefined, + refetchOnMount: false + } + ); - if (popularProduct.isLoading) return + if (query.isLoading) return ; + + const data = query.data; + + if (!data) return null; return ( - popularProduct.data && ( - <> - -
-

- Produk Ready Stock -

- -

Lihat Semua

-
- + <> + {/* Mobile */} + +
+
+

Produk Ready Stock

+ +

Lihat Semua

+
- - - -
-
-

- Produk Ready Stock -

- -

Lihat Semua

- -
-
- {popularProduct.data && - popularProduct.data.products.map((product) => ( -
- -
- ))} -
+ + +
+ + + {/* Desktop */} + +
+
+

Produk Ready Stock

+ +

Lihat Semua

+
- - - ) - ) -} -export default PopularProduct +
+ {data.products.map((product) => ( +
+ +
+ ))} +
+
+
+ + ); +} diff --git a/src/pages/index.jsx b/src/pages/index.jsx index 28db4d3e..fd960e8a 100644 --- a/src/pages/index.jsx +++ b/src/pages/index.jsx @@ -1,7 +1,6 @@ import { HeroBannerSkeleton } from '@/components/skeleton/BannerSkeleton'; import { PopularProductSkeleton } from '@/components/skeleton/PopularProductSkeleton'; import Seo from '@/core/components/Seo'; -import DelayRender from '@/core/components/elements/DelayRender/DelayRender'; import DesktopView from '@/core/components/views/DesktopView'; import MobileView from '@/core/components/views/MobileView'; import PreferredBrandSkeleton from '@/lib/home/components/Skeleton/PreferredBrandSkeleton'; @@ -10,77 +9,118 @@ import dynamic from 'next/dynamic'; import { useRef } from 'react'; import { getAuth } from '~/libs/auth'; import MediaNews from '../lib/home/components/MediaNews'; +import { popularProductApi, popularProductApiSSR } from '@/api/productApi'; +/* ============================ + * DYNAMIC IMPORT SETTINGS + * ============================ */ + +// Layout const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout') ); -const PagePopupIformation = dynamic(() => - import('~/modules/popup-information'), { - ssr: false - } +// Popup +const PagePopupIformation = dynamic( + () => import('~/modules/popup-information'), + { ssr: false } ); -const CategoryPilihan = dynamic(() => - import('../lib/home/components/CategoryPilihan') +// CATEGORY (SSR TRUE) +const CategoryPilihan = dynamic( + () => import('../lib/home/components/CategoryPilihan'), + { ssr: true } ); -const HeroBanner = dynamic(() => import('@/components/ui/HeroBanner'), { - loading: () => , -}); -const HeroBannerSecondary = dynamic( - () => import('@/components/ui/HeroBannerSecondary'), - { - loading: () => , - } + +const CategoryDynamic = dynamic( + () => import('@/lib/home/components/CategoryDynamic'), + { ssr: true } +); + +const CategoryDynamicMobile = dynamic( + () => import('@/lib/home/components/CategoryDynamicMobile'), + { ssr: true } +); + +const CategoryHomeId = dynamic( + () => import('@/lib/home/components/CategoryHomeId'), + { ssr: true } +); + +// PRODUCT (SSR TRUE) +const PopularProduct = dynamic( + () => import('@/components/ui/PopularProduct'), + { loading: () => , ssr: true } ); -const PopularProduct = dynamic(() => import('@/components/ui/PopularProduct'), { - loading: () => , -}); const PreferredBrand = dynamic( () => import('@/lib/home/components/PreferredBrand'), - { - loading: () => , - } + { loading: () => , ssr: true } ); const FlashSale = dynamic( () => import('@/lib/flashSale/components/FlashSale'), + { ssr: true } ); const ProgramPromotion = dynamic( - () => import('@/lib/home/components/PromotionProgram') + () => import('@/lib/home/components/PromotionProgram'), + { ssr: true } ); -const BannerSection = dynamic(() => - import('@/lib/home/components/BannerSection') -); -const CategoryHomeId = dynamic( - () => import('@/lib/home/components/CategoryHomeId') +// CSR ONLY COMPONENTS +const HeroBanner = dynamic( + () => import('@/components/ui/HeroBanner'), + { loading: () => , ssr: false } ); -const CategoryDynamic = dynamic(() => - import('@/lib/home/components/CategoryDynamic') +const HeroBannerSecondary = dynamic( + () => import('@/components/ui/HeroBannerSecondary'), + { loading: () => , ssr: false } ); -const CategoryDynamicMobile = dynamic(() => - import('@/lib/home/components/CategoryDynamicMobile') +const BannerSection = dynamic( + () => import('@/lib/home/components/BannerSection'), + { ssr: false } ); const CustomerReviews = dynamic( () => import('@/lib/review/components/CustomerReviews'), { ssr: false } -); // need to ssr:false -const ServiceList = dynamic(() => import('@/lib/home/components/ServiceList'), { - ssr: false, -}); // need to ssr: false +); + +const ServiceList = dynamic( + () => import('@/lib/home/components/ServiceList'), + { ssr: false } +); + + +/* ===================================== + * SERVER SIDE PROPS (HARUS DI LUAR) + * ===================================== */ -export default function Home({ categoryId }) { + export async function getServerSideProps(context) { + const auth = getAuth(context.req); + + const popularProducts = await popularProductApiSSR(); + + return { + props: { + auth, + popularProducts + } + }; + } + + +/* ============================ + * MAIN PAGE COMPONENT + * ============================ */ + +export default function Home({ auth, popularProducts }) { const bannerRef = useRef(null); const wrapperRef = useRef(null); - const auth = getAuth(); - const handleOnLoad = () => { wrapperRef.current.style.height = bannerRef.current?.querySelector(':first-child')?.clientHeight + 'px'; @@ -90,121 +130,109 @@ export default function Home({ categoryId }) { <> + {/* DESKTOP */} -
+ +
-
+
-
+ +
-
- - - + +
+
-
+
-
+ +
+ {!auth?.feature?.soApproval && ( <> - - - - - - + + )} - {/* */} + +
+ + {/* MOBILE */} - - - -
- - - - - -
- -
-
+ + +
+ + + +
+ +
+ {!auth?.feature?.soApproval && ( <> - - - - - - + + )} - - {/* */} - - - - - - - - - - - - - - - + + + + + + + + + +
-- cgit v1.2.3