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

149 lines
5.0 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import Image from "next/image";
import { getListArticle } from "@/service/article";
type Article = {
id: number;
title: string;
description: string;
createdAt: string;
createdByName: string;
thumbnailUrl: string;
categories: { title: string }[];
};
export default function HeaderNews() {
const [articles, setArticles] = useState<Article[]>([]);
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);
}
}
if (articles.length < 4) return null;
return (
<div className="max-w-7xl mx-auto">
<div className=" py-6 grid grid-cols-1 md:grid-cols-2 gap-4 h-[600px]">
{/* Kiri - berita utama */}
<div className="relative overflow-hidden h-full">
<Image
src={articles[0].thumbnailUrl || "/dummy.jpg"}
alt={articles[0].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">{articles[0].createdByName}</span>{" "}
{" "}
{new Date(articles[0].createdAt).toLocaleDateString("id-ID", {
day: "numeric",
month: "long",
year: "numeric",
})}
</p>
</div>
</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">
{articles.slice(1, 3).map((item) => (
<div key={item.id} className="relative overflow-hidden">
<Image
src={item.thumbnailUrl || "/dummy.jpg"}
alt={item.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">{item.createdByName}</span>{" "}
{" "}
{new Date(item.createdAt).toLocaleDateString("id-ID", {
day: "numeric",
month: "short",
year: "numeric",
})}
</p>
</div>
</div>
))}
</div>
{/* Bawah: 1 berita besar */}
<div className="relative overflow-hidden">
<Image
src={articles[3].thumbnailUrl || "/dummy.jpg"}
alt={articles[3].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>{" "}
{" "}
{new Date(articles[3].createdAt).toLocaleDateString("id-ID", {
day: "numeric",
month: "long",
year: "numeric",
})}
</p>
</div>
</div>
</div>
</div>
<div className="w-full relative mb-2 h-[120px] overflow-hidden flex items-center border my-8">
<Image
src={"/image-kolom.png"}
alt="Kolom PPS"
fill
className="max-w-7xl mx-auto object-cover"
/>
</div>
</div>
);
}