From 7cbfb1cff528e9ce0ef0318881a9a90c5e37e4f5 Mon Sep 17 00:00:00 2001 From: Vins Date: Tue, 20 Feb 2024 14:15:46 +0100 Subject: [PATCH] Finished --- src/front/Api/BaseApiService.ts | 18 ++++++---- .../LeCoffreApi/Notary/OfficeRib/OfficeRib.ts | 8 ++++- .../Layouts/Folder/ViewDocuments/index.tsx | 9 +++-- .../Layouts/Rib/classes.module.scss | 4 +-- src/front/Components/Layouts/Rib/index.tsx | 33 +++++++++++-------- 5 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/front/Api/BaseApiService.ts b/src/front/Api/BaseApiService.ts index 198141b2..bd4a0c59 100644 --- a/src/front/Api/BaseApiService.ts +++ b/src/front/Api/BaseApiService.ts @@ -7,6 +7,10 @@ export enum ContentType { FORM_DATA = "multipart/form-data;", PNG = "image/png", } + +export type IRef = { + response?: Response; +}; export default abstract class BaseApiService { private static baseUrl: string; protected readonly variables = FrontendVariables.getInstance(); @@ -39,13 +43,13 @@ export default abstract class BaseApiService { return JSON.stringify(body); } - protected async getRequest(url: URL, token?: string, contentType?: ContentType, filename?: string) { + protected async getRequest(url: URL, token?: string, contentType?: ContentType, ref?: IRef) { const request = async () => await fetch(url, { method: "GET", headers: this.buildHeaders(contentType ?? ContentType.JSON), }); - return this.sendRequest(request, filename); + return this.sendRequest(request, ref); } protected async postRequest(url: URL, body: { [key: string]: unknown } = {}, token?: string) { @@ -114,18 +118,18 @@ export default abstract class BaseApiService { return this.sendRequest(request); } - private async sendRequest(request: () => Promise, filename?: string): Promise { + private async sendRequest(request: () => Promise, ref?: IRef): Promise { const response = await request(); - return this.processResponse(response, request, filename); + + return this.processResponse(response, request, ref); } - protected async processResponse(response: Response, request: () => Promise, filename?: string): Promise { + protected async processResponse(response: Response, request: () => Promise, ref?: IRef): Promise { let responseContent: T; - + ref && (ref["response"] = response); if (response.ok) { // Check the Content-Type header to determine the response type const contentType = response.headers.get("Content-Type"); - if (contentType && !contentType.includes("application/json")) { responseContent = (await response.blob()) as T; } else { diff --git a/src/front/Api/LeCoffreApi/Notary/OfficeRib/OfficeRib.ts b/src/front/Api/LeCoffreApi/Notary/OfficeRib/OfficeRib.ts index 9390fddc..9b9432eb 100644 --- a/src/front/Api/LeCoffreApi/Notary/OfficeRib/OfficeRib.ts +++ b/src/front/Api/LeCoffreApi/Notary/OfficeRib/OfficeRib.ts @@ -1,3 +1,4 @@ +import { IRef } from "@Front/Api/BaseApiService"; import BaseNotary from "../BaseNotary"; // TODO Type get query params -> Where + inclue + orderby @@ -27,7 +28,12 @@ export default class OfficeRib extends BaseNotary { public async getRibStream() { const url = new URL(this.baseURl); try { - return await this.getRequest(url); + const ref: IRef = {}; + const blob = await this.getRequest(url, undefined, undefined, ref); + const contentDisposition = ref.response?.headers.get("Content-Disposition"); + const r = /attachment; filename="(.*)"/gim; + const fileName = r.exec(contentDisposition ?? "")?.[1] ?? ""; + return { blob, fileName }; } catch (err) { this.onError(err); return Promise.reject(err); diff --git a/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx b/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx index f1a209d1..b49f3482 100644 --- a/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx +++ b/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx @@ -195,6 +195,7 @@ class ViewDocumentsClass extends BasePage { private async getFilePreview(): Promise { try { const fileBlob: Blob = await Files.getInstance().download(this.state.selectedFile?.uid as string); + this.setState({ fileBlob, }); @@ -281,7 +282,9 @@ class ViewDocumentsClass extends BasePage { this.props.router.push( Module.getInstance() .get() - .modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", this.props.folderUid) + '?customerUid=' + this.state.document?.depositor?.uid, + .modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", this.props.folderUid) + + "?customerUid=" + + this.state.document?.depositor?.uid, ); } catch (e) { console.error(e); @@ -297,7 +300,9 @@ class ViewDocumentsClass extends BasePage { this.props.router.push( Module.getInstance() .get() - .modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", this.props.folderUid) + '?customerUid=' + this.state.document?.depositor?.uid, + .modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", this.props.folderUid) + + "?customerUid=" + + this.state.document?.depositor?.uid, ); } catch (e) { console.error(e); diff --git a/src/front/Components/Layouts/Rib/classes.module.scss b/src/front/Components/Layouts/Rib/classes.module.scss index 3848d40a..efb7ade1 100644 --- a/src/front/Components/Layouts/Rib/classes.module.scss +++ b/src/front/Components/Layouts/Rib/classes.module.scss @@ -15,7 +15,6 @@ display: flex; justify-content: space-between; align-items: center; - flex-direction: column; .arrow-container { cursor: pointer; @@ -28,6 +27,7 @@ .file-container { max-width: 1000px; + min-height: 600px; margin: auto; flex: 1; } @@ -36,8 +36,6 @@ .footer { width: fit-content; margin: auto; - display: flex; - gap: 10px; .ocr-container { margin-top: 42px; } diff --git a/src/front/Components/Layouts/Rib/index.tsx b/src/front/Components/Layouts/Rib/index.tsx index 529ebd0a..7f6d93cc 100644 --- a/src/front/Components/Layouts/Rib/index.tsx +++ b/src/front/Components/Layouts/Rib/index.tsx @@ -13,7 +13,7 @@ export default function Rib() { const [documentList, setDocumentList] = useState([]); const router = useRouter(); let { officeUid } = router.query; - const [fileUrl, setFileUrl] = useState(""); + const [fileBlob, setFileBlob] = useState(); const [fileName, setFileName] = useState(""); const [key, setKey] = useState(""); const [isRibModalOpen, setIsRibModalOpen] = useState(false); @@ -25,15 +25,13 @@ export default function Rib() { try { const blob = await OfficeRib.getInstance().getRibStream(); - const ribUrl = URL.createObjectURL(blob); - - setFileUrl(ribUrl); + setFileBlob(blob.blob); setKey(key); - setFileName(key); + setFileName(blob.fileName); } catch (error) { - setFileUrl(""); - setFileName(""); - setKey(""); + // setFileBlob(undefined); + // setFileName(""); + // setKey(""); } }, [officeUid]); @@ -42,10 +40,11 @@ export default function Rib() { }, [officeUid]); function downloadFile() { - if (!fileUrl) return; + if (!fileBlob) return; + const url = window.URL.createObjectURL(fileBlob); const a = document.createElement("a"); a.style.display = "none"; - a.href = fileUrl; + a.href = url; a.download = key; document.body.appendChild(a); a.click(); @@ -98,7 +97,7 @@ export default function Rib() { RIB de l'office - {!fileUrl && ( + {!fileBlob && (
@@ -113,10 +112,16 @@ export default function Rib() {
)} - {fileUrl && ( + {fileBlob && (
-
{fileUrl && }
-
+
+ {fileBlob && } +
+
+ )} + {fileBlob && ( +
+