From 1393acc4b6675fd5eac65c6f1a9e399edfb2d44f Mon Sep 17 00:00:00 2001 From: joonhoekim <26rote@gmail.com> Date: Mon, 3 Nov 2025 18:46:35 +0900 Subject: (김준회) SWP: 커버페이지 생성 API 오류 수정 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/swp/table/swp-table-columns.tsx | 105 +++++++++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 20 deletions(-) (limited to 'lib/swp/table/swp-table-columns.tsx') diff --git a/lib/swp/table/swp-table-columns.tsx b/lib/swp/table/swp-table-columns.tsx index 5334bd8c..9fb85d2a 100644 --- a/lib/swp/table/swp-table-columns.tsx +++ b/lib/swp/table/swp-table-columns.tsx @@ -1,10 +1,12 @@ "use client"; +import React from "react"; import { ColumnDef } from "@tanstack/react-table"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; -import { Download } from "lucide-react"; +import { Download, Loader2 } from "lucide-react"; import type { DocumentListItem } from "@/lib/swp/document-service"; +import { toast } from "sonner"; export const swpDocumentColumns: ColumnDef[] = [ { @@ -130,24 +132,87 @@ export const swpDocumentColumns: ColumnDef[] = [ { id: "actions", header: "액션", - cell: ({ row }) => ( - - ), - size: 120, + cell: function ActionCell({ row }) { + const [isDownloading, setIsDownloading] = React.useState(false); + + const handleDownloadCover = async (e: React.MouseEvent) => { + e.stopPropagation(); // 행 클릭 이벤트 방지 + + const docNumber = row.original.DOC_NO; + const projectCode = row.original.PROJ_NO; + + if (!docNumber || !projectCode) { + toast.error("문서 번호 또는 프로젝트 정보가 없습니다."); + return; + } + + setIsDownloading(true); + + try { + // 1. 프로젝트 코드로 프로젝트 ID 조회 + const projectIdResponse = await fetch( + `/api/projects/code-to-id?code=${encodeURIComponent(projectCode)}` + ); + + if (!projectIdResponse.ok) { + toast.error("프로젝트 정보를 찾을 수 없습니다."); + return; + } + + const { projectId } = await projectIdResponse.json(); + + // 2. 커버페이지 다운로드 API 호출 + const response = await fetch( + `/api/projects/${projectId}/cover?docNumber=${encodeURIComponent(docNumber)}` + ); + + if (!response.ok) { + const error = await response.json(); + throw new Error(error.message || "커버페이지 다운로드 실패"); + } + + // 3. 파일 다운로드 + const blob = await response.blob(); + const url = window.URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = `${docNumber}_cover.docx`; + document.body.appendChild(a); + a.click(); + window.URL.revokeObjectURL(url); + document.body.removeChild(a); + + toast.success("커버페이지 다운로드가 시작되었습니다."); + + } catch (error) { + console.error("커버페이지 다운로드 오류:", error); + toast.error( + error instanceof Error + ? error.message + : "커버페이지 다운로드 중 오류가 발생했습니다." + ); + } finally { + setIsDownloading(false); + } + }; + + return ( + + ); + }, + size: 80, }, ]; -- cgit v1.2.3