From eead8eec2e37a2edf3c431664cacdcc502a64364 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Wed, 26 Jun 2024 11:19:27 +0700
Subject: bug fix side & footer banner

---
 src-migrate/modules/side-banner/index.tsx | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 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..6214edfb 100644
--- a/src-migrate/modules/side-banner/index.tsx
+++ b/src-migrate/modules/side-banner/index.tsx
@@ -1,29 +1,30 @@
-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
+  });
+  // ubah dari static menjadid dynamic dengan menggunakan random index
+  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

