"use client"; import { getListArticle } from "@/service/article"; import Image from "next/image"; import Link from "next/link"; import { useEffect, useState } from "react"; type Article = { id: number; title: string; description: string; categoryName: string; createdAt: string; createdByName: string; customCreatorName: string; thumbnailUrl: string; categories: { title: string }[]; files: { fileUrl: string; file_alt: string }[]; }; export default function News() { const [page, setPage] = useState(1); const [totalPage, setTotalPage] = useState(1); const [articles, setArticles] = useState([]); const [showData, setShowData] = useState("6"); const [search] = useState(""); const [selectedCategories] = useState(""); const [startDateValue] = useState({ startDate: null, endDate: null, }); // Fetch data setiap kali page berubah useEffect(() => { initState(); }, [page, showData, startDateValue, selectedCategories]); async function initState() { const req = { limit: showData, page, search, categorySlug: Array.from(selectedCategories).join(","), sort: "desc", isPublish: true, sortBy: "created_at", }; try { const res = await getListArticle(req); setArticles(res?.data?.data || []); setTotalPage(res?.data?.meta?.totalPage || 1); } catch (err) { console.error("Error fetching articles:", err); } } const handlePrev = () => { if (page > 1) setPage((prev) => prev - 1); }; const handleNext = () => { if (page < totalPage) setPage((prev) => prev + 1); }; return (
{/* Berita Terbaru */}

Berita Terbaru

{articles.length > 0 ? ( articles.map((item) => (
{item.files?.[0]?.file_alt {item.categoryName || item.categories?.[0]?.title || "Umum"}

{item.title}

{new Date(item.createdAt).toLocaleDateString("id-ID", { day: "2-digit", month: "long", year: "numeric", })}

)) ) : (

Tidak ada artikel tersedia.

)}
{/* Pagination */}
{/* Twitter Section */}

Twitter @ArahNegeri

{/* Embed atau konten lain */}
{/* Banner bawah */}
Berita Utama
); }