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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
"use client"
import * as React from "react"
import type {
DataTableAdvancedFilterField,
DataTableFilterField,
DataTableRowAction,
} from "@/types/table"
import { useRouter } from "next/navigation"
import { useDataTable } from "@/hooks/use-data-table"
import { DataTable } from "@/components/data-table/data-table"
import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar"
import { useFeatureFlags } from "./feature-flags-provider"
import { getColumns } from "./rfqs-table-columns"
import { RfqWithAll } from "../types"
import {
fetchRfqAttachments,
fetchRfqAttachmentsbyCommentId,
} from "../../rfqs/service"
import { RfqsVendorTableToolbarActions } from "./rfqs-table-toolbar-actions"
import { RfqsItemsDialog } from "./ItemsDialog"
import { RfqAttachmentsSheet } from "./attachment-rfq-sheet"
import { CommentSheet } from "./comments-sheet"
import { getRfqResponsesForVendor } from "../service"
import { useSession } from "next-auth/react" // Next-auth session hook 추가
interface RfqsTableProps {
promises: Promise<[Awaited<ReturnType<typeof getRfqResponsesForVendor>>]>
}
// 코멘트+첨부파일 구조 예시
export interface RfqCommentWithAttachments {
id: number
commentText: string
commentedBy?: number
commentedByEmail?: string
createdAt?: Date
attachments?: {
id: number
fileName: string
filePath: string
}[]
}
export interface ExistingAttachment {
id: number
fileName: string
filePath: string
createdAt?: Date
vendorId?: number | null
size?: number
}
export interface ExistingItem {
id?: number
itemCode: string
description: string | null
quantity: number | null
uom: string | null
}
export function RfqsVendorTable({ promises }: RfqsTableProps) {
const { featureFlags } = useFeatureFlags()
const { data: session } = useSession() // 세션 정보 가져오기
// 1) 테이블 데이터( RFQs )
const [{ data: responseData, pageCount }] = React.use(promises)
// 데이터를 RfqWithAll 타입으로 변환 (id 필드 추가)
const data: RfqWithAll[] = React.useMemo(() => {
return responseData.map(item => ({
...item,
id: item.rfqId, // id 필드를 rfqId와 동일하게 설정
}));
}, [responseData]);
const router = useRouter()
// 2) 첨부파일 시트 + 관련 상태
const [attachmentsOpen, setAttachmentsOpen] = React.useState(false)
const [selectedRfqIdForAttachments, setSelectedRfqIdForAttachments] = React.useState<number | null>(null)
const [attachDefault, setAttachDefault] = React.useState<ExistingAttachment[]>([])
// 3) 코멘트 시트 + 관련 상태
const [initialComments, setInitialComments] = React.useState<RfqCommentWithAttachments[]>([])
const [commentSheetOpen, setCommentSheetOpen] = React.useState(false)
const [selectedRfqIdForComments, setSelectedRfqIdForComments] = React.useState<number | null>(null)
// 4) rowAction으로 다양한 모달/시트 열기
const [rowAction, setRowAction] = React.useState<DataTableRowAction<RfqWithAll> | null>(null)
// 열리고 닫힐 때마다, rowAction 등을 확인해서 시트 열기/닫기 처리
React.useEffect(() => {
if (rowAction?.type === "comments" && rowAction?.row.original) {
openCommentSheet(rowAction.row.original.id)
}
}, [rowAction])
/**
* (A) 코멘트 시트를 열기 전에,
* DB에서 (rfqId에 해당하는) 코멘트들 + 각 코멘트별 첨부파일을 조회.
*/
const openCommentSheet = React.useCallback(async (rfqId: number) => {
setInitialComments([])
// 여기서 rowAction을 직접 참조하지 않고, 필요한 데이터만 파라미터로 받기
const comments = data.find(rfq => rfq.rfqId === rfqId)?.comments || []
if (comments && comments.length > 0) {
const commentWithAttachments = await Promise.all(
comments.map(async (c) => {
const attachments = await fetchRfqAttachmentsbyCommentId(c.id)
return {
...c,
commentedBy: c.commentedBy || 1,
attachments,
}
})
)
setInitialComments(commentWithAttachments)
}
setSelectedRfqIdForComments(rfqId)
setCommentSheetOpen(true)
}, [data]) // data만 의존성으로 추가
/**
* (B) 첨부파일 시트 열기
*/
const openAttachmentsSheet = React.useCallback(async (rfqId: number) => {
const list = await fetchRfqAttachments(rfqId)
setAttachDefault(list)
setSelectedRfqIdForAttachments(rfqId)
setAttachmentsOpen(true)
}, [])
// 5) DataTable 컬럼 세팅
const columns = React.useMemo(
() =>
getColumns({
setRowAction,
router,
openAttachmentsSheet,
openCommentSheet
}),
[setRowAction, router, openAttachmentsSheet, openCommentSheet]
)
/**
* 간단한 filterFields 예시
*/
const filterFields: DataTableFilterField<RfqWithAll>[] = [
{
id: "rfqCode",
label: "RFQ Code",
placeholder: "Filter RFQ Code...",
},
{
id: "projectName",
label: "Project",
placeholder: "Filter Project...",
},
{
id: "rfqDescription",
label: "Description",
placeholder: "Filter Description...",
},
]
/**
* Advanced filter fields 예시
*/
const advancedFilterFields: DataTableAdvancedFilterField<RfqWithAll>[] = [
{
id: "rfqCode",
label: "RFQ Code",
type: "text",
},
{
id: "rfqDescription",
label: "Description",
type: "text",
},
{
id: "projectCode",
label: "Project Code",
type: "text",
},
{
id: "projectName",
label: "Project Name",
type: "text",
},
{
id: "rfqDueDate",
label: "Due Date",
type: "date",
},
{
id: "responseStatus",
label: "Response Status",
type: "select",
options: [
{ label: "Reviewing", value: "REVIEWING" },
{ label: "Accepted", value: "ACCEPTED" },
{ label: "Declined", value: "DECLINED" },
],
}
]
// useDataTable() 훅 -> pagination, sorting 등 관리
const { table } = useDataTable({
data,
columns,
pageCount,
filterFields,
enablePinning: true,
enableAdvancedFilter: true,
initialState: {
sorting: [{ id: "respondedAt", desc: true }],
columnPinning: { right: ["actions"] },
},
getRowId: (originalRow) => String(originalRow.id),
shallow: false,
clearOnDefault: true,
})
const currentUserId = session?.user?.id ? parseInt(session.user.id, 10) : 0
const currentVendorId = session?.user?.id ? session.user.companyId : 0
return (
<>
<DataTable table={table}>
<DataTableAdvancedToolbar
table={table}
filterFields={advancedFilterFields}
shallow={false}
>
<RfqsVendorTableToolbarActions table={table} />
</DataTableAdvancedToolbar>
</DataTable>
{/* 1) 아이템 목록 Dialog */}
{rowAction?.type === "items" && rowAction?.row.original && (
<RfqsItemsDialog
open={true}
onOpenChange={() => setRowAction(null)}
rfq={rowAction.row.original}
/>
)}
{/* 2) 코멘트 시트 */}
{selectedRfqIdForComments && (
<CommentSheet
open={commentSheetOpen}
onOpenChange={setCommentSheetOpen}
initialComments={initialComments}
rfqId={selectedRfqIdForComments}
vendorId={currentVendorId??0}
currentUserId={currentUserId}
/>
)}
{/* 3) 첨부파일 시트 */}
<RfqAttachmentsSheet
open={attachmentsOpen}
onOpenChange={setAttachmentsOpen}
rfqId={selectedRfqIdForAttachments ?? 0}
attachments={attachDefault}
/>
</>
)
}
|