"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import Link from "next/link"; import { getListArticle } from "@/service/article"; import { getAdvertise } from "@/service/advertisement"; 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 }[]; }; type Advertise = { id: number; title: string; description: string; placement: string; contentFileUrl: string; redirectLink: string; }; export default function HeaderPopular() { const [articles, setArticles] = useState([]); const [page, setPage] = useState(1); const [totalPage, setTotalPage] = useState(1); const [showData, setShowData] = useState("5"); const [search, setSearch] = useState(""); const [selectedCategories, setSelectedCategories] = useState([]); const [startDateValue, setStartDateValue] = useState({ startDate: null, endDate: null, }); const [bannerAd, setBannerAd] = useState(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 || [1]; // filter iklan dengan placement = "banner" const banner = data.find((ad) => ad.placement === "jumbotron"); if (banner) { setBannerAd(banner); } } catch (err) { console.error("Error fetching advertisement:", err); } } 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 (error) { console.error("Gagal memuat artikel:", error); } } if (articles.length === 0) return null; const featuredArticle = articles[0]; const recentPosts = articles.slice(1, 5); // Ambil 4 artikel berikutnya return (

Berita Terpopuler

Berita Populer : Membahas yang Tren, Membuka Wawasan Anda!

{articles.map((article) => (
{article.title}

{article.categories?.[0]?.title || article.categoryName || "BERITA"}

{article.title}

{article.description}

By {article.createdByName} —{" "} {new Date(article.createdAt).toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric", })}

READ MORE >
))}
{Array.from({ length: totalPage }).map((_, i) => ( ))}

Recent Posts

{recentPosts.map((post, index) => (
{post.title}

{post.categories?.[0]?.title || post.categoryName || "BERITA"}

{post.title}

{new Date(post.createdAt).toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric", })}

{String(index + 1).padStart(2, "0")}
))}
{bannerAd ? (
{bannerAd.title
) : ( Berita Utama )}

Subscribe us to get the latest news!

); }