"use client"; import { Clock, MessageCircle } from "lucide-react"; import { Card } from "../ui/card"; import Image from "next/image"; import { useEffect, useRef, useState } from "react"; import { getListArticle } from "@/service/article"; 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; 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 Beranda() { const [page, setPage] = useState(1); const [totalPage, setTotalPage] = useState(1); const [articles, setArticles] = useState([]); 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); 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(() => { initState(); }, [page, showData, startDateValue, selectedCategories]); async function initState() { // loading(); 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); } finally { // close(); } } const scrollRef = useRef(null); const scrollLeft = () => { if (scrollRef.current) { scrollRef.current.scrollBy({ left: -240, behavior: "smooth" }); } }; const scrollRight = () => { if (scrollRef.current) { scrollRef.current.scrollBy({ left: 240, behavior: "smooth" }); } }; return (
{articles.length > 0 ? ( articles.map((article) => (
{article.title}

{article.title}

)) ) : (

Loading...

)}
{articles.length > 0 ? ( articles.map((article) => (
{article.title}

{article.categories?.map((cat) => cat.title).join(", ") || "Beranda"}

{article.title}

BY{" "} {article.createdByName} {" "} {" "} {new Date(article.createdAt).toLocaleDateString("id-ID")} 0

{article.description}

)) ) : (

Tidak ada artikel

)}
{bannerImageAd ? ( {bannerImageAd.title ) : ( Banner Default )}
{articles.map((news, i) => (
{news.title} {news?.categories?.[0]?.title || "TANPA KATEGORI"}

{news.title}

{" "}

{news.createdAt}

))}

Berita Utama

{articles.map((news, i) => (
{news.title}

{news.title}

))}
{bannerImageAd ? ( {bannerImageAd.title ) : ( Banner Default )}

Berita Pilihan

{articles.map((recentNews, i) => (
{recentNews.title}

{recentNews.title}

BY{" "} {recentNews.createdByName} {recentNews.createdAt} 0

{recentNews.description}

))}
{bannerImageAd ? ( {bannerImageAd.title ) : ( Banner Default )}
); }