"use client"; import { useState, useEffect } from "react"; import Image from "next/image"; import { Button } from "@/components/ui/button"; import { ThumbsUp, ThumbsDown } from "lucide-react"; import { Card } from "../ui/card"; import Link from "next/link"; import { listData, listArticles } from "@/service/landing/landing"; import { Swiper, SwiperSlide } from "swiper/react"; import "swiper/css"; import "swiper/css/navigation"; import { Navigation } from "swiper/modules"; // Format tanggal function formatTanggal(dateString: string) { if (!dateString) return ""; return ( new Date(dateString) .toLocaleString("id-ID", { day: "2-digit", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit", hour12: false, timeZone: "Asia/Jakarta", }) .replace(/\./g, ":") + " WIB" ); } export default function MediaUpdate() { const [tab, setTab] = useState<"latest" | "popular">("latest"); const [dataToRender, setDataToRender] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { fetchData(tab); }, [tab]); async function fetchData(section: "latest" | "popular") { try { setLoading(true); // Use new Articles API const response = await listArticles( 1, 20, 1, // typeId for images undefined, undefined, section === "latest" ? "createdAt" : "viewCount" ); console.log("Media Update Articles API response:", response); if (response?.error) { console.error("Articles API failed, falling back to old API"); // Fallback to old API const fallbackRes = await listData( "1", "", "", 20, 0, section === "latest" ? "createdAt" : "clickCount", "", "", "" ); setDataToRender(fallbackRes?.data?.data?.content || []); return; } // Handle new API response structure const articlesData = response?.data?.data || []; // Transform articles data to match old structure for backward compatibility const transformedData = articlesData.map((article: any) => ({ id: article.id, title: article.title, category: article.categoryName || (article.categories && article.categories[0]?.title) || "Tanpa Kategori", createdAt: article.createdAt, smallThumbnailLink: article.thumbnailUrl, label: article.typeId === 1 ? "Image" : article.typeId === 2 ? "Video" : article.typeId === 3 ? "Text" : article.typeId === 4 ? "Audio" : "", ...article })); setDataToRender(transformedData); } catch (err) { console.error("Gagal memuat data:", err); // Try fallback to old API if new API fails try { const fallbackRes = await listData( "1", "", "", 20, 0, section === "latest" ? "createdAt" : "clickCount", "", "", "" ); setDataToRender(fallbackRes?.data?.data?.content || []); } catch (fallbackError) { console.error("Fallback API also failed:", fallbackError); } } finally { setLoading(false); } } return (

Media Update

{/* Tab */}
{/* Slider */} {loading ? (

Loading...

) : ( {dataToRender.map((item) => (
{item.title
{item.category || "Tanpa Kategori"} {item.label || ""}

{formatTanggal(item.createdAt)}

{item.title}

))}
)} {/* Lihat lebih banyak */}
); }