"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import { getListArticle } from "@/service/article"; import { Calendar } from "lucide-react"; import Link from "next/link"; type Article = { id: number; title: string; description: string; categoryName: string; createdAt: string; createdByName: string; thumbnailUrl: string; categories: { title: string }[]; files: { fileUrl: string; file_alt: string }[]; }; export default function Header() { const [articles, setArticles] = useState([]); const [showData] = useState("5"); // default ambil 5 artikel useEffect(() => { fetchArticles(); }, []); async function fetchArticles() { try { const req = { limit: showData, 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); } } return (
{/* Bagian atas (2 artikel pertama) */}
{articles.slice(0, 2).map((item) => (
{item.title}
{item.categoryName}

{item.title}

{item.createdByName}{" "}
{new Date(item.createdAt).toLocaleDateString("en-GB", { day: "2-digit", month: "long", year: "numeric", })}
))}
{/* Bagian bawah (2 artikel selanjutnya) */}
{articles.slice(2, 4).map((item) => (
{item.title}
{item.categoryName}

{item.title}

{item.createdByName}{" "}
{new Date(item.createdAt).toLocaleDateString("en-GB", { day: "2-digit", month: "long", year: "numeric", })}
))}
); }