1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
"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<VendorMaterialsView> | null
setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<VendorMaterialsView> | null>>
}
export function ItemActionsDialogs({
vendorId,
rowAction,
setRowAction,
}: ItemActionsDialogsProps) {
const [isUpdatePending, startUpdateTransition] = React.useTransition()
const [isDeletePending, startDeleteTransition] = React.useTransition()
const [availableMaterials, setAvailableMaterials] = React.useState<any[]>([])
const [selectedItemCode, setSelectedItemCode] = React.useState<string>("")
// 사용 가능한 재료 목록 로드
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 (
<Dialog
open={true}
onOpenChange={(open) => !open && setRowAction(null)}
>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>Change Item</DialogTitle>
<DialogDescription>
Select a new item to replace "{item.itemName}" (Code: {item.itemCode}).
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
<div className="space-y-2">
<Label>Current Item</Label>
<div className="p-2 bg-muted rounded-md">
<div className="font-medium">{item.itemName}</div>
<div className="text-sm text-muted-foreground">Code: {item.itemCode}</div>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="newItem">New Item</Label>
<Select value={selectedItemCode} onValueChange={setSelectedItemCode}>
<SelectTrigger>
<SelectValue placeholder="Select a new item" />
</SelectTrigger>
<SelectContent>
{availableMaterials.map((material) => (
<SelectItem key={material.itemCode} value={material.itemCode}>
<div>
<div className="font-medium">{material.itemName}</div>
<div className="text-sm text-muted-foreground">Code: {material.itemCode}</div>
</div>
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => setRowAction(null)}
disabled={isUpdatePending}
>
Cancel
</Button>
<Button
onClick={handleSubmit}
disabled={isUpdatePending || !selectedItemCode}
>
{isUpdatePending ? "Updating..." : "Update Item"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
// 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 (
<AlertDialog
open={true}
onOpenChange={(open) => !open && setRowAction(null)}
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you sure?</AlertDialogTitle>
<AlertDialogDescription>
This will permanently delete the item "{item.itemName}" (Code: {item.itemCode}).
This action cannot be undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={isDeletePending}>
Cancel
</AlertDialogCancel>
<AlertDialogAction
onClick={handleDelete}
disabled={isDeletePending}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{isDeletePending ? "Deleting..." : "Delete"}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}
return (
<>
<EditDialog />
<DeleteDialog />
</>
)
}
|