"use client"; import Image from "next/image"; import Link from "next/link"; import { ThumbsUp, ThumbsDown } from "lucide-react"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { listData, listArticles } from "@/service/landing/landing"; import { getCookiesDecrypt } from "@/lib/utils"; import { toggleBookmark } from "@/service/content"; export default function Header() { const [data, setData] = useState([]); useEffect(() => { const fetchData = async () => { try { // 🔹 Gunakan API artikel baru const response = await listArticles( 1, 5, undefined, undefined, undefined, "createdAt" ); if (response?.error) { // fallback ke API lama const fallbackResponse = await listData( "", "", "", 5, 0, "createdAt", "", "", "" ); const content = fallbackResponse?.data?.data?.content || []; setData(content); return; } const articlesData = response?.data?.data || []; const transformedData = articlesData.map((article: any) => ({ id: article.id, title: article.title, categoryName: article.categoryName || (article.categories && article.categories[0]?.title) || "", createdAt: article.createdAt, smallThumbnailLink: article.thumbnailUrl, fileTypeId: article.typeId, tenantName: article.tenantName, categories: article.categories, label: article.typeId === 1 ? "Image" : article.typeId === 2 ? "Video" : article.typeId === 3 ? "Text" : article.typeId === 4 ? "Audio" : "", })); setData(transformedData); } catch (error) { console.error("Gagal memuat data:", error); try { const fallbackResponse = await listData( "", "", "", 5, 0, "createdAt", "", "", "" ); const content = fallbackResponse?.data?.data?.content || []; setData(content); } catch (fallbackError) { console.error("Fallback API juga gagal:", fallbackError); } } }; fetchData(); }, []); return (
{data.length > 0 && }
{data.slice(1, 5).map((item) => ( ))}
{"pps"}
); } // ======================== // 🔹 CARD COMPONENT // ======================== function Card({ item, isBig = false }: { item: any; isBig?: boolean }) { const router = useRouter(); const MySwal = withReactContent(Swal); const [isSaving, setIsSaving] = useState(false); const [isBookmarked, setIsBookmarked] = useState(false); const getLink = () => { switch (item?.fileTypeId) { case 1: return `/content/image/detail/${item?.id}`; case 2: return `/content/video/detail/${item?.id}`; case 3: return `/content/text/detail/${item?.id}`; case 4: return `/content/audio/detail/${item?.id}`; default: return "#"; } }; // ✅ Fungsi simpan bookmark const handleSave = async () => { const roleId = Number(getCookiesDecrypt("urie")); // 🔸 Cek apakah user login (roleId ada & valid) if (!roleId || roleId === 0 || isNaN(roleId)) { MySwal.fire({ icon: "warning", title: "Login diperlukan", text: "Silakan login terlebih dahulu untuk menyimpan artikel.", confirmButtonText: "Login Sekarang", confirmButtonColor: "#d33", }).then(() => { router.push("/auth"); }); return; } try { setIsSaving(true); const res = await toggleBookmark(item.id); console.log("Toggle Bookmark Response:", res); if (res?.error) { MySwal.fire({ icon: "error", title: "Gagal", text: "Gagal menyimpan artikel.", confirmButtonColor: "#d33", }); } else { setIsBookmarked(true); MySwal.fire({ icon: "success", title: "Berhasil", text: "Artikel berhasil disimpan ke bookmark.", confirmButtonColor: "#3085d6", timer: 1500, showConfirmButton: false, }); } } catch (err) { console.error("Toggle bookmark error:", err); MySwal.fire({ icon: "error", title: "Kesalahan", text: "Terjadi kesalahan saat menyimpan artikel.", confirmButtonColor: "#d33", }); } finally { setIsSaving(false); } }; return (
{item.title}
{item.tenantName} {item.categories?.map((cat: any) => cat.title).join(", ")}
{new Date(item.createdAt) .toLocaleString("id-ID", { day: "2-digit", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit", hour12: false, timeZone: "Asia/Jakarta", }) .replace(".", ":")}{" "} WIB

{item.title}

{/* ✅ Tombol Save/Disable */}
); } // "use client"; // import Image from "next/image"; // import Link from "next/link"; // import { ThumbsUp, ThumbsDown } from "lucide-react"; // import { useEffect, useState } from "react"; // import { // listData, // listArticles, // } from "@/service/landing/landing"; // export default function Header() { // const [data, setData] = useState([]); // useEffect(() => { // const fetchData = async () => { // try { // // Use new Articles API // const response = await listArticles( // 1, // 5, // undefined, // undefined, // undefined, // "createdAt" // ); // console.log("Articles API response:", response); // if (response?.error) { // console.error("Articles API failed, falling back to old API"); // // Fallback to old API // const fallbackResponse = await listData( // "", // "", // "", // 5, // 0, // "createdAt", // "", // "", // "" // ); // const content = fallbackResponse?.data?.data?.content || []; // setData(content); // return; // } // // Handle new API response structure // const articlesData = response?.data?.data || []; // console.log("Articles data:", articlesData); // // Transform articles data to match old structure for backward compatibility // const transformedData = articlesData.map((article: any) => ({ // id: article.id, // title: article.title, // categoryName: // article.categoryName || // (article.categories && article.categories[0]?.title) || // "", // createdAt: article.createdAt, // smallThumbnailLink: article.thumbnailUrl, // fileTypeId: article.typeId, // label: // article.typeId === 1 // ? "Image" // : article.typeId === 2 // ? "Video" // : article.typeId === 3 // ? "Text" // : article.typeId === 4 // ? "Audio" // : "", // ...article, // })); // setData(transformedData); // } catch (error) { // console.error("Gagal memuat data:", error); // // Try fallback to old API if new API fails // try { // const fallbackResponse = await listData( // "", // "", // "", // 5, // 0, // "createdAt", // "", // "", // "" // ); // const content = fallbackResponse?.data?.data?.content || []; // setData(content); // } catch (fallbackError) { // console.error("Fallback API also failed:", fallbackError); // } // } // }; // fetchData(); // }, []); // return ( //
//
// {data.length > 0 && } //
// {data.slice(1, 5).map((item) => ( // // ))} //
//
//
// {"pps"} //
//
// ); // } // function Card({ item, isBig = false }: { item: any; isBig?: boolean }) { // const getLink = () => { // switch (item?.fileTypeId) { // case 1: // return `/content/image/detail/${item?.id}`; // case 2: // return `/content/video/detail/${item?.id}`; // case 3: // return `/content/text/detail/${item?.id}`; // case 4: // return `/content/audio/detail/${item?.id}`; // default: // return "#"; // fallback kalau type tidak dikenali // } // }; // return ( //
//
//
// // {" "} // {item.title} // //
//
//
// // {item.tenantName} // // // {" "} // {item.categories?.map((cat: any) => cat.title).join(", ")} // //
//
// {new Date(item.createdAt) // .toLocaleString("id-ID", { // day: "2-digit", // month: "short", // year: "numeric", // hour: "2-digit", // minute: "2-digit", // hour12: false, // timeZone: "Asia/Jakarta", // }) // .replace(".", ":")}{" "} // WIB //
// // {" "} //

// {item.title} //

// //
//
// // //
// //
//
//
//
// ); // }