siakad-itm/resources/js/pages/admin/users/students/columns.tsx

191 lines
6.4 KiB
TypeScript

import type { ColumnDef } from '@tanstack/react-table';
import { Key, Pencil, Trash2 } from 'lucide-react';
import { ActiveStatusSwitch } from '@/components/active-status-switch';
import { GenderBadge } from '@/components/gender-badge';
import { RowActions } from '@/components/row-actions';
import { StatusBadge } from '@/components/status-badge';
const StudentStatusVariants: Record<
string,
'default' | 'secondary' | 'destructive' | 'outline'
> = {
active: 'default',
on_leave: 'secondary',
graduated: 'outline',
dropped_out: 'destructive',
};
export type Student = {
id: number;
username: string;
email: string;
is_active: boolean;
profile: { full_name: string; phone_number: string; gender: string } | null;
student: {
student_number: string;
department: { name: string } | null;
enrollment_year: number;
current_semester: number;
status: string | null;
} | null;
};
type StatusOption = { value: string; label: string };
type CreateColumnsParams = {
handleEdit: (student: Student) => void;
handleDeleteClick: (student: Student) => void;
handleResetPassword: (student: Student) => void;
handleStatusChange: (student: Student, status: string) => void;
handleUserStatusChange: (student: Student, isActive: boolean) => void;
statuses: StatusOption[];
canUpdate: boolean;
canDelete: boolean;
canResetPassword: boolean;
canUpdateAcademicStatus: boolean;
canUpdateAccountStatus: boolean;
};
export function createStudentColumns(
params: CreateColumnsParams,
): ColumnDef<Student>[] {
const {
handleEdit,
handleDeleteClick,
handleResetPassword,
handleStatusChange,
handleUserStatusChange,
statuses,
canUpdate,
canDelete,
canResetPassword,
canUpdateAcademicStatus,
canUpdateAccountStatus,
} = params;
const columns: ColumnDef<Student>[] = [
{
id: 'identity',
header: () => <span>NIM</span>,
cell: ({ row }) => (
<div className="flex flex-col">
<span>{row.original.student?.student_number ?? '-'}</span>
<span className="text-sm text-muted-foreground">
{row.original.profile?.full_name ?? '-'}
</span>
</div>
),
},
{
id: 'account',
header: () => <span>Akun</span>,
cell: ({ row }) => (
<div className="flex flex-col">
<span>{row.original.username}</span>
<span className="text-sm text-muted-foreground">
{row.original.email}
</span>
</div>
),
},
{
accessorKey: 'student.department.name',
header: () => <span>Jurusan</span>,
cell: ({ row }) => row.original.student?.department?.name ?? '-',
},
{
accessorKey: 'student.enrollment_year',
header: () => <span>Angkatan</span>,
cell: ({ row }) => row.original.student?.enrollment_year ?? '-',
},
{
accessorKey: 'student.current_semester',
header: () => <span>Semester</span>,
cell: ({ row }) => row.original.student?.current_semester ?? '-',
},
{
accessorKey: 'student.status',
header: () => <span>Status</span>,
cell: ({ row }) => (
<StatusBadge
status={row.original.student?.status}
statuses={statuses}
variants={StudentStatusVariants}
onChange={(status) =>
handleStatusChange(row.original, status)
}
disabled={!canUpdateAcademicStatus}
/>
),
},
{
accessorKey: 'profile.phone_number',
header: () => <span>Nomor Telepon</span>,
cell: ({ row }) => row.original.profile?.phone_number ?? '-',
},
{
accessorKey: 'profile.gender',
header: () => <span>Jenis Kelamin</span>,
cell: ({ row }) => (
<GenderBadge gender={row.original.profile?.gender} />
),
},
{
accessorKey: 'is_active',
header: () => <span>Status Akun</span>,
cell: ({ row }) => (
<ActiveStatusSwitch
isActive={row.original.is_active}
onChange={(isActive) =>
handleUserStatusChange(row.original, isActive)
}
disabled={!canUpdateAccountStatus}
/>
),
},
];
if (canUpdate || canResetPassword || canDelete) {
columns.push({
id: 'actions',
header: () => <span className="block text-center">Aksi</span>,
meta: {
className: 'w-[100px] text-center',
headerClassName: 'w-[100px] text-center',
},
cell: ({ row }) => {
const student = row.original;
return (
<RowActions
actions={[
{
label: 'Edit',
icon: <Pencil className="h-4 w-4" />,
show: canUpdate,
onClick: () => handleEdit(student),
},
{
label: 'Reset Kata Sandi',
icon: <Key className="h-4 w-4" />,
show: canResetPassword,
onClick: () => handleResetPassword(student),
},
{
label: 'Hapus',
icon: (
<Trash2 className="h-4 w-4 text-destructive" />
),
show: canDelete,
onClick: () => handleDeleteClick(student),
},
]}
/>
);
},
});
}
return columns;
}