import * as React from "react"; import { ColumnDef } from "@tanstack/react-table"; import { Eye, MoreVertical, SquarePen, Trash2 } 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 { Link } from "@/components/navigation"; import { useTranslations } from "next-intl"; import withReactContent from "sweetalert2-react-content"; import Swal from "sweetalert2"; import { error } from "@/lib/swal"; import { deleteCalendar } from "@/service/schedule/schedule"; import { loading, success } from "@/config/swal"; const useTableColumns = () => { const t = useTranslations("Table"); const columns: ColumnDef[] = [ { accessorKey: "no", header: t("no", { defaultValue: "No" }), cell: ({ row }) => (

{row.getValue("no")}

), }, { accessorKey: "title", header: t("title", { defaultValue: "Title" }), cell: ({ row }: { row: { getValue: (key: string) => string } }) => { const title: string = row.getValue("title"); return ( {title.length > 50 ? `${title.slice(0, 30)}...` : title} ); }, }, { accessorKey: "startDate", header: t("start-date", { defaultValue: "Start Date" }), cell: ({ row }) => ( {row.getValue("startDate")} ), }, { accessorKey: "endDate", header: t("end-date", { defaultValue: "End Date" }), cell: ({ row }) => ( {row.getValue("endDate")} ), }, { accessorKey: "isActive", header: "Status", cell: ({ row }) => { const isActive = row.getValue("isActive") as boolean; const status = isActive ? "Aktif" : "Tidak Aktif"; const statusStyles = isActive ? "bg-green-100 text-green-600" : "bg-gray-100 text-gray-600"; return ( {status} ); }, }, { accessorKey: "createdByName", header: t("source", { defaultValue: "Source" }), cell: ({ row }) => ( {row.getValue("createdByName")} ), }, { id: "actions", accessorKey: "action", header: t("action", { defaultValue: "Action" }), enableHiding: false, cell: ({ row }) => { const MySwal = withReactContent(Swal); const levelNumber = Number(getCookiesDecrypt("ulne")); const userId = Number(getCookiesDecrypt("uie")); async function doDelete(id: any) { const response = await deleteCalendar(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 handleDeleteCalendars = (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 ( Detail {row.original.createdById === userId && ( Edit )} {row.original.createdById === userId && ( handleDeleteCalendars(row.original.id)} className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-destructive-foreground rounded-none" > Delete )} ); }, }, // { // id: "actions", // accessorKey: "action", // header: t("action", { defaultValue: "Action" }), // enableHiding: false, // cell: ({ row }) => { // const MySwal = withReactContent(Swal); // async function doDelete(id: any) { // // loading(); // const data = { // id, // }; // const response = await deleteCalendar(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 handleDeleteCalendars = (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 ( // // // // // // // // // Detail // // // // // // Edit // // // handleDeleteCalendars(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;