322 lines
11 KiB
TypeScript
322 lines
11 KiB
TypeScript
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 { format } from "date-fns";
|
|
import { Link } from "@/components/navigation";
|
|
import { useRouter } from "next/navigation";
|
|
import { deleteTask } from "@/service/task";
|
|
import { error, loading } from "@/lib/swal";
|
|
import withReactContent from "sweetalert2-react-content";
|
|
import Swal from "sweetalert2";
|
|
import { useTranslations } from "next-intl";
|
|
|
|
const useTableColumns = () => {
|
|
const t = useTranslations("Table");
|
|
const columns: ColumnDef<any>[] = [
|
|
{
|
|
accessorKey: "no",
|
|
header: t("no", { defaultValue: "No" }),
|
|
cell: ({ row }) => <span>{row.getValue("no")}</span>,
|
|
},
|
|
{
|
|
accessorKey: "title",
|
|
header: t("title", { defaultValue: "Title" }),
|
|
cell: ({ row }) => (
|
|
<div>
|
|
<span>{row.getValue("title")}</span>
|
|
{row.original.isForward && (
|
|
<Button
|
|
variant={"outline"}
|
|
color="primary"
|
|
size="sm"
|
|
className="ml-3 rounded-xl"
|
|
>
|
|
Forward
|
|
</Button>
|
|
)}
|
|
</div>
|
|
),
|
|
},
|
|
|
|
{
|
|
accessorKey: "uniqueCode",
|
|
header: t("code", { defaultValue: "Code" }),
|
|
cell: ({ row }) => <span>{row.getValue("uniqueCode")}</span>,
|
|
},
|
|
|
|
{
|
|
accessorKey: "assignmentMainType",
|
|
header: t("type-task", { defaultValue: "Type Task" }),
|
|
cell: ({ row }) => {
|
|
const type = row.getValue("assignmentMainType") as { name: string };
|
|
return <span>{type?.name}</span>;
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "assignmentType",
|
|
header: t("category-task", { defaultValue: "Category Task" }),
|
|
cell: ({ row }) => {
|
|
const type = row.getValue("assignmentType") as { name: string };
|
|
return <span>{type?.name}</span>;
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "createdAt",
|
|
header: t("upload-date", { defaultValue: "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 <span className="whitespace-nowrap">{formattedDate}</span>;
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "status",
|
|
header: "Status",
|
|
cell: ({ row }) => {
|
|
const isActive = row.original.isActive;
|
|
const isDone = row.original.isDone;
|
|
|
|
let statusText = "";
|
|
if (isDone) {
|
|
statusText = "Selesai";
|
|
} else if (isActive) {
|
|
statusText = "Aktif";
|
|
} else {
|
|
statusText = "Nonaktif";
|
|
}
|
|
|
|
const statusColors: Record<string, string> = {
|
|
Aktif: "bg-primary/20 text-primary",
|
|
Selesai: "bg-success/20 text-success",
|
|
Nonaktif: "bg-gray-200 text-gray-500",
|
|
};
|
|
|
|
const statusStyles = statusColors[statusText] || "default";
|
|
|
|
return (
|
|
<Badge className={cn("rounded-full px-5", statusStyles)}>
|
|
{statusText}
|
|
</Badge>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
id: "actions",
|
|
accessorKey: "action",
|
|
header: t("action", { defaultValue: "Action" }),
|
|
enableHiding: false,
|
|
// cell: ({ row }) => {
|
|
// const router = useRouter();
|
|
// const MySwal = withReactContent(Swal);
|
|
|
|
// async function deleteProcess(id: any) {
|
|
// loading();
|
|
// const resDelete = await deleteTask(id);
|
|
|
|
// if (resDelete?.error) {
|
|
// error(resDelete.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 TaskDelete = (id: any) => {
|
|
// MySwal.fire({
|
|
// title: "Hapus Data",
|
|
// text: "",
|
|
// icon: "warning",
|
|
// showCancelButton: true,
|
|
// cancelButtonColor: "#3085d6",
|
|
// confirmButtonColor: "#d33",
|
|
// confirmButtonText: "Hapus",
|
|
// }).then((result) => {
|
|
// if (result.isConfirmed) {
|
|
// deleteProcess(id);
|
|
// }
|
|
// });
|
|
// };
|
|
|
|
// return (
|
|
// <DropdownMenu>
|
|
// <DropdownMenuTrigger asChild>
|
|
// <Button
|
|
// size="icon"
|
|
// className="bg-transparent ring-offset-transparent hover:bg-transparent hover:ring-0 hover:ring-transparent"
|
|
// >
|
|
// <span className="sr-only">Open menu</span>
|
|
// <MoreVertical className="h-4 w-4 text-default-800" />
|
|
// </Button>
|
|
// </DropdownMenuTrigger>
|
|
// <DropdownMenuContent className="p-0" align="end">
|
|
// <Link href={`/contributor/task/detail/${row.original.id}`}>
|
|
// <DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
|
|
// <Eye className="w-4 h-4 me-1.5" />
|
|
// View
|
|
// </DropdownMenuItem>
|
|
// </Link>
|
|
// <Link href={`/contributor/task/update/${row.original.id}`}>
|
|
// <DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
|
|
// <SquarePen className="w-4 h-4 me-1.5" />
|
|
// Edit
|
|
// </DropdownMenuItem>
|
|
// </Link>
|
|
// <DropdownMenuItem
|
|
// onClick={() => TaskDelete(row.original.id)}
|
|
// className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-destructive-foreground rounded-none"
|
|
// >
|
|
// <Trash2 className="w-4 h-4 me-1.5" />
|
|
// Delete
|
|
// </DropdownMenuItem>
|
|
// </DropdownMenuContent>
|
|
// </DropdownMenu>
|
|
// );
|
|
// },
|
|
cell: ({ row }) => {
|
|
const router = useRouter();
|
|
const MySwal = withReactContent(Swal);
|
|
const roleId = getCookiesDecrypt("urie");
|
|
const levelNumber = getCookiesDecrypt("ulne");
|
|
const userLevelId = getCookiesDecrypt("ulie");
|
|
const contentLevelNumber =
|
|
row.original.createdBy?.userLevel?.levelNumber;
|
|
const isSameLevelOrLower =
|
|
levelNumber &&
|
|
contentLevelNumber &&
|
|
+levelNumber <= +contentLevelNumber;
|
|
|
|
async function deleteProcess(id: any) {
|
|
loading();
|
|
const resDelete = await deleteTask(id);
|
|
|
|
if (resDelete?.error) {
|
|
error(resDelete.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 TaskDelete = (id: any) => {
|
|
MySwal.fire({
|
|
title: "Hapus Data",
|
|
text: "",
|
|
icon: "warning",
|
|
showCancelButton: true,
|
|
cancelButtonColor: "#3085d6",
|
|
confirmButtonColor: "#d33",
|
|
confirmButtonText: "Hapus",
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
deleteProcess(id);
|
|
}
|
|
});
|
|
};
|
|
|
|
const isDone = row.original.isDone;
|
|
|
|
return (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button
|
|
size="icon"
|
|
className="bg-transparent ring-offset-transparent hover:bg-transparent hover:ring-0 hover:ring-transparent"
|
|
>
|
|
<span className="sr-only">Open menu</span>
|
|
<MoreVertical className="h-4 w-4 text-default-800" />
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent className="p-0" align="end">
|
|
<Link href={`/contributor/task/detail/${row.original.id}`}>
|
|
<DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
|
|
<Eye className="w-4 h-4 me-1.5" />
|
|
View
|
|
</DropdownMenuItem>
|
|
</Link>
|
|
|
|
{/* {!isDone && (
|
|
<>
|
|
<Link href={`/contributor/task/update/${row.original.id}`}>
|
|
<DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
|
|
<SquarePen className="w-4 h-4 me-1.5" />
|
|
Edit
|
|
</DropdownMenuItem>
|
|
</Link>
|
|
<DropdownMenuItem
|
|
onClick={() => TaskDelete(row.original.id)}
|
|
className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-destructive-foreground rounded-none"
|
|
>
|
|
<Trash2 className="w-4 h-4 me-1.5" />
|
|
Delete
|
|
</DropdownMenuItem>
|
|
</>
|
|
)} */}
|
|
{!isDone && isSameLevelOrLower && (
|
|
<>
|
|
<Link href={`/contributor/task/update/${row.original.id}`}>
|
|
<DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
|
|
<SquarePen className="w-4 h-4 me-1.5" />
|
|
Edit
|
|
</DropdownMenuItem>
|
|
</Link>
|
|
<DropdownMenuItem
|
|
onClick={() => TaskDelete(row.original.id)}
|
|
className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-destructive-foreground rounded-none"
|
|
>
|
|
<Trash2 className="w-4 h-4 me-1.5" />
|
|
Delete
|
|
</DropdownMenuItem>
|
|
</>
|
|
)}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
);
|
|
},
|
|
},
|
|
];
|
|
|
|
return columns;
|
|
};
|
|
|
|
export default useTableColumns;
|