- Updated EmployeeController to accept request filters for employment status, activity status, and gender. - Modified EmployeeService to support filtering in the getAll method. - Enhanced Employee index page with a filter toolbar for better user experience. - Fixed route parameter naming for employee-related routes. - Added comprehensive tests for employee index functionality and filtering capabilities.
246 lines
9.7 KiB
TypeScript
246 lines
9.7 KiB
TypeScript
import { ConfirmDialog } from '@/components/confirm-dialog';
|
|
import { DataTable } from '@/components/data-table';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
|
import { destroy, create as employeeCreate, edit as employeeEdit, index as employeeIndex, toggleActive, resetPassword as resetPasswordRoute } from '@/routes/admin/hr/employees';
|
|
import { Head, router } from '@inertiajs/react';
|
|
import { Filter, Plus, X } from 'lucide-react';
|
|
import { useState } from 'react';
|
|
import type { Employee } from './columns';
|
|
import { createEmployeeColumns } from './columns';
|
|
|
|
type Props = {
|
|
employees: Employee[];
|
|
filters: {
|
|
employment_status?: string;
|
|
is_active?: string;
|
|
gender?: string;
|
|
};
|
|
};
|
|
|
|
export default function EmployeeIndex({ employees, filters }: Props) {
|
|
const [deleting, setDeleting] = useState<Employee | null>(null);
|
|
const [resetPasswordTarget, setResetPasswordTarget] = useState<Employee | null>(null);
|
|
const [filterOpen, setFilterOpen] = useState(false);
|
|
|
|
const hasActiveFilters = filters.employment_status || filters.is_active;
|
|
|
|
function applyFilter(key: string, value: string) {
|
|
const newFilters = { ...filters };
|
|
|
|
if (value === '' || value === 'all') {
|
|
delete newFilters[key as keyof typeof newFilters];
|
|
} else {
|
|
newFilters[key as keyof typeof newFilters] = value;
|
|
}
|
|
|
|
router.get(employeeIndex.url(), newFilters, {
|
|
preserveState: true,
|
|
replace: true,
|
|
});
|
|
}
|
|
|
|
function clearFilters() {
|
|
router.get(employeeIndex.url(), {}, {
|
|
preserveState: true,
|
|
replace: true,
|
|
});
|
|
setFilterOpen(false);
|
|
}
|
|
|
|
function handleDelete() {
|
|
if (!deleting) {
|
|
return;
|
|
}
|
|
|
|
router.delete(destroy.url(deleting.id), {
|
|
onSuccess: () => setDeleting(null),
|
|
});
|
|
}
|
|
|
|
function handleResetPassword() {
|
|
if (!resetPasswordTarget) {
|
|
return;
|
|
}
|
|
|
|
router.post(resetPasswordRoute.url(resetPasswordTarget.id), {}, {
|
|
onSuccess: () => setResetPasswordTarget(null),
|
|
});
|
|
}
|
|
|
|
const columns = createEmployeeColumns({
|
|
handleEdit: (employee) => {
|
|
window.location.href = employeeEdit.url(employee.id);
|
|
},
|
|
handleDeleteClick: (employee) => setDeleting(employee),
|
|
handleResetPassword: (employee) => setResetPasswordTarget(employee),
|
|
toggleActiveUrl: (id) => toggleActive.url(id),
|
|
});
|
|
|
|
const filterToolbar = (
|
|
<Popover open={filterOpen} onOpenChange={setFilterOpen}>
|
|
<PopoverTrigger asChild>
|
|
<Button variant="outline" size="sm">
|
|
<Filter className="h-4 w-4" />
|
|
Filter
|
|
{hasActiveFilters && (
|
|
<span className="ml-1 flex h-5 w-5 items-center justify-center rounded-full bg-primary text-xs text-primary-foreground">
|
|
{Object.values(filters).filter(Boolean).length}
|
|
</span>
|
|
)}
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="w-64" align="end">
|
|
<div className="flex flex-col gap-4">
|
|
<div className="flex items-center justify-between">
|
|
<span className="text-sm font-medium">Filter</span>
|
|
{hasActiveFilters && (
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className="h-6 px-2 text-xs"
|
|
onClick={clearFilters}
|
|
>
|
|
<X className="mr-1 h-3 w-3" />
|
|
Hapus Semua
|
|
</Button>
|
|
)}
|
|
</div>
|
|
|
|
<div className="flex flex-col gap-2">
|
|
<label className="text-xs text-muted-foreground">
|
|
Status Karyawan
|
|
</label>
|
|
<Select
|
|
value={filters.employment_status ?? 'all'}
|
|
onValueChange={(value) => applyFilter('employment_status', value)}
|
|
>
|
|
<SelectTrigger className="w-full">
|
|
<SelectValue placeholder="Semua Status" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="all">Semua Status</SelectItem>
|
|
<SelectItem value="full_time">Full Time</SelectItem>
|
|
<SelectItem value="part_time">Part Time</SelectItem>
|
|
<SelectItem value="contract">Kontrak</SelectItem>
|
|
<SelectItem value="internship">Magang</SelectItem>
|
|
<SelectItem value="resigned">Keluar</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
|
|
<div className="flex flex-col gap-2">
|
|
<label className="text-xs text-muted-foreground">
|
|
Status Aktif
|
|
</label>
|
|
<Select
|
|
value={filters.is_active ?? 'all'}
|
|
onValueChange={(value) => applyFilter('is_active', value)}
|
|
>
|
|
<SelectTrigger className="w-full">
|
|
<SelectValue placeholder="Semua" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="all">Semua</SelectItem>
|
|
<SelectItem value="1">Aktif</SelectItem>
|
|
<SelectItem value="0">Tidak Aktif</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
|
|
<div className="flex flex-col gap-2">
|
|
<label className="text-xs text-muted-foreground">
|
|
Jenis Kelamin
|
|
</label>
|
|
<Select
|
|
value={filters.gender ?? 'all'}
|
|
onValueChange={(value) => applyFilter('gender', value)}
|
|
>
|
|
<SelectTrigger className="w-full">
|
|
<SelectValue placeholder="Semua" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="all">Semua</SelectItem>
|
|
<SelectItem value="male">Laki-laki</SelectItem>
|
|
<SelectItem value="female">Perempuan</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
</div>
|
|
</PopoverContent>
|
|
</Popover>
|
|
);
|
|
|
|
return (
|
|
<>
|
|
<Head title="Pegawai" />
|
|
|
|
<div className="flex h-full flex-1 flex-col gap-6 overflow-x-auto p-4 md:p-6">
|
|
<div className="flex items-center justify-between">
|
|
<div>
|
|
<h2 className="text-2xl font-semibold tracking-tight">
|
|
Pegawai
|
|
</h2>
|
|
</div>
|
|
<Button asChild>
|
|
<a href={employeeCreate.url()}>
|
|
<Plus className="h-4 w-4" />
|
|
Tambah
|
|
</a>
|
|
</Button>
|
|
</div>
|
|
|
|
<DataTable
|
|
columns={columns}
|
|
data={employees}
|
|
searchKey="full_name"
|
|
searchPlaceholder="Cari pegawai..."
|
|
emptyText="Belum ada data pegawai."
|
|
toolbar={filterToolbar}
|
|
/>
|
|
|
|
<ConfirmDialog
|
|
open={deleting !== null}
|
|
onOpenChange={(open) => {
|
|
if (!open) {
|
|
setDeleting(null);
|
|
}
|
|
}}
|
|
title="Hapus Pegawai"
|
|
description={`Apakah Anda yakin ingin menghapus pegawai "${deleting?.user_profile?.full_name}"? Tindakan ini tidak dapat dibatalkan.`}
|
|
confirmLabel="Hapus"
|
|
onConfirm={handleDelete}
|
|
/>
|
|
|
|
<ConfirmDialog
|
|
open={resetPasswordTarget !== null}
|
|
onOpenChange={(open) => {
|
|
if (!open) {
|
|
setResetPasswordTarget(null);
|
|
}
|
|
}}
|
|
title="Reset Kata Sandi"
|
|
description={`Apakah Anda yakin ingin mereset kata sandi pegawai "${resetPasswordTarget?.user_profile?.full_name}" ke kata sandi default?`}
|
|
confirmLabel="Reset"
|
|
variant="default"
|
|
onConfirm={handleResetPassword}
|
|
/>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
|
|
EmployeeIndex.layout = {
|
|
breadcrumbs: [
|
|
{
|
|
title: 'HR',
|
|
href: employeeIndex.url(),
|
|
},
|
|
{
|
|
title: 'Pegawai',
|
|
href: employeeIndex.url(),
|
|
},
|
|
],
|
|
};
|