diff options
| author | joonhoekim <26rote@gmail.com> | 2025-11-03 18:46:35 +0900 |
|---|---|---|
| committer | joonhoekim <26rote@gmail.com> | 2025-11-03 18:46:35 +0900 |
| commit | 1393acc4b6675fd5eac65c6f1a9e399edfb2d44f (patch) | |
| tree | 6610385198545277ed51c4616d315aa0800c07bc /lib/swp/table/swp-table-columns.tsx | |
| parent | a9c038e51f1cf508165e9d196ffe332f6ac54d74 (diff) | |
(김준회) SWP: 커버페이지 생성 API 오류 수정
Diffstat (limited to 'lib/swp/table/swp-table-columns.tsx')
| -rw-r--r-- | lib/swp/table/swp-table-columns.tsx | 105 |
1 files changed, 85 insertions, 20 deletions
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<DocumentListItem>[] = [ { @@ -130,24 +132,87 @@ export const swpDocumentColumns: ColumnDef<DocumentListItem>[] = [ { id: "actions", header: "액션", - cell: ({ row }) => ( - <Button - variant="ghost" - size="sm" - onClick={(e) => { - e.stopPropagation(); // 행 클릭 이벤트 방지 - // 커버페이지 다운로드 핸들러는 부모 컴포넌트에서 제공 - const event = new CustomEvent('coverDownload', { - detail: { document: row.original } - }); - window.dispatchEvent(event); - }} - className="h-8 px-2" - > - <Download className="h-4 w-4 mr-1" /> - 커버페이지 - </Button> - ), - 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 ( + <Button + variant="ghost" + size="sm" + onClick={handleDownloadCover} + disabled={isDownloading} + className="h-8 w-8 p-0" + title="커버페이지 다운로드" + > + {isDownloading ? ( + <Loader2 className="h-4 w-4 animate-spin" /> + ) : ( + <Download className="h-4 w-4" /> + )} + </Button> + ); + }, + size: 80, }, ]; |
