// 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 ( <> ) }