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"; import { useTranslations } from "next-intl"; const useTableColumns = () => { const t = useTranslations("Table"); // Panggil di dalam hook const columns: ColumnDef[] = [ { accessorKey: "no", header: t("no"), cell: ({ row }) => (

{row.getValue("no")}

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

{row.getValue("hastagCode")}

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

{row.getValue("theme")}

), }, { accessorKey: "duration", header: t("duration"), cell: ({ row }) => ( {row.getValue("duration")} ), }, { accessorKey: "targetOutput", header: t("target-output"), cell: ({ row }) => ( {row.getValue("targetOutput")} ), }, { accessorKey: "targetParticipantTopLevel", header: t("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")); const isPublishForAll: boolean = Boolean(row.original.isPublishForAll); const isPublishForMabes: boolean = Boolean( row.original.isPublishForMabes ); const isPending: boolean = (userRoleId === 3 && userLevelNumber === 1 && !isPublishForAll) || ((userRoleId === 11 || userRoleId === 12) && !isPublishForMabes); const isTerkirim: boolean = isPublishForMabes && !isPublishForAll; return ( {isPending ? "Pending" : isTerkirim ? "Terkirim" : "Publish"} ); }, }, { id: "actions", accessorKey: "action", header: t("action"), 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 */} ); }, }, ]; return columns; }; export default useTableColumns;