@@ -4,8 +4,13 @@ import { serverFetchBlob, serverFetchJson } from "@/app/utils/fetchUtil"; | |||||
import { EX02ProjectCashFlowReportRequest } from "."; | import { EX02ProjectCashFlowReportRequest } from "."; | ||||
import { BASE_API_URL } from "@/config/api"; | import { BASE_API_URL } from "@/config/api"; | ||||
export interface FileResponse { | |||||
filename: string; | |||||
blobValue: Uint8Array; | |||||
} | |||||
export const fetchEX02ProjectCashFlowReport = async (data: EX02ProjectCashFlowReportRequest) => { | export const fetchEX02ProjectCashFlowReport = async (data: EX02ProjectCashFlowReportRequest) => { | ||||
const reportBlob = await serverFetchBlob( | |||||
const reportBlob = await serverFetchBlob<FileResponse>( | |||||
`${BASE_API_URL}/reports/EX02-ProjectCashFlowReport`, | `${BASE_API_URL}/reports/EX02-ProjectCashFlowReport`, | ||||
{ | { | ||||
method: "POST", | method: "POST", | ||||
@@ -14,5 +19,7 @@ export const fetchEX02ProjectCashFlowReport = async (data: EX02ProjectCashFlowRe | |||||
}, | }, | ||||
); | ); | ||||
console.log(reportBlob) | |||||
return reportBlob | return reportBlob | ||||
}; | }; |
@@ -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"); | const link = document.createElement("a"); | ||||
link.href = url; | link.href = url; | ||||
link.setAttribute("download", filename); | link.setAttribute("download", filename); | ||||
@@ -8,6 +8,7 @@ export const serverFetch: typeof fetch = async (input, init) => { | |||||
const session = await getServerSession<any, SessionWithTokens>(authOptions); | const session = await getServerSession<any, SessionWithTokens>(authOptions); | ||||
const accessToken = session?.accessToken; | const accessToken = session?.accessToken; | ||||
console.log(accessToken) | |||||
return fetch(input, { | return fetch(input, { | ||||
...init, | ...init, | ||||
headers: { | headers: { | ||||
@@ -56,15 +57,16 @@ export async function serverFetchWithNoContent(...args: FetchParams) { | |||||
} | } | ||||
} | } | ||||
export async function serverFetchBlob(...args: FetchParams) { | |||||
export async function serverFetchBlob<T>(...args: FetchParams) { | |||||
const response = await serverFetch(...args); | const response = await serverFetch(...args); | ||||
if (response.ok) { | if (response.ok) { | ||||
console.log(response) | 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 { | } else { | ||||
switch (response.status) { | switch (response.status) { | ||||
case 401: | case 401: | ||||
@@ -7,6 +7,7 @@ import { ProjectResult } from "@/app/api/projects"; | |||||
import { EX02ProjectCashFlowReportFilter } from "@/app/api/reports"; | import { EX02ProjectCashFlowReportFilter } from "@/app/api/reports"; | ||||
import { fetchEX02ProjectCashFlowReport } from "@/app/api/reports/actions"; | import { fetchEX02ProjectCashFlowReport } from "@/app/api/reports/actions"; | ||||
import { downloadFile } from "@/app/utils/commonUtil"; | import { downloadFile } from "@/app/utils/commonUtil"; | ||||
import { BASE_API_URL } from "@/config/api"; | |||||
interface Props { | interface Props { | ||||
projects: ProjectResult[]; | projects: ProjectResult[]; | ||||
@@ -21,7 +22,7 @@ const GenerateEX02ProjectCashFlowReport: React.FC<Props> = ({ projects }) => { | |||||
const searchCriteria: Criterion<SearchParamNames>[] = useMemo( | const searchCriteria: Criterion<SearchParamNames>[] = useMemo( | ||||
() => [ | () => [ | ||||
{ label: t("Project"), paramName: "project", type: "select", options: projectCombo}, | |||||
{ label: t("Project"), paramName: "project", type: "select", options: projectCombo }, | |||||
], | ], | ||||
[t], | [t], | ||||
); | ); | ||||
@@ -32,17 +33,10 @@ const GenerateEX02ProjectCashFlowReport: React.FC<Props> = ({ projects }) => { | |||||
criteria={searchCriteria} | criteria={searchCriteria} | ||||
onSearch={async (query) => { | onSearch={async (query) => { | ||||
const projectIndex = projectCombo.findIndex(project => project === query.project) | 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) { | 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(); | |||||
}} | }} | ||||
/> | /> | ||||
</> | </> | ||||