"use client"; import Documents, { IGetDocumentsparams } from "@Front/Api/LeCoffreApi/Customer/Documents/Documents"; import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button"; import DepositDocument from "@Front/Components/DesignSystem/DepositDocument"; import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography"; import DefaultTemplate from "@Front/Components/LayoutTemplates/DefaultTemplate"; import Customer, { Document, DocumentType, Note, OfficeFolder } from "le-coffre-resources/dist/Customer"; import React, { useCallback, useEffect, useState } from "react"; import classes from "./classes.module.scss"; import { useRouter } from "next/router"; import JwtService, { ICustomerJwtPayload } from "@Front/Services/JwtService/JwtService"; import DepositOtherDocument from "@Front/Components/DesignSystem/DepositOtherDocument"; import Folders from "@Front/Api/LeCoffreApi/Customer/Folders/Folders"; import OfficeRib from "@Front/Api/LeCoffreApi/Customer/OfficeRib/OfficeRib"; type IProps = {}; export default function ClientDashboard(props: IProps) { const router = useRouter(); let { folderUid } = router.query; const [documents, setDocuments] = useState(null); const [customer, setCustomer] = useState(null); const [contact, setContact] = useState(null); const [folder, setFolder] = useState(null); const [note, setNote] = useState(null); const [isAddDocumentModalVisible, setIsAddDocumentModalVisible] = useState(false); const getDocuments = useCallback(async () => { let jwt: ICustomerJwtPayload | undefined; if (typeof document !== "undefined") { jwt = JwtService.getInstance().decodeCustomerJwt(); } const folder = await Folders.getInstance().getByUid(folderUid as string, { q: { office: true, customers: true, notes: { include: { customer: true, }, }, stakeholders: { include: { contact: true, office_role: true, }, }, }, }); //Loop through the folder stakeholders, if there is at least one stakeholder that role is "Collaborateur" set contact to this stakeholders.contact, else, take the first stakeholders of the list const contact = folder.stakeholders!.find((stakeholder) => stakeholder.office_role?.name === "Collaborateur")?.contact; setContact(contact ?? folder.stakeholders![0]!.contact); const actualCustomer = folder?.customers?.find((customer) => customer.contact?.email === jwt?.email); if (!actualCustomer) throw new Error("Customer not found"); const note = folder.notes?.find((note) => note.customer?.uid === actualCustomer.uid); if (!note) throw new Error("Note not found"); const query: IGetDocumentsparams = { where: { depositor: { uid: actualCustomer.uid }, folder_uid: folderUid as string }, include: { files: true, document_history: true, document_type: true, depositor: true, folder: { include: { customers: { include: { contact: true, }, }, }, }, }, }; const documentList = await Documents.getInstance().get(query); //const folder = await Folders.getInstance().getByUid(folderUid as string, { q: { office: true, customers: true } }); setFolder(folder); setDocuments(documentList); setCustomer(actualCustomer); setNote(note); }, [folderUid]); const onCloseModalAddDocument = useCallback(() => { setIsAddDocumentModalVisible(false); getDocuments(); }, [getDocuments]); const onOpenModalAddDocument = useCallback(() => { setIsAddDocumentModalVisible(true); }, []); const downloadFile = useCallback(async () => { if (!folder?.office?.uid) return; const blob = await OfficeRib.getInstance().getRibStream(folder.office.uid); const ribUrl = URL.createObjectURL(blob); if (!ribUrl) return; const a = document.createElement("a"); a.style.display = "none"; a.href = ribUrl; a.download = ""; document.body.appendChild(a); a.click(); }, [folder]); useEffect(() => { getDocuments(); }, [folderUid, getDocuments]); const renderHeader = useCallback(() => { return (
{/* TODO Get name from userStore */}
Bonjour {customer?.contact?.first_name.concat(" ", customer?.contact?.last_name)}
Dossier {folder?.folder_number} - {folder?.name} {folder?.office?.name} Documents à envoyer Votre notaire est dans l'attente de documents pour valider votre dossier. Voici la liste des documents.
Veuillez glisser / déposer chaque document dans la zone prévue à cet effet ou cliquez sur la zone puis sélectionnez le document correspondant.
En déposant un document, celui-ci est automatiquement enregistré et transmis à votre notaire.
{note?.content}

{contact?.first_name} {contact?.last_name}

{contact?.phone_number ?? contact?.cell_phone_number}

{contact?.email}

{folder?.office?.rib_name && ( //Div to avoid the button to be on the same line as the text )}
); }, [ customer?.contact?.first_name, customer?.contact?.last_name, downloadFile, folder?.folder_number, folder?.name, folder?.office?.name, folder?.office?.rib_name, ]); const renderBox = useCallback(() => { return ( ({ document_type: DocumentType.hydrate({ name: "Autres documents", }), })} /> ); }, [customer, folderUid, isAddDocumentModalVisible, onCloseModalAddDocument]); return (
{renderHeader()}
{documents?.map((document) => ( ))}
Documents supplémentaires (facultatif) Vous souhaitez envoyer d'autres documents à votre notaire ?
{isAddDocumentModalVisible && renderBox()}
); }