first commit
This commit is contained in:
185
resources/js/Pages/Vendor/Index.tsx
vendored
Normal file
185
resources/js/Pages/Vendor/Index.tsx
vendored
Normal file
@@ -0,0 +1,185 @@
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import { Button } from "@/Components/ui/button";
|
||||
import { Input } from "@/Components/ui/input";
|
||||
import { Plus, Search, X } from "lucide-react";
|
||||
import VendorTable from "@/Components/Vendor/VendorTable";
|
||||
import VendorDialog from "@/Components/Vendor/VendorDialog";
|
||||
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
|
||||
import { Head, router } from "@inertiajs/react";
|
||||
import { debounce } from "lodash";
|
||||
|
||||
export interface Vendor {
|
||||
id: number;
|
||||
code: string;
|
||||
name: string;
|
||||
short_name?: string;
|
||||
tax_id?: string;
|
||||
owner?: string;
|
||||
contact_name?: string;
|
||||
tel?: string;
|
||||
phone?: string;
|
||||
email?: string;
|
||||
address?: string;
|
||||
remark?: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
interface PageProps {
|
||||
vendors: {
|
||||
data: Vendor[];
|
||||
links: any[];
|
||||
meta: any;
|
||||
};
|
||||
filters: {
|
||||
search?: string;
|
||||
sort_field?: string;
|
||||
sort_direction?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export default function VendorManagement({ vendors, filters }: PageProps) {
|
||||
const [searchTerm, setSearchTerm] = useState(filters.search || "");
|
||||
const [sortField, setSortField] = useState<string | null>(filters.sort_field || null);
|
||||
const [sortDirection, setSortDirection] = useState<"asc" | "desc" | null>(filters.sort_direction as "asc" | "desc" || null);
|
||||
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
||||
const [editingVendor, setEditingVendor] = useState<Vendor | null>(null);
|
||||
|
||||
// Sync state with props
|
||||
useEffect(() => {
|
||||
setSearchTerm(filters.search || "");
|
||||
setSortField(filters.sort_field || null);
|
||||
setSortDirection(filters.sort_direction as "asc" | "desc" || null);
|
||||
}, [filters]);
|
||||
|
||||
// Debounced Search
|
||||
const debouncedSearch = useCallback(
|
||||
debounce((term: string) => {
|
||||
router.get(
|
||||
route("vendors.index"),
|
||||
{ search: term },
|
||||
{ preserveState: true, replace: true, preserveScroll: true }
|
||||
);
|
||||
}, 500),
|
||||
[]
|
||||
);
|
||||
|
||||
const handleSearchChange = (term: string) => {
|
||||
setSearchTerm(term);
|
||||
debouncedSearch(term);
|
||||
};
|
||||
|
||||
const handleClearSearch = () => {
|
||||
setSearchTerm("");
|
||||
router.get(
|
||||
route("vendors.index"),
|
||||
{ search: "" },
|
||||
{ preserveState: true, replace: true, preserveScroll: true }
|
||||
);
|
||||
};
|
||||
|
||||
const handleSort = (field: string) => {
|
||||
let newField: string | null = field;
|
||||
let newDirection: "asc" | "desc" | null = "asc";
|
||||
|
||||
if (sortField === field) {
|
||||
if (sortDirection === "asc") {
|
||||
newDirection = "desc";
|
||||
} else {
|
||||
newDirection = null;
|
||||
newField = null;
|
||||
}
|
||||
}
|
||||
|
||||
setSortField(newField);
|
||||
setSortDirection(newDirection);
|
||||
|
||||
router.get(
|
||||
route("vendors.index"),
|
||||
{
|
||||
search: searchTerm,
|
||||
sort_field: newField,
|
||||
sort_direction: newDirection
|
||||
},
|
||||
{ preserveState: true, replace: true, preserveScroll: true }
|
||||
);
|
||||
};
|
||||
|
||||
const handleAddVendor = () => {
|
||||
setEditingVendor(null);
|
||||
setIsDialogOpen(true);
|
||||
};
|
||||
|
||||
const handleEditVendor = (vendor: Vendor) => {
|
||||
setEditingVendor(vendor);
|
||||
setIsDialogOpen(true);
|
||||
};
|
||||
|
||||
const handleViewVendor = (vendor: Vendor) => {
|
||||
router.get(route("vendors.show", vendor.id));
|
||||
};
|
||||
|
||||
const handleDeleteVendor = (id: number) => {
|
||||
router.delete(route('vendors.destroy', id));
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthenticatedLayout>
|
||||
<Head title="廠商資料管理" />
|
||||
<div className="container mx-auto p-6 max-w-7xl">
|
||||
{/* Header */}
|
||||
<div className="mb-6">
|
||||
<h1 className="mb-2">廠商資料管理</h1>
|
||||
<p className="text-gray-600">管理 ERP 系統供應商與聯絡資訊</p>
|
||||
</div>
|
||||
|
||||
{/* Toolbar */}
|
||||
<div className="bg-white rounded-lg shadow-sm border p-4 mb-6">
|
||||
<div className="flex flex-col md:flex-row gap-4">
|
||||
{/* Search */}
|
||||
<div className="flex-1 relative">
|
||||
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 h-4 w-4" />
|
||||
<Input
|
||||
placeholder="搜尋廠商名稱、編號、統編、負責人..."
|
||||
value={searchTerm}
|
||||
onChange={(e) => handleSearchChange(e.target.value)}
|
||||
className="pl-10 pr-10"
|
||||
/>
|
||||
{searchTerm && (
|
||||
<button
|
||||
onClick={handleClearSearch}
|
||||
className="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-600"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Add Button */}
|
||||
<Button onClick={handleAddVendor} className="flex-1 md:flex-none button-filled-primary">
|
||||
<Plus className="mr-2 h-4 w-4" />
|
||||
新增廠商
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Vendor Table */}
|
||||
<VendorTable
|
||||
vendors={vendors.data}
|
||||
onView={handleViewVendor}
|
||||
onEdit={handleEditVendor}
|
||||
onDelete={handleDeleteVendor}
|
||||
sortField={sortField}
|
||||
sortDirection={sortDirection}
|
||||
onSort={handleSort}
|
||||
/>
|
||||
|
||||
<VendorDialog
|
||||
open={isDialogOpen}
|
||||
onOpenChange={setIsDialogOpen}
|
||||
vendor={editingVendor}
|
||||
/>
|
||||
</div>
|
||||
</AuthenticatedLayout>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user