web-asuransi-aman/components/landing-page/headers.tsx

243 lines
7.9 KiB
TypeScript
Raw Normal View History

2025-09-23 01:13:46 +00:00
"use client";
import { useEffect, useState } from "react";
import Image from "next/image";
import { getListArticle } from "@/service/article";
2025-09-26 10:26:42 +00:00
import Link from "next/link";
2025-10-12 07:49:35 +00:00
import { getAdvertise } from "@/service/advertisement";
2025-09-23 01:13:46 +00:00
type Article = {
id: number;
title: string;
description: string;
createdAt: string;
2025-11-01 07:30:56 +00:00
slug: string;
2025-09-23 01:13:46 +00:00
createdByName: string;
2025-10-09 04:26:43 +00:00
customCreatorName: string;
2025-09-23 01:13:46 +00:00
thumbnailUrl: string;
categories: { title: string }[];
};
2025-10-12 07:49:35 +00:00
type Advertise = {
id: number;
title: string;
description: string;
placement: string;
contentFileUrl: string;
redirectLink: string;
};
2025-09-23 01:13:46 +00:00
export default function HeaderNews() {
const [articles, setArticles] = useState<Article[]>([]);
2025-10-12 07:49:35 +00:00
const [bannerAd, setBannerAd] = useState<Advertise | null>(null);
useEffect(() => {
initStateAdver();
}, []);
async function initStateAdver() {
const req = {
limit: 100,
page: 1,
sort: "desc",
sortBy: "created_at",
isPublish: true,
};
try {
const res = await getAdvertise(req);
const data: Advertise[] = res?.data?.data || [];
// filter iklan dengan placement = "banner"
const banner = data.find((ad) => ad.placement === "banner");
if (banner) {
setBannerAd(banner);
}
} catch (err) {
console.error("Error fetching advertisement:", err);
}
}
2025-09-23 01:13:46 +00:00
useEffect(() => {
fetchArticles();
}, []);
async function fetchArticles() {
try {
const req = {
limit: "4",
page: 1,
search: "",
categorySlug: "",
sort: "desc",
isPublish: true,
sortBy: "created_at",
};
const res = await getListArticle(req);
setArticles(res?.data?.data || []);
} catch (error) {
console.error("Gagal memuat artikel:", error);
}
}
2025-09-23 07:53:32 +00:00
if (articles.length === 0) {
return <p className="text-center">Belum ada artikel.</p>;
}
2025-09-23 01:13:46 +00:00
return (
<div className="max-w-7xl mx-auto">
2025-09-23 07:53:32 +00:00
<div className="py-6 grid grid-cols-1 md:grid-cols-2 gap-4 h-[600px]">
2025-09-23 01:13:46 +00:00
{/* Kiri - berita utama */}
2025-09-23 07:53:32 +00:00
<div className="relative overflow-hidden h-full">
2025-11-01 07:30:56 +00:00
<Link className="flex" href={`/details/${articles[0]?.slug}`}>
2025-09-26 10:26:42 +00:00
<Image
src={articles[0]?.thumbnailUrl || "/dummy.jpg"}
alt={articles[0]?.title || "No Title"}
fill
className="object-cover"
/>
<div className="absolute inset-0 bg-gradient-to-t from-black/70 to-transparent"></div>
<div className="absolute bottom-4 left-4 right-4 text-white">
<span className="bg-black/70 text-xs px-2 py-1 rounded">
{articles[0]?.categories?.[0]?.title || "Uncategorized"}
</span>
<h2 className="text-2xl font-bold mt-2">{articles[0]?.title}</h2>
<p className="text-sm mt-1">
<span className="font-semibold">
2025-10-09 04:26:43 +00:00
{articles[0]?.customCreatorName || articles[0]?.createdByName}
2025-09-26 10:26:42 +00:00
</span>{" "}
{" "}
{articles[0]?.createdAt &&
new Date(articles[0].createdAt).toLocaleDateString("id-ID", {
day: "numeric",
month: "long",
year: "numeric",
})}
</p>
</div>
</Link>
2025-09-23 01:13:46 +00:00
</div>
{/* Kanan - 3 berita */}
<div className="grid grid-rows-2 gap-2 h-full">
{/* Atas: 2 berita kecil */}
<div className="grid grid-cols-2 gap-4">
2025-09-23 07:53:32 +00:00
{[articles[1], articles[2]].map(
(item, idx) =>
item && (
<div
key={item.id || `dummy-${idx}`}
className="relative overflow-hidden"
>
2025-11-01 07:30:56 +00:00
<Link className="flex" href={`/details/${item?.slug}`}>
2025-09-26 10:26:42 +00:00
<Image
src={item.thumbnailUrl || "/dummy.jpg"}
alt={item.title || "No Title"}
fill
className="object-cover"
/>
<div className="absolute inset-0 bg-gradient-to-t from-black/70 to-transparent"></div>
<div className="absolute bottom-2 left-2 right-2 text-white">
<span className="bg-black/70 text-[10px] px-2 py-1 rounded">
{item.categories?.[0]?.title || "Uncategorized"}
</span>
<h3 className="text-sm font-semibold mt-1 line-clamp-2">
{item.title}
</h3>
<p className="text-[10px]">
<span className="font-semibold">
2025-10-09 04:26:43 +00:00
{item?.customCreatorName || item.createdByName}
2025-09-26 10:26:42 +00:00
</span>{" "}
{" "}
{item.createdAt &&
new Date(item.createdAt).toLocaleDateString(
"id-ID",
{
day: "numeric",
month: "short",
year: "numeric",
}
)}
</p>
</div>
</Link>
2025-09-23 07:53:32 +00:00
</div>
)
)}
2025-09-23 01:13:46 +00:00
</div>
{/* Bawah: 1 berita besar */}
2025-09-23 07:53:32 +00:00
{articles[3] && (
<div className="relative overflow-hidden">
2025-11-01 07:30:56 +00:00
<Link className="flex" href={`/details/${articles[3]?.slug}`}>
2025-09-26 10:26:42 +00:00
<Image
src={articles[3]?.thumbnailUrl || "/dummy.jpg"}
alt={articles[3]?.title || "No Title"}
fill
className="object-cover"
/>
<div className="absolute inset-0 bg-gradient-to-t from-black/70 to-transparent"></div>
<div className="absolute bottom-2 left-2 right-2 text-white">
<span className="bg-black/70 text-xs px-2 py-1 rounded">
{articles[3]?.categories?.[0]?.title || "Uncategorized"}
</span>
<h3 className="text-base font-semibold mt-1">
{articles[3]?.title}
</h3>
<p className="text-xs">
<span className="font-semibold">
{articles[3]?.createdByName}
</span>{" "}
{" "}
{articles[3]?.createdAt &&
new Date(articles[3].createdAt).toLocaleDateString(
"id-ID",
{
day: "numeric",
month: "long",
year: "numeric",
}
)}
</p>
</div>
</Link>
2025-09-23 01:13:46 +00:00
</div>
2025-09-23 07:53:32 +00:00
)}
2025-09-23 01:13:46 +00:00
</div>
</div>
2025-09-23 07:53:32 +00:00
{/* Banner bawah */}
2025-10-12 07:49:35 +00:00
<div className="relative mt-10 mb-2 flex justify-center mx-8 border my-8 h-[350px] overflow-hidden bg-white">
{bannerAd ? (
<a
href={bannerAd.redirectLink}
target="_blank"
rel="noopener noreferrer"
className="block w-full"
>
<div className="relative w-full h-[350px] flex justify-center">
<Image
src={bannerAd.contentFileUrl}
alt={bannerAd.title || "Iklan Banner"}
width={1200}
height={350}
className="object-contain w-full h-full"
/>
</div>
</a>
) : (
<Image
src="/image-kolom.png"
alt="Berita Utama"
width={1200}
height={188}
className="object-contain w-full h-[188px]"
/>
)}
2025-09-23 01:13:46 +00:00
</div>
</div>
);
}