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

{row.getValue("no")}

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

{row.getValue("hastagCode")}

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

{row.getValue("theme")}

), }, { accessorKey: "duration", header: t("duration", { defaultValue: "Duration" }), cell: ({ row }) => ( {row.getValue("duration")} ), }, { accessorKey: "targetOutput", header: t("target-output", { defaultValue: "Target Output" }), cell: ({ row }) => ( {row.getValue("targetOutput")} ), }, { accessorKey: "targetParticipantTopLevel", header: t("target-participant", { defaultValue: "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 isPublish: boolean = isPublishForMabes === true; const isPending: boolean = (userRoleId === 3 && userLevelNumber === 1 && !isPublishForAll) || ((userRoleId === 11 || userRoleId === 12) && !isPublishForMabes); return ( {isPending ? "Pending" : isPublish ? "Publish" : "Terkirim"} ); }, }, { id: "actions", accessorKey: "action", header: t("action", { defaultValue: "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); } }); }; async function doDelete(id: any) { // loading(); const data = { id, }; const response = await deleteContest(id); if (response?.error) { error(response.message); return false; } success(); } const handleDeleteContest = (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 ( {((userRoleId == 11 || userRoleId == 12) && row?.original?.isPublishForMabes != true) || (userRoleId == 3 && userLevelNumber == 1 && row?.original?.isPublishForAll != true) ? ( handlePublishContest(row.original.id)} > Publish ) : ( "" )} View Edit handleDeleteContest(row.original.id)} className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-destructive-foreground rounded-none" > Delete ); }, }, ]; return columns; }; export default useTableColumns;