/** * 倉庫對話框元件 * 重構後:加入驗證邏輯 */ import { useEffect, useState } from "react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/Components/ui/dialog"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/Components/ui/alert-dialog"; import { Input } from "@/Components/ui/input"; import { Label } from "@/Components/ui/label"; import { Textarea } from "@/Components/ui/textarea"; import { Button } from "@/Components/ui/button"; import { Trash2 } from "lucide-react"; import { Warehouse } from "@/types/warehouse"; import { validateWarehouse } from "@/utils/validation"; import { toast } from "sonner"; interface WarehouseDialogProps { open: boolean; onOpenChange: (open: boolean) => void; warehouse: Warehouse | null; onSave: (warehouse: Omit) => void; onDelete?: (warehouseId: string) => void; } export default function WarehouseDialog({ open, onOpenChange, warehouse, onSave, onDelete, }: WarehouseDialogProps) { const [formData, setFormData] = useState<{ code: string; name: string; address: string; description: string; is_sellable: boolean; }>({ code: "", name: "", address: "", description: "", is_sellable: true, }); const [showDeleteDialog, setShowDeleteDialog] = useState(false); useEffect(() => { if (warehouse) { setFormData({ code: warehouse.code, name: warehouse.name, address: warehouse.address || "", description: warehouse.description || "", is_sellable: warehouse.is_sellable ?? true, }); } else { setFormData({ code: "", name: "", address: "", description: "", is_sellable: true, }); } }, [warehouse, open]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const validation = validateWarehouse(formData); if (!validation.isValid) { toast.error(validation.error); return; } onSave(formData); }; const handleDelete = () => { if (warehouse && onDelete) { onDelete(warehouse.id); setShowDeleteDialog(false); onOpenChange(false); } }; return ( <> {warehouse ? "編輯倉庫" : "新增倉庫"} {warehouse ? "修改倉庫資訊" : "建立新的倉庫資訊"}
{/* 區塊 A:基本資訊 */}

基本資訊

{/* 倉庫編號 */}
{/* 倉庫名稱 */}
setFormData({ ...formData, name: e.target.value })} placeholder="例:中央倉庫" required />
{/* 銷售設定 */}

銷售設定

setFormData({ ...formData, is_sellable: e.target.checked })} />
{/* 區塊 B:位置 */}

位置與描述

{/* 倉庫地址 */}
setFormData({ ...formData, address: e.target.value })} placeholder="例:台北市信義區信義路五段7號" required />
{/* 備註說明 */}