mediahub-fe/components/landing-page/hero.tsx

172 lines
7.9 KiB
TypeScript
Raw Normal View History

2024-12-09 16:09:42 +00:00
import { formatDateToIndonesian, textEllipsis } from "@/utils/globals";
2024-11-26 17:00:20 +00:00
import React, { useEffect, useState } from "react";
import "swiper/css/bundle";
2024-12-11 15:08:03 +00:00
import "swiper/css/navigation";
2024-12-09 16:09:42 +00:00
import { getHeroData } from "@/service/landing/landing";
import Link from "next/link";
import { useParams, usePathname, useRouter } from "next/navigation";
import { Carousel, CarouselContent, CarouselItem, CarouselNext, CarouselPrevious } from "@/components/ui/carousel";
2025-01-15 15:59:19 +00:00
import { Skeleton } from "../ui/skeleton";
2025-02-06 10:34:22 +00:00
import Image from "next/image";
2024-11-26 17:00:20 +00:00
const Hero: React.FC = () => {
2024-12-09 16:09:42 +00:00
const router = useRouter();
const pathname = usePathname();
const params = useParams();
const locale = params?.locale;
2025-01-15 15:59:19 +00:00
const [isLoading, setIsLoading] = useState<any>(true);
2024-12-09 16:09:42 +00:00
const [heroData, setHeroData] = useState<any>();
2025-01-15 15:59:19 +00:00
useEffect(() => {
const timer = setTimeout(() => {
setIsLoading(false);
}, 3000);
return () => clearTimeout(timer);
}, []);
2024-12-09 16:09:42 +00:00
useEffect(() => {
async function fetchCategories() {
2025-01-15 15:59:19 +00:00
const url = "https://netidhub.com/api/csrf";
try {
const response = await fetch(url);
2025-01-15 15:59:19 +00:00
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
2025-01-15 15:59:19 +00:00
const data = await response.json();
2025-01-15 15:59:19 +00:00
return data; // Menampilkan data yang diterima dari API
} catch (error) {
2025-01-15 15:59:19 +00:00
console.error("Fetch error: ", error);
}
}
fetchCategories();
2024-12-09 16:09:42 +00:00
initFetch();
}, []);
2025-02-24 16:36:33 +00:00
2024-12-09 16:09:42 +00:00
const initFetch = async () => {
const response = await getHeroData();
console.log(response);
setHeroData(response?.data?.data?.content);
};
2025-03-05 10:05:44 +00:00
2024-11-26 17:00:20 +00:00
return (
<div className="flex flex-col lg:flex-row items-start justify-center gap-8 px-4 lg:px-20 py-4 mx-auto w-auto mt-6">
2024-11-26 17:00:20 +00:00
{/* Section Gambar Utama */}
2025-01-15 15:59:19 +00:00
{isLoading ? (
<div className="flex flex-col space-y-3 mx-auto w-full lg:w-2/3">
<Skeleton className="h-[310px] lg:h-[420px] rounded-xl" />
<div className="space-y-2">
<Skeleton className="h-4 w-[250px]" />
<Skeleton className="h-4 w-[200px]" />
</div>
</div>
) : (
<Carousel className="lg:w-2/3 lg:h-full ">
<CarouselContent>
{heroData?.map((list: any) => (
<CarouselItem key={list?.id}>
<div className="relative h-[310px] lg:h-[420px]">
2025-02-20 09:31:43 +00:00
<Image src={list?.thumbnailLink} alt="gambar-utama" width={1920} height={1080} className="w-full lg:w-[850px] h-[310px] lg:h-[420px] rounded-lg object-cover" />
2025-01-15 15:59:19 +00:00
<div className="absolute bottom-0 left-0 right-0 bg-transparent backdrop-blur-sm text-black dark:text-white p-4 rounded-b-lg">
2025-01-24 15:56:24 +00:00
<span className="text-white bg-[#bb3523] rounded-md w-full h-full font-semibold uppercase text-xs px-2 py-1">{list?.categoryName}</span>
2025-01-15 15:59:19 +00:00
<Link href={`${locale}/image/detail/${list?.slug}`}>
2025-03-05 10:05:44 +00:00
<h2 className="text-lg text-slate-500 dark:text-white font-bold mt-2">{list?.title}</h2>
2025-01-15 15:59:19 +00:00
</Link>
2025-03-05 10:05:44 +00:00
<p className="text-xs flex flex-row items-center text-slate-500 dark:text-white gap-1 mt-1">
2025-01-15 15:59:19 +00:00
{formatDateToIndonesian(new Date(list?.createdAt))} {list?.timezone ? list?.timezone : "WIB"}|{" "}
<svg xmlns="http://www.w3.org/2000/svg" width="1.2em" height="1.2em" viewBox="0 0 24 24">
<path
fill="currentColor"
d="M11.5 18c4 0 7.46-2.22 9.24-5.5C18.96 9.22 15.5 7 11.5 7s-7.46 2.22-9.24 5.5C4.04 15.78 7.5 18 11.5 18m0-12c4.56 0 8.5 2.65 10.36 6.5C20 16.35 16.06 19 11.5 19S3 16.35 1.14 12.5C3 8.65 6.94 6 11.5 6m0 2C14 8 16 10 16 12.5S14 17 11.5 17S7 15 7 12.5S9 8 11.5 8m0 1A3.5 3.5 0 0 0 8 12.5a3.5 3.5 0 0 0 3.5 3.5a3.5 3.5 0 0 0 3.5-3.5A3.5 3.5 0 0 0 11.5 9"
/>
</svg>{" "}
{list?.clickCount}{" "}
</p>
</div>
</div>
</CarouselItem>
))}
</CarouselContent>
2025-02-20 09:31:43 +00:00
<CarouselPrevious className="hover:bg-black ml-1 lg:ml-0" />
<CarouselNext className="hover:bg-black mr-1 lg:mr-0" />
2025-01-15 15:59:19 +00:00
</Carousel>
)}
{/* Section Kanan */}
<div>
{isLoading ? (
<>
<div className="flex items-center gap-4 max-w-sm mx-auto mb-3">
<Skeleton className="h-[73px] w-16 rounded-md" />
<div className="space-y-2">
<Skeleton className="h-4 w-[250px]" />
<Skeleton className="h-4 w-[200px]" />
</div>
</div>
2025-01-31 12:51:04 +00:00
<div className="items-center hidden md:block gap-4 max-w-sm mx-auto mb-3 lg:flex">
2025-01-15 15:59:19 +00:00
<Skeleton className="h-[73px] w-16 rounded-md" />
<div className="space-y-2">
<Skeleton className="h-4 w-[250px]" />
<Skeleton className="h-4 w-[200px]" />
</div>
</div>
2025-01-31 12:51:04 +00:00
<div className="hidden md:block items-center gap-4 max-w-sm mx-auto mb-3 lg:flex">
2025-01-15 15:59:19 +00:00
<Skeleton className="h-[73px] w-16 rounded-md" />
<div className="space-y-2">
<Skeleton className="h-4 w-[250px]" />
<Skeleton className="h-4 w-[200px]" />
</div>
</div>
2025-01-31 12:51:04 +00:00
<div className="hidden md:block items-center gap-4 max-w-sm mx-auto mb-3 lg:flex">
2025-01-15 15:59:19 +00:00
<Skeleton className="h-[73px] w-16 rounded-md" />
<div className="space-y-2">
<Skeleton className="h-4 w-[250px]" />
<Skeleton className="h-4 w-[200px]" />
</div>
</div>
2025-01-31 12:51:04 +00:00
<div className="hidden md:block items-center gap-4 max-w-sm mx-auto lg:flex">
2025-01-15 15:59:19 +00:00
<Skeleton className="h-[73px] w-16 rounded-md" />
<div className="space-y-2">
<Skeleton className="h-4 w-[250px]" />
<Skeleton className="h-4 w-[200px]" />
</div>
</div>
</>
) : (
2025-01-16 16:55:04 +00:00
<ul className="py-4 lg:py-0 flex flex-row lg:flex-col gap-4 flex-nowrap w-[95vw] lg:w-auto overflow-x-auto">
2025-01-15 15:59:19 +00:00
{heroData?.map((item: any) => (
<li key={item?.id} className="flex gap-4 flex-row lg:w-full ">
<div className="flex-shrink-0 w-24 rounded-lg">
2025-02-06 10:34:22 +00:00
<Image width={720} height={480} src={item?.thumbnailLink} alt={item?.title} className="w-full h-[73px] object-cover rounded-lg" />
2025-01-15 15:59:19 +00:00
</div>
<div className="w-[280px] lg:w-auto">
<span className="text-white bg-[#bb3523] px-4 py-1 rounded-lg flex text-[8px] font-bold uppercase w-fit">{item?.categoryName}</span>
<Link href={`${locale}/image/detail/${item?.slug}`}>
2025-02-18 02:10:23 +00:00
<h3 className="text-base font-bold mt-2 h-6 hover:h-auto truncate hover:whitespace-normal hover:overflow-visible">{item?.title}</h3>
</Link>
2025-01-15 15:59:19 +00:00
<p className="text-[10px] flex flex-row items-center gap-1 text-gray-500 mt-1">
{formatDateToIndonesian(new Date(item?.createdAt))} {item?.timezone ? item?.timezone : "WIB"} |{" "}
<svg xmlns="http://www.w3.org/2000/svg" width="1.2em" height="1.2em" viewBox="0 0 24 24">
<path
fill="currentColor"
d="M11.5 18c4 0 7.46-2.22 9.24-5.5C18.96 9.22 15.5 7 11.5 7s-7.46 2.22-9.24 5.5C4.04 15.78 7.5 18 11.5 18m0-12c4.56 0 8.5 2.65 10.36 6.5C20 16.35 16.06 19 11.5 19S3 16.35 1.14 12.5C3 8.65 6.94 6 11.5 6m0 2C14 8 16 10 16 12.5S14 17 11.5 17S7 15 7 12.5S9 8 11.5 8m0 1A3.5 3.5 0 0 0 8 12.5a3.5 3.5 0 0 0 3.5 3.5a3.5 3.5 0 0 0 3.5-3.5A3.5 3.5 0 0 0 11.5 9"
/>
</svg>{" "}
2025-01-15 15:59:19 +00:00
{item?.clickCount}
</p>
2024-12-09 16:09:42 +00:00
</div>
2025-01-15 15:59:19 +00:00
</li>
))}
</ul>
)}
2024-11-26 17:00:20 +00:00
</div>
</div>
);
};
export default Hero;