dstpabuaran.com/resources/js/pages/admin/master/supplier/index.tsx
Yoga Pangestu 8edeaf0db7 feat: implement supplier management with CRUD functionality
- Added SupplierController for handling supplier operations.
- Created SupplierRequest for validation of supplier data.
- Introduced SupplierService for business logic related to suppliers.
- Developed UI components for supplier management, including a data table and dialogs for creating and editing suppliers.
- Updated routes to include resourceful routes for suppliers.
- Added SupplierSeeder for populating initial supplier data.
- Implemented tests for supplier functionality, including creation, updating, and deletion.
2026-07-29 01:16:58 +07:00

255 lines
13 KiB
TypeScript

import { Form, Head, router } from '@inertiajs/react';
import { Plus } from 'lucide-react';
import { useState } from 'react';
import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { destroy, index as supplierIndex, store, update } from '@/routes/admin/master/suppliers';
import { createSupplierColumns } from './columns';
import type { Supplier } from './columns';
import { ConfirmDialog } from '@/components/confirm-dialog';
import { DataTable } from '@/components/data-table';
type Props = {
suppliers: Supplier[];
};
export default function SupplierIndex({ suppliers }: Props) {
const [createOpen, setCreateOpen] = useState(false);
const [editing, setEditing] = useState<Supplier | null>(null);
const [deleting, setDeleting] = useState<Supplier | null>(null);
function handleDelete() {
if (!deleting) {
return;
}
router.delete(destroy(deleting.id), {
onSuccess: () => setDeleting(null),
});
}
const columns = createSupplierColumns({
handleEdit: (supplier) => setEditing(supplier),
handleDeleteClick: (supplier) => setDeleting(supplier),
});
return (
<>
<Head title="Supplier" />
<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">
Supplier
</h2>
</div>
<Dialog open={createOpen} onOpenChange={setCreateOpen}>
<Button asChild>
<button
type="button"
onClick={() => setCreateOpen(true)}
>
<Plus className="h-4 w-4" />
Tambah
</button>
</Button>
<DialogContent>
<Form action={store()} resetOnSuccess onSuccess={() => setCreateOpen(false)}>
{({ errors, processing }) => {
return (
<>
<DialogHeader>
<DialogTitle>Tambah Supplier</DialogTitle>
</DialogHeader>
<div className="grid gap-4 py-4">
<div className="grid gap-2">
<Label htmlFor="name">
Nama{' '} <span className="text-destructive">*</span>
</Label>
<Input
id="name"
name="name"
placeholder="Masukkan nama supplier"
/>
<InputError message={errors.name} />
</div>
<div className="grid gap-2">
<Label htmlFor="phone_number">
No. Telepon
</Label>
<Input
id="phone_number"
name="phone_number"
type="tel"
inputMode="numeric"
pattern="[0-9]*"
placeholder="Masukkan nomor telepon"
/>
<InputError message={errors.phone_number} />
</div>
<div className="grid gap-2">
<Label htmlFor="address">
Alamat
</Label>
<Input
id="address"
name="address"
placeholder="Masukkan alamat"
/>
<InputError message={errors.address} />
</div>
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => setCreateOpen(false)}
>
Batal
</Button>
<Button
type='submit'
disabled={processing}
>
{processing
? 'Menyimpan...'
: 'Simpan'}
</Button>
</DialogFooter>
</>
);
}}
</Form>
</DialogContent>
</Dialog>
</div>
<DataTable
columns={columns}
data={suppliers}
searchKey="name"
searchPlaceholder="Cari supplier..."
emptyText="Belum ada data supplier."
/>
<Dialog
open={editing !== null}
onOpenChange={(open) => {
if (!open) {
setEditing(null);
}
}}
>
<DialogContent>
{editing && (
<Form action={update(editing.id)} resetOnSuccess onSuccess={() => setEditing(null)}>
{({ errors, processing }) => {
return (
<>
<DialogHeader>
<DialogTitle>Edit Supplier</DialogTitle>
</DialogHeader>
<div className="grid gap-4 py-4">
<div className="grid gap-2">
<Label htmlFor="edit-name">Nama{' '} <span className="text-destructive">*</span></Label>
<Input
id="edit-name"
name="name"
placeholder="Masukkan nama supplier"
defaultValue={editing.name}
/>
<InputError message={errors.name} />
</div>
<div className="grid gap-2">
<Label htmlFor="edit-phone_number">No. Telepon</Label>
<Input
id="edit-phone_number"
name="phone_number"
type="tel"
inputMode="numeric"
pattern="[0-9]*"
placeholder="Masukkan nomor telepon"
defaultValue={editing.phone_number ?? ''}
/>
<InputError message={errors.phone_number} />
</div>
<div className="grid gap-2">
<Label htmlFor="edit-address">Alamat</Label>
<Input
id="edit-address"
name="address"
placeholder="Masukkan alamat"
defaultValue={editing.address ?? ''}
/>
<InputError message={errors.address} />
</div>
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => {
setEditing(null);
}}
>
Batal
</Button>
<Button
type="submit"
disabled={processing}
>
{processing
? 'Menyimpan...'
: 'Simpan'}
</Button>
</DialogFooter>
</>
);
}}
</Form>
)}
</DialogContent>
</Dialog>
<ConfirmDialog
open={deleting !== null}
onOpenChange={(open) => {
if (!open) {
setDeleting(null);
}
}}
title="Hapus Supplier"
description={`Apakah Anda yakin ingin menghapus supplier "${deleting?.name}"? Tindakan ini tidak dapat dibatalkan.`}
confirmLabel="Hapus"
onConfirm={handleDelete}
/>
</div>
</>
);
}
SupplierIndex.layout = {
breadcrumbs: [
{
title: 'Master',
href: supplierIndex(),
},
{
title: 'Supplier',
href: supplierIndex(),
},
],
};