From 8b7db0f3ef5058f6e41996fd162a805ab9147cc6 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Fri, 21 Jun 2024 16:16:34 +0700
Subject: <iman> update side banner & footer banner search

---
 src-migrate/modules/side-banner/index.tsx | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

(limited to 'src-migrate/modules/side-banner/index.tsx')

diff --git a/src-migrate/modules/side-banner/index.tsx b/src-migrate/modules/side-banner/index.tsx
index be52c554..80a57a00 100644
--- a/src-migrate/modules/side-banner/index.tsx
+++ b/src-migrate/modules/side-banner/index.tsx
@@ -1,29 +1,31 @@
-import Link from "next/link"
-import { useQuery } from "react-query"
-import Image from "~/components/ui/image"
-import { getBanner } from "~/services/banner"
+import React, { useMemo } from "react";
+import Link from "next/link";
+import { useQuery } from "react-query";
+import Image from "~/components/ui/image";
+import { getBanner } from "~/services/banner";
+import { getRandomInt } from '@/utils/getRandomInt';
 
 const SideBanner = () => {
   const fetchSideBanner = useQuery({
     queryKey: 'sideBanner',
     queryFn: () => getBanner({ type: 'side-banner-search' })
-  })
+  });
 
-  const banner = fetchSideBanner?.data?.[0] || false
+  const length = useMemo(() => fetchSideBanner.data?.length, [fetchSideBanner.data]);
+  const randomIndex = useMemo(() => getRandomInt(length), [length]);
+  const banner = fetchSideBanner?.data?.[randomIndex] || false;
 
   return banner && (
     <>
-      {banner.url && (
+      {banner.url ? (
         <Link href={banner.url}>
           <Image src={banner.image} alt={banner.name} width={315} height={450} className='object-cover object-center rounded-lg' />
         </Link>
-      )}
-
-      {!banner.url && (
+      ) : (
         <Image src={banner.image} alt={banner.name} width={315} height={450} className='object-cover object-center rounded-lg' />
       )}
     </>
-  )
+  );
 }
 
-export default SideBanner
\ No newline at end of file
+export default SideBanner;
-- 
cgit v1.2.3

