import * as React from "react"; import { ColumnDef } from "@tanstack/react-table"; import { Eye, MoreVertical, SquarePen, Trash2, Upload } from "lucide-react"; import { cn, getCookiesDecrypt } 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 withReactContent from "sweetalert2-react-content"; import Swal from "sweetalert2"; import { error } from "@/lib/swal"; import { deleteMedia } from "@/service/content/content"; import { publishContest } from "@/service/contest/contest"; const columns: ColumnDef[] = [ { accessorKey: "no", header: "No", cell: ({ row }) => (

{row.getValue("no")}

), }, { accessorKey: "hastagCode", header: "Kode", cell: ({ row }) => (

{row.getValue("hastagCode")}

), }, { accessorKey: "theme", header: "Judul", cell: ({ row }) => (

{row.getValue("theme")}

), }, { accessorKey: "duration", header: "Durasi ", cell: ({ row }) => ( {row.getValue("duration")} ), }, { accessorKey: "targetOutput", header: "Target Output ", cell: ({ row }) => ( {row.getValue("targetOutput")} ), }, { accessorKey: "targetParticipantTopLevel", header: "Target Participant ", cell: ({ row }) => ( {row.getValue("targetParticipantTopLevel")} ), }, { accessorKey: "isPublishForAll", // Bisa menggunakan ini untuk membaca default data header: "Status", cell: ({ row, }: { row: { original: { isPublishForAll?: boolean; isPublishForMabes?: boolean }; }; }) => { const userRoleId: number = Number(getCookiesDecrypt("urie")); const userLevelNumber: number = Number(getCookiesDecrypt("ulne")); // Mengambil data dari row.original agar lebih aman const isPublishForAll: boolean = Boolean(row.original.isPublishForAll); const isPublishForMabes: boolean = Boolean( row.original.isPublishForMabes ); // Logika status berdasarkan role dan data dari API const isPending: boolean = (userRoleId === 3 && userLevelNumber === 1 && !isPublishForAll) || // Role 3 dengan level 1 hanya publish jika isPublishForAll true ((userRoleId === 11 || userRoleId === 12) && !isPublishForMabes); // Role 11 dan 12 hanya publish jika isPublishForMabes true return ( {isPending ? "Pending" : "Publish"} ); }, }, { id: "actions", accessorKey: "action", header: "Actions", enableHiding: false, cell: ({ row }) => { const MySwal = withReactContent(Swal); const userRoleId = Number(getCookiesDecrypt("urie")); const userLevelId = Number(getCookiesDecrypt("ulie")); const userLevelNumber = Number(getCookiesDecrypt("ulne")); async function doPublish(id: any) { // loading(); // const data = { // id, // }; const response = await publishContest(id); 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 handlePublishContest = (id: any) => { MySwal.fire({ title: "Apakah anda ingin publish Lomba?", showCancelButton: true, cancelButtonColor: "#3085d6", confirmButtonColor: "#d33", confirmButtonText: "Ya", cancelButtonText: "Tidak", }).then((result) => { if (result.isConfirmed) { doPublish(id); } }); }; return ( {((userRoleId == 11 || userRoleId == 12) && row?.original?.isPublishForMabes != true) || (userRoleId == 3 && userLevelNumber == 1 && row?.original?.isPublishForAll != true) ? ( handlePublishContest(row.original.id)} > Publish ) : ( "" )} View {/* Edit Delete */} ); }, }, ]; export default columns;