From 0d3c0cf6a00ef81bfdb944490e48f16af41fc029 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Wed, 13 Nov 2024 10:08:16 +0700
Subject: <iman> add radis

---
 src/components/ui/HeroBannerSecondary.jsx | 69 ++++++++++++++++++++-----------
 1 file changed, 44 insertions(+), 25 deletions(-)

(limited to 'src/components/ui/HeroBannerSecondary.jsx')

diff --git a/src/components/ui/HeroBannerSecondary.jsx b/src/components/ui/HeroBannerSecondary.jsx
index a7b32a4a..6074c9a6 100644
--- a/src/components/ui/HeroBannerSecondary.jsx
+++ b/src/components/ui/HeroBannerSecondary.jsx
@@ -1,39 +1,58 @@
-import Link from '@/core/components/elements/Link/Link'
-import { getRandomInt } from '@/utils/getRandomInt'
-import Image from 'next/image'
-import { useMemo } from 'react'
-import { useQuery } from 'react-query'
-import { HeroBannerSkeleton } from '../skeleton/BannerSkeleton'
-import { bannerApi } from '@/api/bannerApi'
+import Link from '@/core/components/elements/Link/Link';
+import { getRandomInt } from '@/utils/getRandomInt';
+import Image from 'next/image';
+import { useMemo, useEffect, useState } from 'react';
+import { useQuery } from 'react-query';
+import { HeroBannerSkeleton } from '../skeleton/BannerSkeleton';
+import { bannerApi } from '@/api/bannerApi';
 
 const HeroBannerSecondary = () => {
-  const heroBannerSecondary = useQuery('heroBannerSecondary', bannerApi({ type: 'index-a-2' }))
+  const [heroBannerSecondary, setHeroBannerSecondary] = useState([]);
+  const [isLoading, setIsLoading] = useState(false);
+  // const heroBannerSecondary = useQuery(
+  //   'heroBannerSecondary',
+  //   bannerApi({ type: 'index-a-2' })
+  // );
 
-  const randomIndex = useMemo(() => {
-    if (!heroBannerSecondary.data) return null
-    const length = heroBannerSecondary.data?.length
-    return getRandomInt(length)
-  }, [heroBannerSecondary.data])
+  useEffect(() => {
+    const fetchData = async () => {
+      setIsLoading(true);
+      const res = await fetch(`/api/hero-banner?type=index-a-2`);
+      const { data } = await res.json();
+      if (data) {
+        setHeroBannerSecondary(data);
+      }
+      setIsLoading(false);
+    };
+
+    fetchData();
+  }, []);
 
-  if (heroBannerSecondary.isLoading) return <HeroBannerSkeleton />
+  const randomIndex = useMemo(() => {
+    if (!heroBannerSecondary) return null;
+    const length = heroBannerSecondary?.length;
+    return getRandomInt(length);
+  }, [heroBannerSecondary]);
+  if (isLoading) return <HeroBannerSkeleton />;
 
   return (
-    heroBannerSecondary.data && randomIndex !== null && (
-      <Link href={heroBannerSecondary.data[randomIndex].url} className="h-full">
+    heroBannerSecondary &&
+    randomIndex !== null && (
+      <Link href={heroBannerSecondary[randomIndex]?.url} className='h-full'>
         <Image
-          src={heroBannerSecondary.data[randomIndex].image}
+          src={heroBannerSecondary[randomIndex]?.image}
           width={512}
           height={1024}
-          alt={heroBannerSecondary.data[randomIndex].name}
-          className="object-cover object-center h-full"
-          loading="lazy" 
-          placeholder="blur" 
-          blurDataURL="/images/indoteknik-placeholder.png" 
-          sizes="(max-width: 768px) 100vw, 50vw" 
+          alt={heroBannerSecondary[randomIndex]?.name}
+          className='object-cover object-center h-full'
+          loading='lazy'
+          placeholder='blur'
+          blurDataURL='/images/indoteknik-placeholder.png'
+          sizes='(max-width: 768px) 100vw, 50vw'
         />
       </Link>
     )
   );
-}
+};
 
-export default HeroBannerSecondary
+export default HeroBannerSecondary;
-- 
cgit v1.2.3

