summaryrefslogtreecommitdiff
path: root/src/core/components/elements/Navbar/TopBannerMobile.jsx
blob: c3f42f85181dfa2be89f3367282e5f6da6522c97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Image from 'next/image';
import { useQuery } from 'react-query';
import useDevice from '@/core/hooks/useDevice';
import odooApi from '@/core/api/odooApi';
import SmoothRender from '~/components/ui/smooth-render';
import Link from '../Link/Link';
import { background } from '@chakra-ui/react';
import { useEffect, useState } from 'react';

const TopBannerMobile = ({ onLoad = () => {} }) => {
  const [topBannerMobile, setTopBannerMobile] = useState([]);
  const { isDesktop, isMobile } = useDevice();

  useEffect(() => {
    const fetchData = async () => {
      const res = await fetch(`/api/hero-banner?type=top-banner-mobile`);
      const { data } = await res.json();
      if (data) {
        setTopBannerMobile(data);
      }
    };

    fetchData();
  }, []);

  // const topBannerMobile = useQuery({
  //   queryKey: 'topBannerMobile',
  //   queryFn: async () => await odooApi('GET', '/api/v1/banner?type=top-banner'),
  //   refetchOnWindowFocus: false,
  // });

  // const backgroundColor = topBannerMobile.data?.[0]?.backgroundColor || 'transparent';
  const hasData = topBannerMobile?.length > 0;
  const data = topBannerMobile?.[0] || null;

  useEffect(() => {
    if (hasData) {
      onLoad();
    }
  }, [hasData, onLoad]);

  
  if (!hasData || !data?.image) {
    return null;
  }
  return (
    <SmoothRender
      isLoaded={hasData}
      duration='700ms'
      delay='300ms'
      className='h-auto'
    >
      <Link
        href={data?.url}
        aria-label='panduan pick up barang'
        className='block w-full'
      >
        <Image
          src={data?.image}
          alt='Panduan Pick Up Barang'
          width={0}
          height={0}
          sizes='100vw'
          className='w-full h-auto'
          priority
        />
      </Link>
    </SmoothRender>
  );
};

export default TopBannerMobile;