From 2ce5f9dfbb69f0898c42ab862db5ad142fa24943 Mon Sep 17 00:00:00 2001 From: dujinkim Date: Tue, 14 Oct 2025 09:14:10 +0000 Subject: (최겸) 구매 입찰 1회성 품목 기준정보 개발(스키마, 테이블, CRUD, 페이지 등) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../procurement-items-table-toolbar-actions.tsx | 182 +++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 lib/procurement-items/table/procurement-items-table-toolbar-actions.tsx (limited to 'lib/procurement-items/table/procurement-items-table-toolbar-actions.tsx') diff --git a/lib/procurement-items/table/procurement-items-table-toolbar-actions.tsx b/lib/procurement-items/table/procurement-items-table-toolbar-actions.tsx new file mode 100644 index 00000000..f9bc8805 --- /dev/null +++ b/lib/procurement-items/table/procurement-items-table-toolbar-actions.tsx @@ -0,0 +1,182 @@ +"use client" + +import * as React from "react" +import { useRouter } from "next/navigation" +import { type Table } from "@tanstack/react-table" +import { Download, FileDown, Plus } from "lucide-react" +import * as ExcelJS from 'exceljs' +import { saveAs } from "file-saver" +import { toast } from "sonner" + +import { Button } from "@/components/ui/button" +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuTrigger, +} from "@/components/ui/dropdown-menu" + +import { ProcurementItem } from "@/db/schema/items" +import { DeleteProcurementItemsDialog } from "./delete-procurement-items-dialog" +import { AddProcurementItemDialog } from "./add-procurement-items-dialog" +import { exportProcurementItemTemplate } from "./procurement-items-excel-template" +import { ImportProcurementItemButton } from "./import-procurement-items-excel-button" + +interface ProcurementItemsTableToolbarActionsProps { + table: Table +} + +export function ProcurementItemsTableToolbarActions({ + table +}: ProcurementItemsTableToolbarActionsProps) { + const [isAddDialogOpen, setIsAddDialogOpen] = React.useState(false) + const router = useRouter() + + // 가져오기 성공 후 테이블 갱신 + const handleImportSuccess = () => { + router.refresh() + } + + // Excel 내보내기 함수 + const exportTableToExcel = async ( + table: Table, + options: { + filename: string; + excludeColumns?: string[]; + sheetName?: string; + } + ) => { + const { filename, excludeColumns = [], sheetName = "품목 목록" } = options; + + // 워크북 생성 + const workbook = new ExcelJS.Workbook(); + workbook.creator = 'Procurement Item Management System'; + workbook.created = new Date(); + + // 워크시트 생성 + const worksheet = workbook.addWorksheet(sheetName); + + // 테이블 데이터 가져오기 + const data = table.getFilteredRowModel().rows.map(row => row.original); + + // 테이블 헤더 가져오기 + const headers = table.getAllColumns() + .filter(column => !excludeColumns.includes(column.id)) + .map(column => ({ + key: column.id, + header: column.columnDef.meta?.excelHeader || column.id, + })); + + // 헤더 행 추가 + worksheet.addRow(headers.map(h => h.header)); + + // 데이터 행 추가 + data.forEach((item) => { + const rowData = headers.map(header => { + const value = item[header.key]; + + // 날짜 처리 + if (header.key === 'createdAt' || header.key === 'updatedAt') { + return value instanceof Date ? value.toLocaleDateString('ko-KR') : value; + } + + // 활성화 여부 처리 + if (header.key === 'isActive') { + return value === 'Y' ? '활성' : '비활성'; + } + + return value || ''; + }); + worksheet.addRow(rowData); + }); + + // 컬럼 너비 설정 + worksheet.columns = headers.map(header => ({ + key: header.key, + width: header.key === 'itemName' || header.key === 'specification' ? 30 : 15 + })); + + // 파일 저장 + const buffer = await workbook.xlsx.writeBuffer(); + const blob = new Blob([buffer], { + type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' + }); + saveAs(blob, `${filename}_${new Date().toISOString().split('T')[0]}.xlsx`); + }; + + // 테이블 내보내기 + const handleExportToExcel = () => { + exportTableToExcel(table, { + filename: 'procurement_items', + excludeColumns: ['select', 'actions'], + sheetName: '품목 목록' + }); + toast.success('엑셀 파일이 다운로드되었습니다.'); + }; + + // 템플릿 다운로드 + const handleDownloadTemplate = () => { + exportProcurementItemTemplate(); + toast.success('엑셀 템플릿이 다운로드되었습니다.'); + }; + + return ( + <> +
+ {/* 추가 버튼 */} + + + {/* 엑셀 내보내기 메뉴 */} + + + + + + + + 현재 목록 내보내기 + + + + 템플릿 다운로드 + + + + + {/* 엑셀 가져오기 버튼 */} + + + {/* 선택된 항목들 삭제 버튼 */} + {table.getFilteredSelectedRowModel().rows.length > 0 && ( + row.original)} + showTrigger={true} + onSuccess={() => { + table.toggleAllRowsSelected(false) + router.refresh() + }} + /> + )} +
+ + { + setIsAddDialogOpen(false) + router.refresh() + }} + /> + + ) +} -- cgit v1.2.3