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
|
"use client"
import * as React from "react"
import { ClientDataTable } from "@/components/client-data-table/data-table"
import { getColumns } from "./vendor-list-table-column"
import { DataTableAdvancedFilterField } from "@/types/table"
import { addItemToVendors, getAllVendors } from "../../service"
import { Loader2, Plus } from "lucide-react"
import { Button } from "@/components/ui/button"
import { useToast } from "@/hooks/use-toast"
export interface VendorData {
id: number
vendorName: string
vendorCode: string | null
taxId: string
address: string | null
country: string | null
phone: string | null
email: string | null
website: string | null
status: string
createdAt: Date
updatedAt: Date
}
interface VendorsListTableProps {
rfqId: number
}
export function VendorsListTable({ rfqId }: VendorsListTableProps) {
const { toast } = useToast()
// Changed to array for multiple selection
const [selectedVendorIds, setSelectedVendorIds] = React.useState<number[]>([])
const [isSubmitting, setIsSubmitting] = React.useState(false)
const [vendors, setVendors] = React.useState<VendorData[]>([])
const [isLoading, setIsLoading] = React.useState(false)
const columns = React.useMemo(
() => getColumns({ setSelectedVendorIds }),
[setSelectedVendorIds]
)
// 고급 필터 필드 정의
const advancedFilterFields: DataTableAdvancedFilterField<VendorData>[] = [
{
id: "vendorName",
label: "Vendor Name",
type: "text",
},
{
id: "vendorCode",
label: "Vendor Code",
type: "text",
},
{
id: "status",
label: "Status",
type: "select",
options: [
{ label: "Active", value: "ACTIVE" },
{ label: "Inactive", value: "INACTIVE" },
{ label: "Pending", value: "PENDING" },
],
},
{
id: "country",
label: "Country",
type: "text",
},
{
id: "email",
label: "Email",
type: "text",
},
]
// 초기 데이터 로드
React.useEffect(() => {
async function loadVendors() {
setIsLoading(true)
try {
const result = await getAllVendors()
if (result.data) {
setVendors(result.data)
}
} catch (error) {
console.error("협력업체 목록 로드 오류:", error)
toast({
title: "Error",
description: "Failed to load vendors",
variant: "destructive",
})
} finally {
setIsLoading(false)
}
}
loadVendors()
}, [toast])
async function handleAddVendors() {
if (selectedVendorIds.length === 0) return // Safety check
setIsSubmitting(true)
try {
// Update to use the multiple vendor service
const result = await addItemToVendors(rfqId, selectedVendorIds)
if (result.success) {
toast({
title: "Success",
description: `Added items to ${selectedVendorIds.length} vendors`,
})
// Reset selection after successful addition
setSelectedVendorIds([])
} else {
toast({
title: "Error",
description: result.error || "Failed to add items to vendors",
variant: "destructive",
})
}
} catch (err) {
console.error("Failed to add vendors:", err)
toast({
title: "Error",
description: "An unexpected error occurred",
variant: "destructive",
})
} finally {
setIsSubmitting(false)
}
}
// If loading, show a flex container that fills the parent and centers the spinner
if (isLoading && vendors.length === 0) {
return (
<div className="flex h-full w-full items-center justify-center">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
)
}
return (
<ClientDataTable
data={vendors}
columns={columns}
advancedFilterFields={advancedFilterFields}
>
<div className="flex items-center gap-2">
<Button
variant="default"
size="sm"
onClick={handleAddVendors}
disabled={selectedVendorIds.length === 0 || isSubmitting}
>
{isSubmitting ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Adding...
</>
) : (
<>
<Plus className="mr-2 h-4 w-4" />
Add Vendors ({selectedVendorIds.length})
</>
)}
</Button>
</div>
</ClientDataTable>
)
}
|