"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import { getListArticle } from "@/service/article"; import { ScrollArea } from "@/components/ui/scroll-area"; // pastikan ada komponen ini import { Calendar } from "lucide-react"; import Link from "next/link"; import { getAdvertise } from "@/service/advertisement"; type Article = { id: number; title: string; description: string; categoryName: string; createdAt: string; createdByName: string; thumbnailUrl: string; files: { fileUrl: string; file_alt: string }[]; }; type Advertise = { id: number; title: string; description: string; placement: string; contentFileUrl: string; redirectLink: string; }; export default function News() { const [articles, setArticles] = useState([]); const [recentPosts, setRecentPosts] = useState([]); const [bannerAd, setBannerAd] = useState(null); const [bannerImageAd, setBannerImageAd] = 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 || []; // ambil iklan placement jumbotron const jumbotron = data.find((ad) => ad.placement === "jumbotron"); if (jumbotron) setBannerAd(jumbotron); // ambil iklan placement banner const banner = data.find((ad) => ad.placement === "banner"); if (banner) setBannerImageAd(banner); } catch (err) { console.error("Error fetching advertisement:", err); } } useEffect(() => { fetchArticles(); fetchRecentPosts(); }, []); async function fetchArticles() { try { const req = { limit: "8", // ambil 8 artikel untuk konten utama 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 utama:", error); } } async function fetchRecentPosts() { try { const req = { limit: "5", // ambil 5 artikel untuk recent posts page: 1, search: "", categorySlug: "", sort: "desc", isPublish: true, sortBy: "created_at", }; const res = await getListArticle(req); setRecentPosts(res?.data?.data || []); } catch (error) { console.error("Gagal memuat recent posts:", error); } } return (
{/* Scrollable Left Section */}
{articles.slice(0, 4).map((article) => (
{article.title}

{article.categoryName}

{article.title}

{article.description}

{article.createdByName}{" "}
{new Date(article.createdAt).toLocaleDateString("en-GB", { day: "2-digit", month: "long", year: "numeric", })}
))}
{/* Banner */}
{bannerImageAd ? ( {bannerImageAd.title ) : ( Banner Default )}
{/* Artikel kedua */}
{articles.slice(4, 8).map((article) => (
{article.title}

{article.categoryName}

{article.title}

{article.description}

{article.createdByName}{" "}
{new Date(article.createdAt).toLocaleDateString("en-GB", { day: "2-digit", month: "long", year: "numeric", })}
))}
{/* Banner */}
{bannerImageAd ? ( {bannerImageAd.title ) : ( Banner Default )}
{/* Pagination (sementara statis, bisa pakai API meta untuk dinamis) */}
...
{/* Fixed Right Section */}

Recent Posts

{recentPosts.map((item) => (
{item.title}

{item.categoryName}

{item.title}

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

))} {/* Ads / Subscribe */}
{bannerAd ? (
{bannerAd.title
) : ( Berita Utama )}

Subscribe us to get the latest news!

{bannerAd ? (
{bannerAd.title
) : ( Berita Utama )}
); }