import LeftArrowIcon from "@Assets/Icons/left-arrow.svg"; import RightArrowIcon from "@Assets/Icons/right-arrow.svg"; import Documents from "@Front/Api/LeCoffreApi/Notary/Documents/Documents"; import Button, { EButtonStyleType, EButtonVariant } from "@Front/Components/DesignSystem/Button"; import FilePreview from "@Front/Components/DesignSystem/FilePreview"; import Confirm from "@Front/Components/DesignSystem/OldModal/Confirm"; import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Typography"; import DefaultNotaryDashboard from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard"; import Module from "@Front/Config/Module"; import { Document, File } from "le-coffre-resources/dist/Notary"; import { EDocumentStatus } from "le-coffre-resources/dist/Notary/Document"; import Image from "next/image"; import { NextRouter, useRouter } from "next/router"; import React from "react"; import BasePage from "../../Base"; import classes from "./classes.module.scss"; import Files from "@Front/Api/LeCoffreApi/Notary/Files/Files"; import TextAreaField from "@Front/Components/DesignSystem/Form/TextareaField"; import MessageBox from "@Front/Components/Elements/MessageBox"; type IProps = {}; type IPropsClass = { documentUid: string; router: NextRouter; folderUid: string; }; type IState = { isRefuseModalVisible: boolean; isValidateModalVisible: boolean; refuseText: string; selectedFileIndex: number; selectedFile: File | null; validatedPercentage: number; document: Document | null; fileBlob: Blob | null; isLoading: boolean; }; class ViewDocumentsClass extends BasePage { public constructor(props: IPropsClass) { super(props); this.state = { isValidateModalVisible: false, isRefuseModalVisible: false, refuseText: "", selectedFileIndex: 0, selectedFile: null, validatedPercentage: this.getRandomPercentageForOcr(), document: null, fileBlob: null, isLoading: true, }; this.closeModals = this.closeModals.bind(this); this.openValidateModal = this.openValidateModal.bind(this); this.openRefuseModal = this.openRefuseModal.bind(this); this.onRefuseTextChange = this.onRefuseTextChange.bind(this); this.validateDocument = this.validateDocument.bind(this); this.goToNext = this.goToNext.bind(this); this.goToPrevious = this.goToPrevious.bind(this); this.hasPrevious = this.hasPrevious.bind(this); this.hasNext = this.hasNext.bind(this); this.downloadFile = this.downloadFile.bind(this); this.refuseDocument = this.refuseDocument.bind(this); } public override render(): JSX.Element | null { return ( {this.state.document && this.state.document.files && this.state.selectedFile && !this.state.isLoading && (
{this.state.document.folder?.name} {this.state.document.document_type?.name}
{this.state.document.files.length > 1 && (
left arrow
)}
{this.state.document.files.length > 1 && (
right arrow
)}
Veuillez valider le document afin de pouvoir le télécharger.
{/* {this.state.document?.document_type?.name === "Document d'identité" && (
)} */}
{this.state.document?.document_status === EDocumentStatus.DEPOSITED && ( <> )} {this.state.document?.document_status === "VALIDATED" && this.state.fileBlob && ( )}
Êtes-vous sûr de vouloir valider ce document ?
Veuillez indiquer au client le motif du refus de son document afin qu'il puisse vous renvoyer une bonne version.
)} {(!this.state.selectedFile || !this.state.document) && !this.state.isLoading && (
Document non trouvé
)}
); } override async componentDidMount() { try { const document = await Documents.getInstance().getByUid(this.props.documentUid, { files: { where: { archived_at: null }, }, document_type: true, folder: true, depositor: true, }); this.setState( { document, selectedFileIndex: 0, selectedFile: document.files![0]!, isLoading: false, }, () => { this.getFilePreview(); }, ); } catch (e) { this.setState({ isLoading: false, }); console.error(e); } } private async getFilePreview(): Promise { try { const fileBlob: Blob = await Files.getInstance().download(this.state.selectedFile?.uid as string); this.setState({ fileBlob, }); } catch (e) { console.error(e); } } private downloadFile() { if (!this.state.fileBlob) return; const url = window.URL.createObjectURL(this.state.fileBlob); const a = document.createElement("a"); a.style.display = "none"; a.href = url; // the filename you want a.download = this.state.selectedFile?.file_name as string; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(url); } private getRandomPercentageForOcr() { // find diff let difference = 100 - 90; // generate random number let rand = Math.random(); // multiply with difference rand = Math.floor(rand * difference); // add with min value rand = rand + 90; return rand; } private goToPrevious() { const index = this.state.selectedFileIndex - 1; if (this.hasPrevious()) { this.setState( { selectedFile: this.state.document!.files![index]!, selectedFileIndex: index, fileBlob: null, }, () => { this.getFilePreview(); }, ); } } private goToNext() { if (this.hasNext()) { const index = this.state.selectedFileIndex + 1; this.setState( { selectedFile: this.state.document!.files![index]!, selectedFileIndex: index, fileBlob: null, }, () => { this.getFilePreview(); }, ); } } private hasPrevious() { const index = this.state.selectedFileIndex - 1; return index >= 0; } private hasNext() { const index = this.state.selectedFileIndex + 1; return index < this.state.document!.files!.length; } private async refuseDocument() { try { await Documents.getInstance().refuse(this.props.documentUid, this.state.refuseText); 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, ); } catch (e) { console.error(e); } } private async validateDocument() { try { await Documents.getInstance().put(this.props.documentUid, { document_status: EDocumentStatus.VALIDATED, }); 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, ); } catch (e) { console.error(e); } } private onRefuseTextChange(e: React.ChangeEvent) { this.setState({ refuseText: e.target.value, }); } private openValidateModal() { this.setState({ isValidateModalVisible: true, }); } private openRefuseModal() { this.setState({ isRefuseModalVisible: true, }); } private closeModals() { this.setState({ isValidateModalVisible: false, isRefuseModalVisible: false, }); } } export default function ViewDocuments(props: IProps) { const router = useRouter(); let { folderUid, documentUid } = router.query; documentUid = documentUid as string; folderUid = folderUid as string; return ; }