From 529c26d114239b6e9ab810a72df99c7c551046f6 Mon Sep 17 00:00:00 2001 From: "cyril.tsui" Date: Mon, 29 Apr 2024 18:19:35 +0800 Subject: [PATCH] update gen EX02 report --- src/app/api/reports/actions.ts | 9 ++++++++- src/app/utils/commonUtil.ts | 12 ++---------- src/app/utils/fetchUtil.ts | 12 +++++++----- .../GenerateEX02ProjectCashFlowReport.tsx | 14 ++++---------- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/app/api/reports/actions.ts b/src/app/api/reports/actions.ts index e0db4c4..6a7e70c 100644 --- a/src/app/api/reports/actions.ts +++ b/src/app/api/reports/actions.ts @@ -4,8 +4,13 @@ import { serverFetchBlob, serverFetchJson } from "@/app/utils/fetchUtil"; import { EX02ProjectCashFlowReportRequest } from "."; import { BASE_API_URL } from "@/config/api"; +export interface FileResponse { + filename: string; + blobValue: Uint8Array; +} + export const fetchEX02ProjectCashFlowReport = async (data: EX02ProjectCashFlowReportRequest) => { - const reportBlob = await serverFetchBlob( + const reportBlob = await serverFetchBlob( `${BASE_API_URL}/reports/EX02-ProjectCashFlowReport`, { method: "POST", @@ -14,5 +19,7 @@ export const fetchEX02ProjectCashFlowReport = async (data: EX02ProjectCashFlowRe }, ); + console.log(reportBlob) + return reportBlob }; \ No newline at end of file diff --git a/src/app/utils/commonUtil.ts b/src/app/utils/commonUtil.ts index f0cc208..72d4a56 100644 --- a/src/app/utils/commonUtil.ts +++ b/src/app/utils/commonUtil.ts @@ -22,16 +22,8 @@ export const dateInRange = (currentDate: string, startDate: string, endDate: str } } -function s2ab(s: string) { - var buf = new ArrayBuffer(s.length); - var view = new Uint8Array(buf); - for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; - return buf; -} - -export const downloadFile = (blob: Blob | string, type: string, filename: string) => { - - const url = URL.createObjectURL(typeof blob === "string" ? new Blob([blob], { type: type }) : blob); +export const downloadFile = (blobData: Uint8Array, filename: string) => { + const url = URL.createObjectURL(new Blob([blobData])); const link = document.createElement("a"); link.href = url; link.setAttribute("download", filename); diff --git a/src/app/utils/fetchUtil.ts b/src/app/utils/fetchUtil.ts index fa11529..7e9ab5f 100644 --- a/src/app/utils/fetchUtil.ts +++ b/src/app/utils/fetchUtil.ts @@ -8,6 +8,7 @@ export const serverFetch: typeof fetch = async (input, init) => { const session = await getServerSession(authOptions); const accessToken = session?.accessToken; + console.log(accessToken) return fetch(input, { ...init, headers: { @@ -56,15 +57,16 @@ export async function serverFetchWithNoContent(...args: FetchParams) { } } -export async function serverFetchBlob(...args: FetchParams) { +export async function serverFetchBlob(...args: FetchParams) { const response = await serverFetch(...args); if (response.ok) { console.log(response) - const blob = await response.blob() - const blobText = await blob.text(); - const blobType = await blob.type; - return {filename: response.headers.get("filename"), blobText: blobText, blobType: blobType}; + // const blob = await response.blob() + // const blobText = await blob.text(); + // const blobType = await blob.type; + const blobValue = (await response.body?.getReader().read())!!.value!! + return {filename: response.headers.get("filename"), blobValue: blobValue} as T; } else { switch (response.status) { case 401: diff --git a/src/components/GenerateEX02ProjectCashFlowReport/GenerateEX02ProjectCashFlowReport.tsx b/src/components/GenerateEX02ProjectCashFlowReport/GenerateEX02ProjectCashFlowReport.tsx index d0bc75f..43b5e5d 100644 --- a/src/components/GenerateEX02ProjectCashFlowReport/GenerateEX02ProjectCashFlowReport.tsx +++ b/src/components/GenerateEX02ProjectCashFlowReport/GenerateEX02ProjectCashFlowReport.tsx @@ -7,6 +7,7 @@ import { ProjectResult } from "@/app/api/projects"; import { EX02ProjectCashFlowReportFilter } from "@/app/api/reports"; import { fetchEX02ProjectCashFlowReport } from "@/app/api/reports/actions"; import { downloadFile } from "@/app/utils/commonUtil"; +import { BASE_API_URL } from "@/config/api"; interface Props { projects: ProjectResult[]; @@ -21,7 +22,7 @@ const GenerateEX02ProjectCashFlowReport: React.FC = ({ projects }) => { const searchCriteria: Criterion[] = useMemo( () => [ - { label: t("Project"), paramName: "project", type: "select", options: projectCombo}, + { label: t("Project"), paramName: "project", type: "select", options: projectCombo }, ], [t], ); @@ -32,17 +33,10 @@ const GenerateEX02ProjectCashFlowReport: React.FC = ({ projects }) => { criteria={searchCriteria} onSearch={async (query) => { const projectIndex = projectCombo.findIndex(project => project === query.project) - const response = await fetchEX02ProjectCashFlowReport({projectId: projects[projectIndex].id}) - console.log(response) + const response = await fetchEX02ProjectCashFlowReport({ projectId: projects[projectIndex].id }) if (response) { - downloadFile(response.blobText, response.blobType, response.filename!!) + downloadFile(new Uint8Array(response.blobValue), response.filename!!) } - - // const url = URL.createObjectURL(response.blob); - // const link = document.createElement("a"); - // link.href = url; - // link.setAttribute("download", "abc.xlsx"); - // link.click(); }} />