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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// Swiper
import { Autoplay, Pagination } from 'swiper';
import 'swiper/css';
import 'swiper/css/autoplay';
import 'swiper/css/pagination';
import { Swiper, SwiperSlide } from 'swiper/react';
import Image from 'next/image';
import { useEffect, useMemo, useState } from 'react';
import Link from '@/core/components/elements/Link/Link';
import DesktopView from '@/core/components/views/DesktopView';
import MobileView from '@/core/components/views/MobileView';
import SmoothRender from '~/components/ui/smooth-render';
const swiperBanner = {
autoplay: {
delay: 6000,
disableOnInteraction: false,
},
modules: [Pagination, Autoplay],
loop: true,
className: 'border border-gray_r-6 min-h-full',
slidesPerView: 1,
};
const HeroBanner = () => {
// const heroBanner = useQuery('heroBanner', bannerApi({ type: 'index-a-1' }));
const [data, setData] = useState(null);
useEffect(() => {
const fetchData = async () => {
const res = await fetch(`/api/hero-banner?type=index-a-1`);
const { data } = await res.json();
if (data) {
setData(data);
}
};
fetchData();
}, []);
const heroBanner = data;
const swiperBannerMobile = {
...swiperBanner,
pagination: { dynamicBullets: true, clickable: true },
};
const swiperBannerDesktop = {
...swiperBanner,
pagination: { dynamicBullets: false, clickable: true },
};
const BannerComponent = useMemo(() => {
if (!heroBanner) return null;
return heroBanner.map((banner, index) => (
<SwiperSlide key={index}>
<Link href={banner?.url} aria-label={banner.name}>
<Image
src={banner.image}
alt={banner.name}
width={1152}
height={768}
placeholder='blur'
blurDataURL='/images/indoteknik-placeholder.png'
sizes='(max-width: 768px) 100vw, 50vw'
priority={true}
/>
</Link>
</SwiperSlide>
));
}, [heroBanner]);
return (
<>
<MobileView>
<SmoothRender
isLoaded={heroBanner?.length > 0}
height='68vw'
duration='750ms'
delay='100ms'
>
<Swiper {...swiperBannerMobile}>{BannerComponent}</Swiper>
</SmoothRender>
</MobileView>
<DesktopView>
{heroBanner?.length > 0 && (
<Swiper {...swiperBannerDesktop}>{BannerComponent}</Swiper>
)}
</DesktopView>
</>
);
};
export default HeroBanner;
|