import ChevronIcon from "@Assets/Icons/chevron.svg"; import PlusIcon from "@Assets/Icons/plus.svg"; import Documents from "@Front/Api/LeCoffreApi/Notary/Documents/Documents"; import Module from "@Front/Config/Module"; import classNames from "classnames"; import { EDocumentStatus } from "le-coffre-resources/dist/Customer/Document"; import { Customer, Document } from "le-coffre-resources/dist/Notary"; import { OfficeFolder } from "le-coffre-resources/dist/Notary"; import Image from "next/image"; import Link from "next/link"; import React from "react"; import Button, { EButtonVariant } from "../Button"; import Confirm from "../Modal/Confirm"; import QuantityProgressBar from "../QuantityProgressBar"; import classes from "./classes.module.scss"; import DocumentList from "./DocumentList"; import UserFolderHeader from "./UserFolderHeader"; import { AnchorStatus } from "@Front/Components/Layouts/Folder/FolderInformation"; import Trash from "@Assets/Icons/trash.svg"; import Folders from "@Front/Api/LeCoffreApi/Notary/Folders/Folders"; type IProps = { customer: Customer; animationDelay?: number; folder: OfficeFolder; isArchived?: boolean; isOpened: boolean; onChange: (id: string) => void; anchorStatus: AnchorStatus; getFolderCallback: () => Promise; }; type IState = { isOpenDeletionModal: boolean; selectedDocumentToDelete: string; }; export default class UserFolder extends React.Component { static defaultProps = { animationDelay: 300, isArchived: false, }; public rootRefElement = React.createRef(); constructor(props: IProps) { super(props); this.state = { isOpenDeletionModal: false, selectedDocumentToDelete: "", }; this.closeDeletionModal = this.closeDeletionModal.bind(this); this.openDeletionModal = this.openDeletionModal.bind(this); this.changeUserFolder = this.changeUserFolder.bind(this); this.deleteAskedDocument = this.deleteAskedDocument.bind(this); } public override render(): JSX.Element { const documentsAsked: Document[] | null = [ ...(this.getDocumentsByStatus("ASKED") ?? []), ...(this.getDocumentsByStatus("REFUSED") ?? []), ]; const otherDocuments: Document[] | null = this.getValidatedAndPendindDocuments(); const redirectPath = Module.getInstance() .get() .modules.pages.Folder.pages.AskDocument.props.path.replace("[folderUid]", this.props.folder.uid ?? "") .replace("[customerUid]", this.props.customer.uid ?? ""); let documentAskedSubtitle = ""; if (documentsAsked && documentsAsked?.length > 0) { documentAskedSubtitle = "Un mail de demande de documents a été envoyé pour ces documents :"; } else { if (otherDocuments && otherDocuments.length === 0) { documentAskedSubtitle = "Vous n'avez pas encore demandé de documents"; } else { documentAskedSubtitle = "Aucun document en attente"; } } return (
)} )} ); } public override componentDidUpdate(prevProps: IProps): void { this.rootRefElement.current?.style.setProperty("--animation-delay", this.props.animationDelay!.toString().concat("ms")); } private async deleteAskedDocument() { try { await Documents.getInstance().delete(this.state.selectedDocumentToDelete); await this.props.getFolderCallback(); } catch (e) { console.error(e); } } private calculateDocumentsPercentageProgress(): number { if (!this.props.customer.documents) return 0; const documents = this.props.customer.documents.filter((document) => document.folder?.uid === this.props.folder.uid); const totalDocuments: number = documents.length; const numberDocumentsRefused: number = this.getDocumentsByStatus(EDocumentStatus.REFUSED)?.length || 0; const numberDocumentsAsked: number = (this.getDocumentsByStatus(EDocumentStatus.ASKED)?.length || 0) + (this.getDocumentsByStatus(EDocumentStatus.DEPOSITED)?.length || 0); const depositedDocuments: number = totalDocuments - numberDocumentsAsked - numberDocumentsRefused; const percentage = (depositedDocuments / totalDocuments) * 100; return isNaN(percentage) ? 0 : percentage; } private getDocumentsByStatus(status: string): Document[] | null { if (!this.props.customer.documents) return null; const filteredDocuments = this.props.customer.documents.filter( (document) => document.document_status === status && document.folder?.uid === this.props.folder.uid, ); return filteredDocuments; } // Get all documents Validated, pending private getValidatedAndPendindDocuments(): Document[] | null { const pendingDocuments = this.getDocumentsByStatus(EDocumentStatus.DEPOSITED); const validatedDocuments = this.getDocumentsByStatus(EDocumentStatus.VALIDATED); if (!pendingDocuments && !validatedDocuments) return null; pendingDocuments?.push(...validatedDocuments!); return pendingDocuments; } // TODO: REFACTO this because the other documents should only be correspond to other documents of the props folders // private getOtherDocuments(documentToExclude: Document[] | null): Document[] | null { // if (!this.props.customer.documents) return null; // if (!documentToExclude) return this.props.customer.documents; // return this.props.customer.documents.filter((document) => !documentToExclude.includes(document)); // } private changeUserFolder() { this.props.onChange(this.props.customer.uid!); } private openDeletionModal(uid?: string): void { if (!uid) return; this.setState({ isOpenDeletionModal: true, selectedDocumentToDelete: uid, }); } private closeDeletionModal(): void { this.setState({ isOpenDeletionModal: false, }); } private async removeCustomer(uid: string) { try { //use folder put to remove customer from folder const query = { q: { customers: "true", }, }; const folder = await Folders.getInstance().getByUid(this.props.folder.uid!, query); if (!folder.customers) return; const customers = folder.customers.filter((customer) => customer.uid !== uid); folder.customers = customers; await Folders.getInstance().put(this.props.folder.uid!, folder); //redirect on the same page await this.props.getFolderCallback(); } catch (e) { console.error(e); } } }