"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import { Button } from "@/components/ui/button"; import { ThumbsUp, ThumbsDown, Trash2 } from "lucide-react"; import Link from "next/link"; import { getBookmarks, toggleBookmarkById, BookmarkItem } from "@/service/content"; import { getCookiesDecrypt } from "@/lib/utils"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; // 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" ); } // Function to get link based on typeId function getLink(item: BookmarkItem) { switch (item.article?.typeId) { case 1: return `/content/image/detail/${item.article?.id}`; case 2: return `/content/video/detail/${item.article?.id}`; case 3: return `/content/text/detail/${item.article?.id}`; case 4: return `/content/audio/detail/${item.article?.id}`; default: return "#"; } } // Function to get content type label function getContentTypeLabel(typeId: number) { switch (typeId) { case 1: return "📸 Image"; case 2: return "🎬 Video"; case 3: return "📝 Text"; case 4: return "🎵 Audio"; default: return "📄 Content"; } } type PublicationCardGridProps = { selectedCategory: string; title?: string; refresh?: boolean; categoryFilter?: string[]; formatFilter?: string[]; isInstitute?: boolean; instituteId?: string; sortBy?: string; }; export default function ForYouCardGrid({ selectedCategory, title, refresh, isInstitute = false, instituteId = "", }: PublicationCardGridProps) { const [bookmarks, setBookmarks] = useState([]); const [loading, setLoading] = useState(true); const [currentPage, setCurrentPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const [totalCount, setTotalCount] = useState(0); const [limit] = useState(12); const MySwal = withReactContent(Swal); useEffect(() => { fetchBookmarks(); }, [currentPage, selectedCategory, title, refresh]); const fetchBookmarks = async () => { try { setLoading(true); const response = await getBookmarks(currentPage, limit); if (!response?.error) { setBookmarks(response.data?.data || []); setTotalPages(response.data?.meta?.totalPage || 1); setTotalCount(response.data?.meta?.count || 0); } else { console.error("Failed to fetch bookmarks:", response?.error); MySwal.fire({ icon: "error", title: "Error", text: "Gagal memuat bookmark.", confirmButtonColor: "#d33", }); } } catch (err) { console.error("Error fetching bookmarks:", err); MySwal.fire({ icon: "error", title: "Error", text: "Terjadi kesalahan saat memuat bookmark.", confirmButtonColor: "#d33", }); } finally { setLoading(false); } }; const handleDeleteBookmark = async (bookmarkId: number, articleId: number, articleTitle: string) => { const result = await MySwal.fire({ title: "Hapus Bookmark", text: `Apakah Anda yakin ingin menghapus "${articleTitle}" dari bookmark?`, icon: "warning", showCancelButton: true, confirmButtonColor: "#d33", cancelButtonColor: "#3085d6", confirmButtonText: "Ya, Hapus!", cancelButtonText: "Batal", }); if (result.isConfirmed) { try { const response = await toggleBookmarkById(articleId); if (response?.data?.success || !response?.error) { // Remove from local state setBookmarks(prev => prev.filter(bookmark => bookmark.id !== bookmarkId)); setTotalCount(prev => prev - 1); MySwal.fire({ icon: "success", title: "Berhasil", text: "Bookmark berhasil dihapus.", timer: 1500, showConfirmButton: false, }); } else { MySwal.fire({ icon: "error", title: "Gagal", text: "Gagal menghapus bookmark.", confirmButtonColor: "#d33", }); } } catch (err) { console.error("Error deleting bookmark:", err); MySwal.fire({ icon: "error", title: "Error", text: "Terjadi kesalahan saat menghapus bookmark.", confirmButtonColor: "#d33", }); } } }; const goToPage = (page: number) => { setCurrentPage(page); }; if (loading) { return (
{Array.from({ length: 6 }).map((_, index) => (
))}
); } if (bookmarks.length === 0) { return (
📚

Tidak Ada Bookmark

Anda belum menyimpan artikel apapun ke bookmark.

); } return (
{/* Header */}

Bookmark Saya

Total {totalCount} artikel tersimpan

{/* Grid Card */}
{bookmarks.map((bookmark) => (
{bookmark.article?.title
{getContentTypeLabel(bookmark.article?.typeId || 0)} {/* Bookmark */}

Disimpan: {formatTanggal(bookmark.createdAt)}

{bookmark.article?.title}

))}
{/* Pagination */}
{Array.from({ length: totalPages }, (_, i) => ( ))}
); } // "use client"; // import { useEffect, useState } from "react"; // import PublicationKlFilter from "./publication-filter"; // import { Button } from "@/components/ui/button"; // import { mediaWishlist } from "@/service/landing/landing"; // const itemsPerPage = 9; // type PublicationCardGridProps = { // selectedCategory: string; // title?: string; // refresh?: boolean; // categoryFilter?: string[]; // formatFilter?: string[]; // isInstitute?: boolean; // instituteId?: string; // sortBy?: string; // }; // export default function ForYouCardGrid({ // selectedCategory, // title, // refresh, // isInstitute = false, // instituteId = "", // }: PublicationCardGridProps) { // const [currentPage, setCurrentPage] = useState(0); // const [contentImage, setContentImage] = useState([]); // const [totalPages, setTotalPages] = useState(1); // const [categoryFilter] = useState([]); // const [formatFilter] = useState([]); // const [sortBy] = useState(); // useEffect(() => { // getDataImage(); // }, [currentPage, selectedCategory, title, refresh]); // async function getDataImage() { // const filter = // categoryFilter?.length > 0 // ? categoryFilter?.sort().join(",") // : selectedCategory || ""; // const name = title ?? ""; // const format = formatFilter?.join(",") ?? ""; // const response = await mediaWishlist( // "1", // isInstitute ? instituteId : "", // name, // filter, // "12", // currentPage, // sortBy, // format // ); // setTotalPages(response?.data?.data?.totalPages || 1); // setContentImage(response?.data?.data?.content || []); // } // const goToPage = (page: number) => { // setCurrentPage(page); // }; // return ( //
// {/* Grid Card */} //
// {contentImage.length === 0 ? ( //
// Tidak ada konten ditemukan. //
// ) : ( // contentImage.map((item: any, idx: number) => ( // // )) // )} //
// {/* Pagination */} //
// {Array.from({ length: totalPages }, (_, i) => ( // // ))} //
//
// ); // }