From c4c3f12b4a6d1a3c09b797e1a160747cac8761e9 Mon Sep 17 00:00:00 2001
From: rlaks5757 <rlaks5757@gmail.com>
Date: Fri, 28 Mar 2025 11:27:25 +0900
Subject: template file download 개발 완료
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 components/form-data/form-data-report-dialog.tsx | 139 ++++-------------------
 1 file changed, 21 insertions(+), 118 deletions(-)

(limited to 'components/form-data/form-data-report-dialog.tsx')

diff --git a/components/form-data/form-data-report-dialog.tsx b/components/form-data/form-data-report-dialog.tsx
index deb0873b..e28b4345 100644
--- a/components/form-data/form-data-report-dialog.tsx
+++ b/components/form-data/form-data-report-dialog.tsx
@@ -8,11 +8,10 @@ import React, {
   useEffect,
   useRef,
 } from "react";
-import { WebViewerInstance, Core } from "@pdftron/webviewer";
-import { useToast } from "@/hooks/use-toast";
-import prettyBytes from "pretty-bytes";
-import { X, Loader2 } from "lucide-react";
-import { Badge } from "@/components/ui/badge";
+import { WebViewerInstance } from "@pdftron/webviewer";
+import { Loader2 } from "lucide-react";
+import { saveAs } from "file-saver";
+import { toast } from "sonner";
 import {
   Dialog,
   DialogContent,
@@ -25,11 +24,11 @@ import { Label } from "@/components/ui/label";
 import {
   Select,
   SelectContent,
-  SelectGroup,
   SelectItem,
   SelectTrigger,
   SelectValue,
 } from "@/components/ui/select";
+
 import { Button } from "@/components/ui/button";
 import { getReportTempList } from "@/lib/forms/services";
 import { DataTableColumnJSON } from "./form-data-table-columns";
@@ -58,7 +57,9 @@ export const FormDataReportDialog: FC<FormDataReportDialogProps> = ({
   setReportData,
   packageId,
   formId,
+  formCode,
 }) => {
+  
   const [tempList, setTempList] = useState<tempFile[]>([]);
   const [selectTemp, setSelectTemp] = useState<string>("");
   const [instance, setInstance] = useState<null | WebViewerInstance>(null);
@@ -92,46 +93,9 @@ export const FormDataReportDialog: FC<FormDataReportDialogProps> = ({
         // },
       });
 
-      const blob = new Blob([fileData], {
-        type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
-      });
+      saveAs(new Blob([fileData]), fileName);      
 
-      const link = document.createElement("a");
-      link.href = URL.createObjectURL(blob);
-      link.download = fileName;
-      document.body.appendChild(link);
-      link.click();
-      document.body.removeChild(link);
-
-      // const allTabs = UI.TabManager.getAllTabs() as {
-      //   id: number;
-      //   src: Core.Document;
-      // }[];
-
-      // for (const tab of allTabs) {
-      //   // await UI.TabManager.setActiveTab(tab.id);
-      //   await activateTabAndWaitForLoad(instance, tab.id);
-      //   const tabDoc = tab.src;
-      //   const fileName = tabDoc.getFilename();
-
-      //   const fileData = await tabDoc.getFileData({
-      //     includeAnnotations: true,
-      //   });
-
-      //   console.log({ fileData });
-
-      //   const blob = new Blob([fileData], {
-      //     type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
-      //   });
-
-      //   // 다운로드
-      //   // const link = document.createElement("a");
-      //   // link.href = URL.createObjectURL(blob);
-      //   // link.download = fileName;
-      //   // document.body.appendChild(link);
-      //   // link.click();
-      //   // document.body.removeChild(link);
-      // }
+      toast.success("Report 다운로드 완료!");      
     }
   };
 
