diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-05-22 11:58:05 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-05-22 11:58:05 +0700 |
| commit | 33da0fcb718335eb1d077af4321ac65e0146a2d6 (patch) | |
| tree | d87dab8d58c3e148e3e541a88b1ef7d2fd6d0ba7 /src/components | |
| parent | c65d7f6b82a7f2f80b1fe43a0bd06144d2ca64ff (diff) | |
Refactoring hero banner feature
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/skeleton/BannerSkeleton.jsx | 20 | ||||
| -rw-r--r-- | src/components/ui/HeroBanner.jsx | 64 | ||||
| -rw-r--r-- | src/components/ui/HeroBannerSecondary.jsx | 35 |
3 files changed, 119 insertions, 0 deletions
diff --git a/src/components/skeleton/BannerSkeleton.jsx b/src/components/skeleton/BannerSkeleton.jsx new file mode 100644 index 00000000..3de9c027 --- /dev/null +++ b/src/components/skeleton/BannerSkeleton.jsx @@ -0,0 +1,20 @@ +const HeroBannerSkeleton = () => { + return ( + <div role='status' className='animate-pulse h-full min-h-[460px]'> + <div className='flex justify-center items-center h-full w-full mb-4 bg-gray-300 rounded' aria-busy> + <svg + className='w-20 h-20 text-gray-100' + xmlns='http://www.w3.org/2000/svg' + aria-hidden='true' + fill='currentColor' + viewBox='0 0 640 512' + > + <path d='M480 80C480 35.82 515.8 0 560 0C604.2 0 640 35.82 640 80C640 124.2 604.2 160 560 160C515.8 160 480 124.2 480 80zM0 456.1C0 445.6 2.964 435.3 8.551 426.4L225.3 81.01C231.9 70.42 243.5 64 256 64C268.5 64 280.1 70.42 286.8 81.01L412.7 281.7L460.9 202.7C464.1 196.1 472.2 192 480 192C487.8 192 495 196.1 499.1 202.7L631.1 419.1C636.9 428.6 640 439.7 640 450.9C640 484.6 612.6 512 578.9 512H55.91C25.03 512 .0006 486.1 .0006 456.1L0 456.1z' /> + </svg> + </div> + <span className='sr-only'>Loading...</span> + </div> + ) +} + +export { HeroBannerSkeleton } diff --git a/src/components/ui/HeroBanner.jsx b/src/components/ui/HeroBanner.jsx new file mode 100644 index 00000000..1b5bf165 --- /dev/null +++ b/src/components/ui/HeroBanner.jsx @@ -0,0 +1,64 @@ +import DesktopView from '@/core/components/views/DesktopView' +import MobileView from '@/core/components/views/MobileView' +import { Swiper, SwiperSlide } from 'swiper/react' +import { Pagination, Autoplay } from 'swiper' +import 'swiper/css' +import 'swiper/css/pagination' +import 'swiper/css/autoplay' +import { useMemo } from 'react' +import Link from '@/core/components/elements/Link/Link' +import Image from '@/core/components/elements/Image/Image' +import { useQuery } from 'react-query' +import { BannerApi } from '@/api/BannerApi' +import { HeroBannerSkeleton } from '../skeleton/BannerSkeleton' + +const swiperBanner = { + autoplay: { + delay: 6000, + disableOnInteraction: false + }, + modules: [Pagination, Autoplay], + loop: true, + className: 'border border-gray_r-6', + slidesPerView: 1 +} + +const HeroBanner = () => { + const heroBanner = useQuery('heroBanner', BannerApi({ type: 'index-a-1' })) + + const swiperBannerMobile = { + ...swiperBanner, + pagination: { dynamicBullets: true, clickable: true } + } + + const swiperBannerDesktop = { + ...swiperBanner, + pagination: { dynamicBullets: false, clickable: true } + } + + const BannerComponent = useMemo(() => { + return heroBanner.data?.map((banner, index) => ( + <SwiperSlide key={index}> + <Link href={banner.url} className='w-full h-auto'> + <Image src={banner.image} alt={banner.name} className='w-full h-auto' /> + </Link> + </SwiperSlide> + )) + }, [heroBanner.data]) + + if (heroBanner.isLoading) return <HeroBannerSkeleton /> + + return ( + <> + <MobileView> + <Swiper {...swiperBannerMobile}>{BannerComponent}</Swiper> + </MobileView> + + <DesktopView> + <Swiper {...swiperBannerDesktop}>{BannerComponent}</Swiper> + </DesktopView> + </> + ) +} + +export default HeroBanner diff --git a/src/components/ui/HeroBannerSecondary.jsx b/src/components/ui/HeroBannerSecondary.jsx new file mode 100644 index 00000000..a3227002 --- /dev/null +++ b/src/components/ui/HeroBannerSecondary.jsx @@ -0,0 +1,35 @@ +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' + +const HeroBannerSecondary = () => { + 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]) + + if (heroBannerSecondary.isLoading) return <HeroBannerSkeleton /> + + return ( + heroBannerSecondary.data && ( + <Link href={heroBannerSecondary.data[randomIndex].url} className='h-full'> + <Image + src={heroBannerSecondary.data[randomIndex].image} + width={512} + height={1024} + alt={heroBannerSecondary.data[randomIndex].name} + className='object-cover object-center h-full' + /> + </Link> + ) + ) +} + +export default HeroBannerSecondary |
