220 lines
11 KiB
TypeScript

import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { EmptyState } from '@/components/ui/empty-state';
import { Input } from '@/components/ui/input';
import { Pagination } from '@/components/ui/pagination';
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
import AppLayout from '@/layouts/app-layout';
import { type BreadcrumbItem } from '@/types';
import { Head, Link, usePage } from '@inertiajs/react';
import { PencilIcon, PlusIcon, Trash2Icon } from 'lucide-react';
import { useEffect, useState } from 'react';
import { Toaster, toast } from 'sonner';
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from '@/components/ui/alert-dialog';
interface Procedure {
id: string;
code: string;
name: string;
category: string;
price: string;
tax: string;
approval: string;
status: string;
created_at: string;
}
interface PageProps {
procedures: {
data: Procedure[];
links: Array<{
url: string | null;
label: string;
active: boolean;
}>;
};
status?: string;
}
const breadcrumbs: BreadcrumbItem[] = [
{ title: 'Dashboard', href: '/dashboard' },
{ title: 'Prosedur', href: '/procedures' },
];
export default function ProcedureIndex() {
const { procedures, status } = usePage<PageProps>().props;
const [searchTerm, setSearchTerm] = useState('');
const [filteredProcedures, setFilteredProcedures] = useState<Procedure[]>(procedures.data);
useEffect(() => {
if (status) {
toast.success(status);
}
}, [status]);
useEffect(() => {
const results = procedures.data.filter(procedure =>
Object.values(procedure).some(
value =>
value &&
value.toString().toLowerCase().includes(searchTerm.toLowerCase())
)
);
setFilteredProcedures(results);
}, [searchTerm, procedures.data]);
return (
<AppLayout breadcrumbs={breadcrumbs}>
<Head title="Manajemen Prosedur" />
<Toaster position="top-right" richColors />
<div className="flex h-full flex-1 flex-col gap-4 rounded-xl p-4">
<div className="flex items-center justify-between">
<div>
<h1 className="text-2xl font-bold">Data Prosedur</h1>
</div>
<Button asChild>
<Link href={route('procedures.create')}>
<PlusIcon className="mr-2 h-4 w-4" />
Tambah Prosedur
</Link>
</Button>
</div>
<div className="w-full md:w-1/3">
<Input
type="text"
placeholder="Cari prosedur..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
/>
</div>
<div>
{filteredProcedures.length === 0 ? (
<EmptyState
title={searchTerm ? "Prosedur tidak ditemukan" : "Belum ada prosedur"}
description={searchTerm ?
"Tidak ada prosedur yang sesuai dengan pencarian Anda" :
"Mulai dengan menambahkan prosedur baru"}
action={
<Button asChild>
<Link href={route('procedures.create')}>
<PlusIcon className="mr-2 h-4 w-4" />
Tambah Prosedur
</Link>
</Button>
}
/>
) : (
<>
<div className="rounded-md border">
<Table>
<TableHeader>
<TableRow>
<TableHead>Kode</TableHead>
<TableHead>Nama Prosedur</TableHead>
<TableHead>Kategori</TableHead>
<TableHead>Harga Dasar</TableHead>
<TableHead>Pajak</TableHead>
<TableHead>Persetujuan</TableHead>
<TableHead>Status</TableHead>
<TableHead>Dibuat Pada</TableHead>
<TableHead>Aksi</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{filteredProcedures.map((procedure) => (
<TableRow key={procedure.id}>
<TableCell className="font-medium">
{procedure.code}
</TableCell>
<TableCell>{procedure.name}</TableCell>
<TableCell className="capitalize">
{procedure.category}
</TableCell>
<TableCell>{procedure.price}</TableCell>
<TableCell>{procedure.tax}</TableCell>
<TableCell>{procedure.approval}</TableCell>
<TableCell>
<Badge
variant={procedure.status === 'Aktif' ? 'default' : 'destructive'}
>
{procedure.status}
</Badge>
</TableCell>
<TableCell>{procedure.created_at}</TableCell>
<TableCell className="flex justify-start gap-2">
<Button
variant="ghost"
size="icon"
asChild
className="hover:bg-neutral-100"
>
<Link href={route('procedures.edit', procedure.id)}>
<PencilIcon className="h-4 w-4" />
</Link>
</Button>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button
variant="ghost"
size="icon"
className="text-red-600 hover:bg-red-50 hover:text-red-700"
>
<Trash2Icon className="h-4 w-4" />
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Apakah Anda yakin?</AlertDialogTitle>
<AlertDialogDescription>
Data prosedur akan dihapus permanen. Tindakan ini tidak dapat dibatalkan.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Batal</AlertDialogCancel>
<AlertDialogAction asChild>
<Link
href={route('procedures.destroy', procedure.id)}
method="delete"
as="button"
preserveScroll
>
Hapus
</Link>
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
{procedures.links.length > 3 && (
<div className="mt-4">
<Pagination links={procedures.links} />
</div>
)}
</>
)}
</div>
</div>
</AppLayout>
);
}