@@ -175,6 +139,7 @@ export const FormDataReportDialog: FC<FormDataReportDialogProps> = ({
             instance={instance}
             setInstance={setInstance}
             setFileLoading={setFileLoading}
+            formCode={formCode}
           />
         </div>
 
@@ -195,6 +160,7 @@ interface ReportWebViewerProps {
   instance: null | WebViewerInstance;
   setInstance: Dispatch<SetStateAction<WebViewerInstance | null>>;
   setFileLoading: Dispatch<SetStateAction<boolean>>;
+  formCode: string;
 }
 
 const ReportWebViewer: FC<ReportWebViewerProps> = ({
@@ -204,6 +170,7 @@ const ReportWebViewer: FC<ReportWebViewerProps> = ({
   instance,
   setInstance,
   setFileLoading,
+  formCode,
 }) => {
   const [viwerLoading, setViewerLoading] = useState<boolean>(true);
   const viewer = useRef<HTMLDivElement>(null);
@@ -234,12 +201,6 @@ const ReportWebViewer: FC<ReportWebViewerProps> = ({
               viewer.current as HTMLDivElement
             ).then(async (instance: WebViewerInstance) => {
               setInstance(instance);
-              // //Tab 메뉴 사용 필요시 활성화
-              // instance.UI.enableFeatures([instance.UI.Feature.MultiTab]);
-              // instance.UI.disableElements([
-              //   "addTabButton",
-              //   "multiTabsEmptyPage",
-              // ]);
               setViewerLoading(false);
             });
           });
@@ -262,9 +223,10 @@ const ReportWebViewer: FC<ReportWebViewerProps> = ({
       instance,
       reportDatas,
       reportTempPath,
-      setFileLoading
+      setFileLoading,
+      formCode
     );
-  }, [reportTempPath, reportDatas, instance, columnsJSON]);
+  }, [reportTempPath, reportDatas, instance, columnsJSON, formCode]);
 
   return (
     <div ref={viewer} className="h-[100%]">
@@ -319,7 +281,8 @@ type ImportReportData = (
   instance: null | WebViewerInstance,
   reportDatas: ReportData[],
   reportTempPath: string,
-  setFileLoading: Dispatch<SetStateAction<boolean>>
+  setFileLoading: Dispatch<SetStateAction<boolean>>,
+  formCode: string
 ) => void;
 
 const importReportData: ImportReportData = async (
@@ -327,7 +290,8 @@ const importReportData: ImportReportData = async (
   instance,
   reportDatas,
   reportTempPath,
-  setFileLoading
+  setFileLoading,
+  formCode
 ) => {
   setFileLoading(true);
   try {
@@ -352,12 +316,13 @@ const importReportData: ImportReportData = async (
       });
 
       const doc = await createDocument(reportFileBlob, {
+        filename: `${formCode}_report.docx`,
         extension: "docx",
       });
 
       await doc.applyTemplateValues(reportValueMapping);
 
-      documentViewer.loadDocument(doc, {
+      documentViewer.loadDocument(doc, {        
         extension: "docx",
         enableOfficeEditing: true,
         officeOptions: {
@@ -373,68 +338,6 @@ const importReportData: ImportReportData = async (
   }
 };
 
-const importReportDataTab: ImportReportData = async (
-  columnJSON,
-  instance,
-  reportDatas,
-  reportTempPath,
-  setFileLoading
-) => {
-  setFileLoading(true);
-  try {
-    if (instance && reportDatas.length > 0 && reportTempPath.length > 0) {
-      const { UI, Core } = instance;
-      const { createDocument } = Core;
-
-      const getFileData = await fetch(reportTempPath);
-      const reportFileBlob = await getFileData.blob();
-
-      const prevTab = UI.TabManager.getAllTabs();
-
-      (prevTab as object[] as { id: number }[]).forEach((c) => {
-        const { id } = c;
-        UI.TabManager.deleteTab(id);
-      });
-
-      const fileOptions = reportDatas.map((c) => {
-        const { tagNumber } = c;
-
-        const options = {
-          filename: `${tagNumber}_report.docx`,
-        };
-
-        return { options, reportData: c };
-      });
-
-      const tabIds = [];
-
-      for (const fileOption of fileOptions) {
-        let doc = await createDocument(reportFileBlob, {
-          ...fileOption.options,
-          extension: "docx",
-        });
-
-        await doc.applyTemplateValues(
-          stringifyAllValues(fileOption.reportData)
-        );
-
-        const tab = await UI.TabManager.addTab(doc, {
-          ...fileOption.options,
-        });
-
-        tabIds.push(tab); // 탭 ID 저장
-      }
-
-      if (tabIds.length > 0) {
-        await UI.TabManager.setActiveTab(tabIds[0]);
-      }
-    }
-  } catch (err) {
-  } finally {
-    setFileLoading(false);
-  }
-};
-
 type UpdateReportTempList = (
   packageId: number,
   formId: number,
-- 
cgit v1.2.3

