import DepositDocumentIcon from "@Assets/Icons/deposit-document.svg"; import CrossIcon from "@Assets/Icons/cross.svg"; import DocumentCheckIcon from "@Assets/Icons/document-check.svg"; import PlusIcon from "@Assets/Icons/plus.svg"; import Image from "next/image"; import React from "react"; import Typography, { ETypo, ETypoColor } from "../Typography"; import classes from "./classes.module.scss"; import { Document } from "le-coffre-resources/dist/Customer"; import { EDocumentStatus } from "le-coffre-resources/dist/Customer/Document"; import classNames from "classnames"; import Button, { EButtonStyleType, EButtonVariant } from "../Button"; import Confirm from "../Modal/Confirm"; import Documents from "@Front/Api/LeCoffreApi/Customer/Documents/Documents"; import Files from "@Front/Api/LeCoffreApi/Customer/Files/Files"; import Alert from "../Modal/Alert"; type IProps = { onChange?: (files: File[]) => void; open: boolean; onClose?: () => void; document: Document; customer_uid: string; folder_uid: string | string[]; }; type IFile = { index: number; file: File; fileName: string; }; type IState = { files: IFile[]; isDragOver: boolean; currentFiles?: IFile[]; refusedReason?: string; isShowRefusedReasonModalVisible: boolean; showFailedUploaded: string | null; showFailedDocument: string | null; isAddDocumentModalVisible: boolean; isLoading: boolean; }; export default class DepositOtherDocument extends React.Component { private inputRef = React.createRef(); private index = 0; public constructor(props: IProps) { super(props); this.state = { isAddDocumentModalVisible: this.props.open, files: [], isDragOver: false, refusedReason: "", isShowRefusedReasonModalVisible: false, showFailedUploaded: null, isLoading: false, showFailedDocument: null, }; this.addDocument = this.addDocument.bind(this); this.onFileChange = this.onFileChange.bind(this); this.removeFile = this.removeFile.bind(this); this.onDragOver = this.onDragOver.bind(this); this.onDragDrop = this.onDragDrop.bind(this); this.onDragLeave = this.onDragLeave.bind(this); this.onAccept = this.onAccept.bind(this); this.onCloseAlertUpload = this.onCloseAlertUpload.bind(this); } public override render(): JSX.Element { return (
Vous souhaitez envoyer un autre document à votre notaire ? Glissez / Déposez votre document dans la zone prévue à cet effet ou cliquez sur la zone puis sélectionnez le document correspondant.
Deposit document
{this.props.document.document_type?.name}
Sélectionnez des documents .jpg, .pdf ou .png
{this.state.currentFiles && this.state.currentFiles.length > 0 && (
{this.state.currentFiles.map((file) => { const fileObj = file.file; return (
Document check {this.shortName(fileObj.name)}
Cross icon
); })}
)}
{this.state.showFailedDocument && (
{this.state.showFailedDocument}
)} {this.state.showFailedUploaded && (
{this.state.showFailedUploaded}
)}
); } public override componentDidMount(): void {} private onCloseAlertUpload() { this.setState({ showFailedUploaded: null }); } private async onAccept() { this.setState({ isLoading: true, }); const filesArray = this.state.currentFiles; if (!filesArray) return; let documentCreated: Document = {} as Document; try { documentCreated = await Documents.getInstance().post({ folder: { uid: this.props.folder_uid, }, depositor: { uid: this.props.customer_uid, }, }); } catch (e) { this.setState({ showFailedDocument: "Le dossier est vérifié aucune modification n'est acceptée", isLoading: false }); return; } for (let i = 0; i < filesArray.length; i++) { const formData = new FormData(); formData.append("file", filesArray[i]!.file, filesArray[i]!.fileName); const query = JSON.stringify({ document: { uid: documentCreated.uid } }); formData.append("q", query); try { await Files.getInstance().post(formData); } catch (e) { this.setState({ showFailedUploaded: "Le fichier ne correspond pas aux critères demandés", isLoading: false }); return; } } this.setState({ isLoading: false, }); this.props.onClose!(); } private shortName(name: string): string { const maxLength = 20; if (name.length > maxLength) { return name.substring(0, maxLength / 2) + "..." + name.substring(name.length - maxLength / 2, name.length); } return name; } private onDragOver(event: React.DragEvent) { if (!this.state.isDragOver) { this.setState({ isDragOver: true, }); } event.preventDefault(); } private onDragLeave(event: React.DragEvent) { this.setState({ isDragOver: false, }); event.preventDefault(); } private async onDragDrop(event: React.DragEvent) { event.preventDefault(); this.setState({ isDragOver: false, }); const file = event.dataTransfer.files[0]; if (file) this.addFile(file); } private async addFile(file: File) { const iFile: IFile = { file: file, fileName: file.name, index: this.index++, }; const tmpArray: IFile[] = this.state.currentFiles || []; tmpArray.push(iFile); this.setState({ currentFiles: tmpArray, }); // const formData = new FormData(); // formData.append("file", file, file.name); // const query = JSON.stringify({ document: { uid: this.props.document.uid } }); // formData.append("q", query); // const newFile = await Files.getInstance().post(formData); // const newFile: FileCustomer = { // file_name: file.name, // } // const files = this.state.currentFiles ? [...this.state.currentFiles, newFile] : [newFile]; // this.setState({ // currentFiles: files, // loading: false, // files: [ // ...this.state.files, // { // index: this.index++, // file: file, // uid: newFile.uid!, // archived: null, // fileName: newFile?.file_name ?? "", // }, // ], // }); // if (this.props.onChange) this.props.onChange(this.state.files.map((file) => file.file)); } private async removeFile(e: any) { const image = e.target as HTMLElement; const indexToRemove = image.getAttribute("data-file"); if (!indexToRemove) return; const file = this.state.currentFiles!.find((file) => file.index === parseInt(indexToRemove)); if (!file) return; this.setState({ currentFiles: this.state.currentFiles!.filter((file) => file.index !== parseInt(indexToRemove)), }); if (this.props.onChange) this.props.onChange(this.state.currentFiles!.map((file) => file.file)); } private async onFileChange() { if (!this.inputRef.current) return; const files = this.inputRef.current.files; if (!files) { return; } const file = files[0]; if (file) this.addFile(file); } private addDocument() { if (!this.inputRef.current) return; this.inputRef.current.click(); } // private formatDate(date: Date) { // const dateToConvert = new Date(date); // return dateToConvert.toLocaleDateString("fr-FR"); // } }