From bac0228d21b7195065e9cddcc327ae33659c7bcc Mon Sep 17 00:00:00 2001 From: dujinkim Date: Sun, 1 Jun 2025 13:52:21 +0000 Subject: (대표님) 20250601까지 작업사항 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/vendors/items-table/item-action-dialog.tsx | 248 +++++++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 lib/vendors/items-table/item-action-dialog.tsx (limited to 'lib/vendors/items-table/item-action-dialog.tsx') diff --git a/lib/vendors/items-table/item-action-dialog.tsx b/lib/vendors/items-table/item-action-dialog.tsx new file mode 100644 index 00000000..19df27f8 --- /dev/null +++ b/lib/vendors/items-table/item-action-dialog.tsx @@ -0,0 +1,248 @@ +// components/vendor-items/item-actions-dialogs.tsx +"use client" + +import * as React from "react" +import type { DataTableRowAction } from "@/types/table" +import { VendorItemsView } from "@/db/schema/vendors" +import { toast } from "sonner" + +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 { Button } from "@/components/ui/button" +import { Label } from "@/components/ui/label" +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select" + +import { updateVendorItem, deleteVendorItem, getItemsForVendor } from "../service" + +interface ItemActionsDialogsProps { + vendorId: number + rowAction: DataTableRowAction | null + setRowAction: React.Dispatch | null>> +} + +export function ItemActionsDialogs({ + vendorId, + rowAction, + setRowAction, +}: ItemActionsDialogsProps) { + const [isUpdatePending, startUpdateTransition] = React.useTransition() + const [isDeletePending, startDeleteTransition] = React.useTransition() + const [availableMaterials, setAvailableMaterials] = React.useState([]) + const [selectedItemCode, setSelectedItemCode] = React.useState("") + + // 사용 가능한 재료 목록 로드 + React.useEffect(() => { + if (rowAction?.type === "update") { + getItemsForVendor(vendorId).then((result) => { + if (result.data) { + setAvailableMaterials(result.data) + } + }) + } + }, [rowAction, vendorId]) + + // Edit Dialog + const EditDialog = () => { + if (!rowAction || rowAction.type !== "update") return null + + const item = rowAction.row.original + + const handleSubmit = () => { + if (!selectedItemCode) { + toast.error("Please select a new item") + return + } + + if (!item.itemCode) { + toast.error("Invalid item code") + return + } + + startUpdateTransition(async () => { + const result = await updateVendorItem(vendorId, item.itemCode, selectedItemCode) + + if (result.error) { + toast.error(result.error) + } else { + toast.success("Item updated successfully") + setRowAction(null) + } + }) + } + + return ( + !open && setRowAction(null)} + > + + + Change Item + + Select a new item to replace "{item.itemName}" (Code: {item.itemCode || 'N/A'}). + + + +
+
+ +
+
{item.itemName}
+
Code: {item.itemCode || 'N/A'}
+
+
+ +
+ + +
+
+ + + + + +
+
+ ) + } + + // Delete Dialog + const DeleteDialog = () => { + if (!rowAction || rowAction.type !== "delete") return null + + const item = rowAction.row.original + + const handleDelete = () => { + if (!item.itemCode) { + toast.error("Invalid item code") + return + } + + startDeleteTransition(async () => { + const result = await deleteVendorItem(vendorId, item.itemCode) + + if (result.error) { + toast.error(result.error) + } else { + toast.success("Item deleted successfully") + setRowAction(null) + } + }) + } + + return ( + !open && setRowAction(null)} + > + + return ( + !open && setRowAction(null)} + > + + + Are you sure? + + This will permanently delete the item "{item.itemName}" (Code: {item.itemCode || 'N/A'}). + This action cannot be undone. + + + + + Cancel + + + {isDeletePending ? "Deleting..." : "Delete"} + + + + + ) + } + + return ( + <> + + + + ) +} + + + Cancel + + + {isDeletePending ? "Deleting..." : "Delete"} + + + + + ) + } + + return ( + <> + + + + ) +} \ No newline at end of file -- cgit v1.2.3