mediahub-fe/app/[locale]/(protected)/admin/add-experts/component/column.tsx

166 lines
4.6 KiB
TypeScript
Raw Normal View History

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";
2025-06-09 07:41:13 +00:00
import { Link } from "@/components/navigation";
2025-06-11 06:08:44 +00:00
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";
2025-11-23 15:37:29 +00:00
const getColumns = ({ onRefresh }: { onRefresh: () => void }): ColumnDef<any>[] => [
{
accessorKey: "no",
header: "No",
cell: ({ row }) => <span>{row.getValue("no")}</span>,
},
{
2025-03-03 03:00:17 +00:00
accessorKey: "fullname",
header: "Nama",
2025-03-03 03:00:17 +00:00
cell: ({ row }) => <span>{row.getValue("fullname")}</span>,
},
2025-11-23 15:37:29 +00:00
{
2025-03-03 03:00:17 +00:00
accessorKey: "address",
header: "Wilayah",
2025-11-23 15:37:29 +00:00
cell: () => <span>MABES</span>,
},
2025-11-23 15:37:29 +00:00
2025-06-09 07:41:13 +00:00
{
2025-06-11 04:38:13 +00:00
accessorKey: "userRolePlacements",
2025-06-09 07:41:13 +00:00
header: "Posisi",
2025-06-11 04:38:13 +00:00
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>;
},
2025-06-09 07:41:13 +00:00
},
2025-11-23 15:37:29 +00:00
{
2025-03-03 03:00:17 +00:00
accessorKey: "role.name",
header: "Bidang Keahlian",
2025-06-09 07:41:13 +00:00
cell: ({ row }) => (
<span>
{row.original.userProfilesAdditional?.userCompetency?.name ?? "-"}
</span>
),
},
{
2025-06-09 08:41:30 +00:00
accessorKey: "userExperienceId",
header: "Pengalaman",
2025-06-09 07:41:13 +00:00
cell: ({ row }) => {
const experienceId =
row.original.userProfilesAdditional?.userExperienceId;
2025-06-09 07:41:13 +00:00
const experienceMap: Record<number, string> = {
1: "Akademisi",
2: "Praktisi",
3: "Akademisi + Praktisi",
};
return <span>{experienceMap[experienceId] ?? "-"}</span>;
},
},
{
id: "actions",
header: "Actions",
cell: ({ row }) => {
2025-06-11 06:08:44 +00:00
const { toast } = useToast();
const MySwal = withReactContent(Swal);
2025-11-23 15:37:29 +00:00
2025-06-11 06:08:44 +00:00
const doDelete = async (id: number) => {
2025-11-19 16:40:31 +00:00
Swal.fire({
title: "Menghapus user...",
2025-11-23 15:37:29 +00:00
text: "Mohon tunggu",
2025-11-19 16:40:31 +00:00
allowOutsideClick: false,
2025-11-23 15:37:29 +00:00
didOpen: () => Swal.showLoading(),
2025-11-19 16:40:31 +00:00
});
2025-06-11 06:08:44 +00:00
const response = await deleteUser(id);
2025-11-19 16:40:31 +00:00
2025-11-23 15:37:29 +00:00
Swal.close();
2025-11-19 16:40:31 +00:00
2025-11-23 15:37:29 +00:00
if (response?.error) {
2025-06-11 06:08:44 +00:00
toast({
title: stringify(response?.message),
variant: "destructive",
});
2025-11-19 16:40:31 +00:00
return;
2025-06-11 06:08:44 +00:00
}
2025-11-19 16:40:31 +00:00
2025-11-23 15:37:29 +00:00
toast({ title: "Berhasil menghapus user" });
2025-06-11 06:08:44 +00:00
2025-11-23 15:37:29 +00:00
// ⬅️ INI YANG PENTING → REFRESH TABLE TANPA RELOAD
onRefresh();
2025-06-11 06:08:44 +00:00
};
const handleDelete = (id: number) => {
MySwal.fire({
2025-11-23 15:37:29 +00:00
title: "Hapus user ini?",
2025-06-11 06:08:44 +00:00
showCancelButton: true,
confirmButtonColor: "#dc3545",
confirmButtonText: "Iya",
cancelButtonText: "Tidak",
2025-11-23 15:37:29 +00:00
}).then((res) => {
if (res.isConfirmed) doDelete(id);
2025-06-11 06:08:44 +00:00
});
};
2025-11-19 16:40:31 +00:00
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
2025-11-23 15:37:29 +00:00
<Button size="icon" variant="ghost">
<MoreVertical className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
2025-11-23 15:37:29 +00:00
<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
2025-06-09 07:41:13 +00:00
</DropdownMenuItem>
</Link>
2025-11-23 15:37:29 +00:00
<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
2025-06-09 07:41:13 +00:00
</DropdownMenuItem>
</Link>
2025-11-23 15:37:29 +00:00
2025-06-09 07:41:13 +00:00
<DropdownMenuItem
2025-06-11 06:08:44 +00:00
onClick={() => handleDelete(row.original.userKeycloakId)}
2025-11-23 15:37:29 +00:00
className="text-red-600 cursor-pointer hover:bg-red-300"
2025-06-09 07:41:13 +00:00
>
2025-11-23 15:37:29 +00:00
<Trash2 className="w-4 h-4 me-1.5" /> Delete
</DropdownMenuItem>
2025-11-23 15:37:29 +00:00
</DropdownMenuContent>
</DropdownMenu>
);
},
},
];
2025-11-23 15:37:29 +00:00
export default getColumns;