import DepositDocumentIcon from "@Assets/Icons/deposit-document.svg"; import PlusIcon from "@Assets/Icons/plus.svg"; import CrossIcon from "@Assets/Icons/cross.svg"; import DocumentCheckIcon from "@Assets/Icons/document-check.svg"; import Image from "next/image"; import React from "react"; import Button, { EButtonVariant } from "../Button"; import Tooltip from "../ToolTip"; import Typography, { ITypo, ITypoColor } from "../Typography"; import classes from "./classes.module.scss"; import { File as FileCustomer } from "le-coffre-resources/dist/Customer"; type IProps = { title: string; dateAsked: Date; defaultFiles?: FileCustomer[]; onChange?: (files: File[]) => void; }; type IFile = { index: number; file: File; }; type IState = { files: IFile[]; isDragOver: boolean; }; export default class DepositDocument extends React.Component { private inputRef = React.createRef(); private index = 0; public constructor(props: IProps) { super(props); this.state = { files: [], isDragOver: false, }; 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); } public override render(): JSX.Element { return (
Deposit document
{this.props.title} Demandé par le notaire le {this.formatDate(this.props.dateAsked)}
{this.state.files.length > 0 && (
{this.state.files.map((file) => { const fileObj = file.file; return (
Document check {this.shortName(fileObj.name)}
Cross icon
); })}
)}
); } public override componentDidMount(): void { if (this.props.defaultFiles) { this.setState({ files: this.props.defaultFiles.map((file) => ({ index: this.index++, file: new File([""], file.file_path, {}), })), }); } } 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 addFile(file: File) { this.setState({ files: [ ...this.state.files, { index: this.index++, file: file, }, ], }); if (this.props.onChange) this.props.onChange(this.state.files.map((file) => file.file)); } private removeFile(e: any) { const image = e.target as HTMLElement; const indexToRemove = image.getAttribute("data-file"); if (!indexToRemove) return; this.setState({ files: this.state.files.filter((file) => file.index !== parseInt(indexToRemove)), }); if (this.props.onChange) this.props.onChange(this.state.files.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) { return date.toLocaleDateString("fr-FR"); } }