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
|
"use client";
import * as React from "react";
import { type Table } from "@tanstack/react-table";
import { Download, Upload, FileSpreadsheet, Trash2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { useToast } from "@/hooks/use-toast";
import { deleteTechVendorPossibleItems } from "@/lib/tech-vendor-possible-items/service";
// Excel 함수들을 동적 import로만 사용하기 위해 타입만 import
type TechVendorPossibleItemsData = {
id: number;
vendorId: number;
vendorCode: string | null;
vendorName: string;
techVendorType: string;
itemCode: string;
createdAt: Date;
updatedAt: Date;
};
interface PossibleItemsTableToolbarActionsProps {
table: Table<TechVendorPossibleItemsData>;
}
export function PossibleItemsTableToolbarActions({
table,
}: PossibleItemsTableToolbarActionsProps) {
const { toast } = useToast();
const [isPending, startTransition] = React.useTransition();
const selectedRows = table.getFilteredSelectedRowModel().rows;
const hasSelection = selectedRows.length > 0;
const handleDelete = () => {
if (!hasSelection) return;
startTransition(async () => {
const selectedIds = selectedRows.map((row) => row.original.id);
try {
const result = await deleteTechVendorPossibleItems(selectedIds);
if (result.success) {
toast({
title: "성공",
description: `${selectedIds.length}개의 아이템이 삭제되었습니다.`,
});
table.toggleAllRowsSelected(false);
// 페이지 새로고침이나 데이터 다시 로드 필요
window.location.reload();
} else {
toast({
title: "오류",
description: result.error || "삭제 중 오류가 발생했습니다.",
variant: "destructive",
});
}
} catch (error) {
console.error("Delete error:", error);
toast({
title: "오류",
description: "삭제 중 오류가 발생했습니다.",
variant: "destructive",
});
}
});
};
const handleExport = async () => {
try {
const { exportTechVendorPossibleItemsToExcel } = await import("./excel-export");
const result = await exportTechVendorPossibleItemsToExcel(table.getFilteredRowModel().rows.map(row => row.original));
if (result.success) {
toast({
title: "성공",
description: "Excel 파일이 다운로드되었습니다.",
});
} else {
toast({
title: "오류",
description: result.error || "내보내기 중 오류가 발생했습니다.",
variant: "destructive",
});
}
} catch (error) {
console.error("Export error:", error);
toast({
title: "오류",
description: "내보내기 중 오류가 발생했습니다.",
variant: "destructive",
});
}
};
const handleImport = async (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (!file) return;
try {
const { importTechVendorPossibleItemsFromExcel } = await import("./excel-import");
const result = await importTechVendorPossibleItemsFromExcel(file);
if (result.success) {
toast({
title: "성공",
description: `${result.successCount}개의 아이템이 가져와졌습니다.`,
});
// 페이지 새로고침이나 데이터 다시 로드 필요
window.location.reload();
} else {
toast({
title: "가져오기 완료",
description: `${result.successCount}개 성공, ${result.failedRows.length}개 실패`,
variant: result.successCount > 0 ? "default" : "destructive",
});
}
} catch (error) {
console.error("Import error:", error);
toast({
title: "오류",
description: "가져오기 중 오류가 발생했습니다.",
variant: "destructive",
});
}
// Reset input
event.target.value = "";
};
const handleDownloadTemplate = async () => {
try {
const { exportTechVendorPossibleItemsTemplate } = await import("./excel-template");
const result = await exportTechVendorPossibleItemsTemplate();
if (result.success) {
toast({
title: "성공",
description: "템플릿 파일이 다운로드되었습니다.",
});
} else {
toast({
title: "오류",
description: result.error || "템플릿 다운로드 중 오류가 발생했습니다.",
variant: "destructive",
});
}
} catch (error) {
console.error("Template download error:", error);
toast({
title: "오류",
description: "템플릿 다운로드 중 오류가 발생했습니다.",
variant: "destructive",
});
}
};
return (
<div className="flex items-center gap-2">
{hasSelection && (
<Button
variant="destructive"
size="sm"
onClick={handleDelete}
disabled={isPending}
>
<Trash2 className="mr-2 h-4 w-4" />
삭제 ({selectedRows.length})
</Button>
)}
<Button variant="outline" size="sm" onClick={handleExport}>
<Download className="mr-2 h-4 w-4" />
Export
</Button>
<Input
id="import-file"
type="file"
accept=".xlsx,.xls"
onChange={handleImport}
className="hidden"
/>
<Button
variant="outline"
size="sm"
onClick={() => document.getElementById("import-file")?.click()}
>
<Upload className="mr-2 h-4 w-4" />
Import
</Button>
<Button variant="outline" size="sm" onClick={handleDownloadTemplate}>
<FileSpreadsheet className="mr-2 h-4 w-4" />
Download Template
</Button>
</div>
);
}
|