166 lines
4.6 KiB
TypeScript
166 lines
4.6 KiB
TypeScript
import * as React from "react";
|
|
import { ColumnDef } from "@tanstack/react-table";
|
|
|
|
import { Eye, MoreVertical, SquarePen, Trash2 } from "lucide-react";
|
|
import { cn } from "@/lib/utils";
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuTrigger,
|
|
DropdownMenuItem,
|
|
} from "@/components/ui/dropdown-menu";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Link } from "@/components/navigation";
|
|
import { useToast } from "@/components/ui/use-toast";
|
|
import withReactContent from "sweetalert2-react-content";
|
|
import Swal from "sweetalert2";
|
|
import { useRouter } from "next/navigation";
|
|
import { deleteUser } from "@/service/management-user/management-user";
|
|
import { stringify } from "querystring";
|
|
|
|
const getColumns = ({ onRefresh }: { onRefresh: () => void }): ColumnDef<any>[] => [
|
|
{
|
|
accessorKey: "no",
|
|
header: "No",
|
|
cell: ({ row }) => <span>{row.getValue("no")}</span>,
|
|
},
|
|
|
|
{
|
|
accessorKey: "fullname",
|
|
header: "Nama",
|
|
cell: ({ row }) => <span>{row.getValue("fullname")}</span>,
|
|
},
|
|
|
|
{
|
|
accessorKey: "address",
|
|
header: "Wilayah",
|
|
cell: () => <span>MABES</span>,
|
|
},
|
|
|
|
{
|
|
accessorKey: "userRolePlacements",
|
|
header: "Posisi",
|
|
cell: ({ row }) => {
|
|
const placements = row.original.userRolePlacements || [];
|
|
const placement = placements.find(
|
|
(p: any) => p.roleId === 11 || p.roleId === 12
|
|
);
|
|
|
|
let posisi = "-";
|
|
if (placement) {
|
|
posisi = placement.roleId === 11 ? "Koorkurator" : "Kurator";
|
|
}
|
|
|
|
return <span>{posisi}</span>;
|
|
},
|
|
},
|
|
|
|
{
|
|
accessorKey: "role.name",
|
|
header: "Bidang Keahlian",
|
|
cell: ({ row }) => (
|
|
<span>
|
|
{row.original.userProfilesAdditional?.userCompetency?.name ?? "-"}
|
|
</span>
|
|
),
|
|
},
|
|
|
|
{
|
|
accessorKey: "userExperienceId",
|
|
header: "Pengalaman",
|
|
cell: ({ row }) => {
|
|
const experienceId =
|
|
row.original.userProfilesAdditional?.userExperienceId;
|
|
|
|
const experienceMap: Record<number, string> = {
|
|
1: "Akademisi",
|
|
2: "Praktisi",
|
|
3: "Akademisi + Praktisi",
|
|
};
|
|
|
|
return <span>{experienceMap[experienceId] ?? "-"}</span>;
|
|
},
|
|
},
|
|
|
|
{
|
|
id: "actions",
|
|
header: "Actions",
|
|
cell: ({ row }) => {
|
|
const { toast } = useToast();
|
|
const MySwal = withReactContent(Swal);
|
|
|
|
const doDelete = async (id: number) => {
|
|
Swal.fire({
|
|
title: "Menghapus user...",
|
|
text: "Mohon tunggu",
|
|
allowOutsideClick: false,
|
|
didOpen: () => Swal.showLoading(),
|
|
});
|
|
|
|
const response = await deleteUser(id);
|
|
|
|
Swal.close();
|
|
|
|
if (response?.error) {
|
|
toast({
|
|
title: stringify(response?.message),
|
|
variant: "destructive",
|
|
});
|
|
return;
|
|
}
|
|
|
|
toast({ title: "Berhasil menghapus user" });
|
|
|
|
// ⬅️ INI YANG PENTING → REFRESH TABLE TANPA RELOAD
|
|
onRefresh();
|
|
};
|
|
|
|
const handleDelete = (id: number) => {
|
|
MySwal.fire({
|
|
title: "Hapus user ini?",
|
|
showCancelButton: true,
|
|
confirmButtonColor: "#dc3545",
|
|
confirmButtonText: "Iya",
|
|
cancelButtonText: "Tidak",
|
|
}).then((res) => {
|
|
if (res.isConfirmed) doDelete(id);
|
|
});
|
|
};
|
|
|
|
return (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button size="icon" variant="ghost">
|
|
<MoreVertical className="h-4 w-4" />
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end">
|
|
|
|
<Link href={`/admin/add-experts/detail/${row.original.id}`}>
|
|
<DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none cursor-pointer">
|
|
<Eye className="w-4 h-4 me-1.5" /> View
|
|
</DropdownMenuItem>
|
|
</Link>
|
|
|
|
<Link href={`/admin/add-experts/update/${row.original.id}`}>
|
|
<DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none cursor-pointer">
|
|
<SquarePen className="w-4 h-4 me-1.5" /> Edit
|
|
</DropdownMenuItem>
|
|
</Link>
|
|
|
|
<DropdownMenuItem
|
|
onClick={() => handleDelete(row.original.userKeycloakId)}
|
|
className="text-red-600 cursor-pointer hover:bg-red-300"
|
|
>
|
|
<Trash2 className="w-4 h-4 me-1.5" /> Delete
|
|
</DropdownMenuItem>
|
|
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
);
|
|
},
|
|
},
|
|
];
|
|
|
|
export default getColumns;
|