"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import { Calendar, Eye, MessageSquare } from "lucide-react"; import { getListArticle } from "@/service/article"; import Link from "next/link"; // Definisi type Article type Article = { id: number; title: string; description: string; categoryName: string; createdAt: string; slug: string; createdByName: string; thumbnailUrl: string; categories: { title: string }[]; files: { fileUrl: string; file_alt: string }[]; }; type PaginationMeta = { total: number; page: number; limit: number; last_page: number; }; export default function BreakingNews() { const [articles, setArticles] = useState([]); const [loading, setLoading] = useState(true); const [page, setPage] = useState(1); const [limit] = useState("8"); // jumlah per halaman const [meta, setMeta] = useState(null); useEffect(() => { fetchArticles(); }, [page]); async function fetchArticles() { try { setLoading(true); const req = { limit, page, search: "", categorySlug: "", sort: "desc", isPublish: true, sortBy: "created_at", }; const res = await getListArticle(req); setArticles(res?.data?.data || []); setMeta(res?.data?.meta || null); // pastikan API mengembalikan meta } catch (error) { console.error("Gagal memuat artikel:", error); } finally { setLoading(false); } } return (
{/* Judul */}

Berita Populer

{/* Loader */} {loading &&

Memuat data...

} {/* Grid berita */}
{articles.map((article) => (
{article.files?.[0]?.file_alt {article.categoryName || article.categories?.[0]?.title}

{article.title}

{article.description}

{" "} {new Date(article.createdAt).toLocaleDateString("id-ID", { day: "numeric", month: "long", year: "numeric", })} {0} {0}
))}
{meta && (
    {/* Tombol Prev */}
  • {/* Nomor Halaman 1 - last_page */} {Array.from({ length: meta.last_page }, (_, i) => i + 1).map( (p) => (
  • ) )} {/* Tombol Next */}
)}
); }