/* eslint-disable @typescript-eslint/no-explicit-any */ import { Badge } from "@/shared/components/ds/badge"; import { Button } from "@/shared/components/ds/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger } from "@/shared/components/ui/dropdown-menu"; import { Organization } from "@/shared/types/organization"; import { ColumnDef } from "@tanstack/react-table"; import { ChevronsUpDown, MoreHorizontal, ChevronUp, ChevronDown } from "lucide-react"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import organizationApi from "@/shared/services/organization"; import { toast } from "sonner"; import { hasPermission } from "@/shared/config/role"; import { DeleteDialog } from "../../_components/delete-dialog"; import { useAuthSession } from "@/shared/hooks/use-session"; import { AxiosError } from "axios"; interface ColumnConfig { id: string; header: string; accessor?: keyof Organization; accessorFn?: (row: Organization) => any; sortable?: boolean; cell?: (value: any) => React.ReactNode; } const COLUMN_CONFIGS: ColumnConfig[] = [ { id: "name", header: "Nama Perangkat Daerah", accessor: "name", sortable: true, }, { id: "email", header: "Email", accessor: "email", sortable: true, }, { id: "phone_number", header: "Telepon", accessor: "phone_number", sortable: false, }, { id: "is_active", header: "Status", accessor: "is_active", sortable: true, cell: (value) => {value ? "Aktif" : "Tidak Aktif"}, }, ]; export const useOrganizationColumns = (): ColumnDef[] => { const router = useRouter(); const [organizationToDelete, setOrganizationToDelete] = useState(null); const queryClient = useQueryClient(); const { session } = useAuthSession(); const userRole = session?.user?.role; const deleteMutation = useMutation({ mutationFn: async (id: string) => { return await organizationApi.deleteOrganization(id); }, onSuccess: () => { toast.success("Berhasil menghapus data"); queryClient.invalidateQueries({ queryKey: ["organizations"] }); setOrganizationToDelete(null); }, onError: (error) => { const axiosError = error as AxiosError<{ detail?: string }>; const apiDetail = axiosError.response?.data?.detail; toast.error(apiDetail || "Gagal menghapus data"); console.error("Error deleting organization:", error); setOrganizationToDelete(null); }, }); const renderSortableHeader = (column: any, label: string) => ( ); const baseColumns = COLUMN_CONFIGS.map((config) => { const column: ColumnDef = { id: config.id, header: ({ column }) => (config.sortable ? renderSortableHeader(column, config.header) : config.header), ...(config.accessor && { accessorKey: config.accessor }), ...(config.accessorFn && { accessorFn: config.accessorFn }), cell: ({ row }) => { const value = row.getValue(config.id); return config.cell ? config.cell(value) :
{value as React.ReactNode}
; }, enableSorting: config.sortable !== false, enableHiding: config.id !== "select" && config.id !== "actions", }; return column; }); if (userRole && (hasPermission(userRole, "organization", "read") || hasPermission(userRole, "organization", "update") || hasPermission(userRole, "organization", "delete"))) { baseColumns.push({ id: "actions", enableHiding: false, cell: ({ row }) => { const organization = row.original; return ( <> Aksi {hasPermission(userRole, "organization", "read") && ( router.push(`/admin/organization/detail/${organization.id}`)} className="flex items-center gap-2"> Lihat Detail )} {hasPermission(userRole, "organization", "update") && ( router.push(`/admin/organization/edit/${organization.id}`)} className="flex items-center gap-2"> Edit Perangkat Daerah )} {hasPermission(userRole, "organization", "delete") && ( <> setOrganizationToDelete(organization)} className="flex items-center gap-2 text-destructive focus:text-destructive"> Hapus Perangkat Daerah )} {organizationToDelete?.id === organization.id && ( deleteMutation.mutate(organizationToDelete.id)} onCancel={() => setOrganizationToDelete(null)} open={true} /> )} ); }, }); } return baseColumns; };