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
|
"use client"
import * as React from "react"
import type {
DataTableAdvancedFilterField,
DataTableFilterField,
DataTableRowAction,
} from "@/types/table"
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 { getEvaluationSubmissions, EvaluationSubmissionWithVendor } from "../service"
import { getColumns } from "./evaluation-submissions-table-columns"
import { EsgEvaluationFormSheet } from "./esg-evaluation-form-sheet"
import { useRouter } from "next/navigation"
import { GeneralEvaluationFormSheet } from "./general-evaluation-form-sheet"
import { EvaluationSubmissionDialog } from "./evaluation-submit-dialog"
interface EvaluationSubmissionsTableProps {
promises: Promise<
[
Awaited<ReturnType<typeof getEvaluationSubmissions>>,
]
>
}
export function EvaluationSubmissionsTable({ promises }: EvaluationSubmissionsTableProps) {
// 1. 데이터 로딩 상태 관리
const [isLoading, setIsLoading] = React.useState(true)
const [tableData, setTableData] = React.useState<{
data: EvaluationSubmissionWithVendor[]
pageCount: number
}>({ data: [], pageCount: 0 })
const router = useRouter()
// 2. 행 액션 상태 관리
const [rowAction, setRowAction] =
React.useState<DataTableRowAction<EvaluationSubmissionWithVendor> | null>(null)
// 3. Promise 해결을 useEffect로 처리
React.useEffect(() => {
promises
.then(([result]) => {
setTableData(result)
setIsLoading(false)
})
// .catch((error) => {
// console.error('Failed to load evaluation submissions:', error)
// setIsLoading(false)
// })
}, [promises])
// 4. 컬럼 정의
const columns = React.useMemo(
() => getColumns({ setRowAction }),
[setRowAction]
)
// 5. 필터 필드 정의
const filterFields: DataTableFilterField<EvaluationSubmissionWithVendor>[] = [
{
id: "submissionStatus",
label: "제출상태",
placeholder: "상태 선택...",
},
{
id: "evaluationYear",
label: "평가연도",
placeholder: "연도 선택...",
},
]
const advancedFilterFields: DataTableAdvancedFilterField<EvaluationSubmissionWithVendor>[] = [
{
id: "submissionId",
label: "제출 ID",
type: "text",
},
{
id: "evaluationYear",
label: "평가연도",
type: "number",
},
{
id: "evaluationRound",
label: "평가회차",
type: "text",
},
{
id: "submissionStatus",
label: "제출상태",
type: "select",
options: [
{ label: "임시저장", value: "draft" },
{ label: "제출완료", value: "submitted" },
{ label: "검토중", value: "under_review" },
{ label: "승인", value: "approved" },
{ label: "반려", value: "rejected" },
],
},
{
id: "submittedAt",
label: "제출일시",
type: "date",
},
{
id: "reviewedAt",
label: "검토일시",
type: "date",
},
{
id: "averageEsgScore",
label: "ESG 점수",
type: "number",
},
{
id: "createdAt",
label: "생성일",
type: "date",
},
{
id: "updatedAt",
label: "수정일",
type: "date",
},
]
// 6. 데이터 테이블 설정
const { table } = useDataTable({
data: tableData.data,
columns,
pageCount: tableData.pageCount,
filterFields,
enablePinning: true,
enableAdvancedFilter: true,
initialState: {
sorting: [{ id: "createdAt", desc: true }],
columnPinning: { left: ["select"], right: ["actions"] },
},
getRowId: (originalRow) => String(originalRow.id),
shallow: false,
clearOnDefault: true,
})
// 7. 데이터 새로고침 함수
const handleRefresh = React.useCallback(() => {
setIsLoading(true)
router.refresh()
}, [router])
// 8. 각종 성공 핸들러
const handleActionSuccess = React.useCallback(() => {
setRowAction(null)
table.resetRowSelection()
handleRefresh()
}, [handleRefresh, table])
// 9. 로딩 상태 표시
if (isLoading) {
return (
<div className="flex items-center justify-center h-32">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900"></div>
<span className="ml-2">평가 제출 목록을 불러오는 중...</span>
</div>
)
}
return (
<>
{/* 메인 테이블 */}
<DataTable table={table}>
<DataTableAdvancedToolbar
table={table}
filterFields={advancedFilterFields}
shallow={false}
>
</DataTableAdvancedToolbar>
</DataTable>
{/* 일반평가 작성 시트 */}
<GeneralEvaluationFormSheet
open={rowAction?.type === "general_evaluation"}
onOpenChange={() => setRowAction(null)}
submission={rowAction?.row.original ?? null}
onSuccess={handleActionSuccess}
/>
{/* ESG평가 작성 시트 */}
<EsgEvaluationFormSheet
open={rowAction?.type === "esg_evaluation"}
onOpenChange={() => setRowAction(null)}
submission={rowAction?.row.original ?? null}
onSuccess={handleActionSuccess}
/>
<EvaluationSubmissionDialog
open={rowAction?.type === "submit"}
onOpenChange={() => setRowAction(null)}
submission={rowAction?.row.original ?? null}
onSuccess={handleActionSuccess}
/>
</>
)
}
|