import * as React from "react"; import { ColumnDef } from "@tanstack/react-table"; import { Eye, MoreVertical, SquarePen, Trash2 } from "lucide-react"; import { cn } from "@/lib/utils"; import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger, DropdownMenuItem, } from "@/components/ui/dropdown-menu"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { format } from "date-fns"; import { Link } from "@/components/navigation"; import { useRouter } from "next/navigation"; import { useToast } from "@/components/ui/use-toast"; import { deleteMedia } from "@/service/content/content"; import { error, loading } from "@/lib/swal"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; const MySwal = withReactContent(Swal); const columns: ColumnDef[] = [ { accessorKey: "no", header: "No", cell: ({ row }) => (

{row.getValue("no")}

), }, { accessorKey: "title", header: "Title", cell: ({ row }: { row: { getValue: (key: string) => string } }) => { const title: string = row.getValue("title"); return ( {title.length > 50 ? `${title.slice(0, 30)}...` : title} ); }, }, { accessorKey: "categoryName", header: "Category Name", cell: ({ row }) => ( {row.getValue("categoryName")} ), }, { accessorKey: "createdAt", header: "Upload Date", cell: ({ row }) => { const createdAt = row.getValue("createdAt") as | string | number | undefined; const formattedDate = createdAt && !isNaN(new Date(createdAt).getTime()) ? format(new Date(createdAt), "dd-MM-yyyy HH:mm:ss") : "-"; return {formattedDate}; }, }, { accessorKey: "creatorName", header: "Creator Group", cell: ({ row }) => ( {row.getValue("creatorName")} ), }, { accessorKey: "creatorGroupLevelName", header: "Sumber", cell: ({ row }) => ( {row.getValue("creatorGroupLevelName")} ), }, { accessorKey: "publishedOn", header: "Published", cell: ({ row }) => { const isPublish = row.original.isPublish; const isPublishOnPolda = row.original.isPublishOnPolda; let displayText = "-"; if (isPublish && !isPublishOnPolda) { displayText = "Mabes"; } else if (isPublish && isPublishOnPolda) { displayText = "Mabes & Polda"; } else if (!isPublish && isPublishOnPolda) { displayText = "Polda"; } return (
{displayText}
); }, }, { accessorKey: "statusId", header: "Status", cell: ({ row }) => { const userLevelId = 2; // Gantilah sesuai dengan konteks aplikasi const statusId = Number(row.getValue("statusId")); const reviewedAtLevel = row.original.reviewedAtLevel as string | null; const needApprovalFromLevel = Number(row.original.needApprovalFromLevel); let statusName = row.getValue("statusName") as string; let icon = "fa:times-circle"; let statusClass = "bg-gray-100 text-gray-600"; if ( (statusId === 2 && reviewedAtLevel !== null && !reviewedAtLevel.includes(`:${userLevelId}:`)) || (statusId === 1 && needApprovalFromLevel === userLevelId) ) { statusName = "Menunggu Review"; icon = "fa:hourglass-end"; statusClass = "bg-orange-100 text-orange-600"; } else if ( statusId === 2 && reviewedAtLevel?.includes(`:${userLevelId}:`) ) { icon = "fa:check-circle"; statusClass = "bg-green-100 text-green-600"; } else if (statusId === 2) { icon = "fa:check-circle"; statusClass = "bg-green-100 text-green-600"; } else if (statusId === 3) { icon = "fa:comment"; statusClass = "bg-blue-100 text-blue-600"; } else if (statusId === 1) { icon = "fa:hourglass-end"; statusClass = "bg-orange-100 text-orange-600"; } return ( {statusName} ); }, }, { id: "actions", accessorKey: "action", header: "Actions", enableHiding: false, cell: ({ row }) => { const router = useRouter(); const MySwal = withReactContent(Swal); async function doDelete(id: any) { // loading(); const data = { id, }; const response = await deleteMedia(data); if (response?.error) { error(response.message); return false; } success(); } function success() { MySwal.fire({ title: "Sukses", icon: "success", confirmButtonColor: "#3085d6", confirmButtonText: "OK", }).then((result) => { if (result.isConfirmed) { window.location.reload(); } }); } const handleDeleteMedia = (id: any) => { MySwal.fire({ title: "Hapus Data", text: "", icon: "warning", showCancelButton: true, cancelButtonColor: "#3085d6", confirmButtonColor: "#d33", confirmButtonText: "Hapus", }).then((result) => { if (result.isConfirmed) { doDelete(id); } }); }; return ( View Edit handleDeleteMedia(row.original.id)} className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-destructive-foreground rounded-none" > Delete ); }, }, ]; export default columns;