"use client" import * as React from "react" import type { DataTableRowAction } from "@/types/table" import { VendorMaterialsView } 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 { deleteVendorMaterial, getMaterialsForVendor, updateVendorMaterial } 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") { getMaterialsForVendor(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 } startUpdateTransition(async () => { const result = await updateVendorMaterial(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}).
{item.itemName}
Code: {item.itemCode}
) } // Delete Dialog const DeleteDialog = () => { if (!rowAction || rowAction.type !== "delete") return null const item = rowAction.row.original const handleDelete = () => { startDeleteTransition(async () => { const result = await deleteVendorMaterial(vendorId, item.itemCode) if (result.success) { toast.success(result.message) setRowAction(null) } else { toast.error(result.message) } }) } return ( !open && setRowAction(null)} > Are you sure? This will permanently delete the item "{item.itemName}" (Code: {item.itemCode}). This action cannot be undone. Cancel {isDeletePending ? "Deleting..." : "Delete"} ) } return ( <> ) }