diff --git a/src/front/Components/Layouts/Folder/AskDocuments/ParameterDocuments/classes.module.scss b/src/front/Components/Layouts/Folder/AskDocuments/ParameterDocuments/classes.module.scss new file mode 100644 index 00000000..4ed9ba68 --- /dev/null +++ b/src/front/Components/Layouts/Folder/AskDocuments/ParameterDocuments/classes.module.scss @@ -0,0 +1,11 @@ +.add-document-form-container { + display: flex; + flex-direction: column; + gap: 24px; + + .radiobox-container { + > :not(:last-child) { + margin-bottom: 16px; + } + } +} diff --git a/src/front/Components/Layouts/Folder/AskDocuments/ParameterDocuments/index.tsx b/src/front/Components/Layouts/Folder/AskDocuments/ParameterDocuments/index.tsx new file mode 100644 index 00000000..1425919d --- /dev/null +++ b/src/front/Components/Layouts/Folder/AskDocuments/ParameterDocuments/index.tsx @@ -0,0 +1,159 @@ +import Confirm from "@Front/Components/DesignSystem/Modal/Confirm"; +import classes from "./classes.module.scss"; +import TextField from "@Front/Components/DesignSystem/Form/TextField"; +import TextAreaField from "@Front/Components/DesignSystem/Form/TextareaField"; +import { ChangeEvent, useCallback, useEffect, useState } from "react"; +import DocumentTypes from "@Front/Api/LeCoffreApi/Notary/DocumentTypes/DocumentTypes"; +import { DocumentType, OfficeFolder } from "le-coffre-resources/dist/Notary"; +import Deeds from "@Front/Api/LeCoffreApi/Notary/Deeds/Deeds"; +import MultiSelect from "@Front/Components/DesignSystem/MultiSelect"; +import { IOption } from "@Front/Components/DesignSystem/Form/SelectField"; +import { MultiValue } from "react-select"; +import RadioBox from "@Front/Components/DesignSystem/RadioBox"; +import Typography, { ITypo } from "@Front/Components/DesignSystem/Typography"; + +type IProps = { + isCreateDocumentModalVisible: boolean; + closeModal: () => void; + folder: OfficeFolder; +}; + +export default function ParameterDocuments(props: IProps) { + const [visibleDescription, setVisibleDescription] = useState(""); + const [documentName, setDocumentName] = useState(""); + + const [addOrEditDocument, setAddOrEditDocument] = useState<"add" | "edit">("edit"); + + const [selectedDocuments, setSelectedDocuments] = useState([]); + const [formattedOptions, setFormattedOptions] = useState([]); + + const getAvailableDocuments = useCallback(async () => { + const documents = await DocumentTypes.getInstance().get({}); + + const formattedOptions: IOption[] = documents + .filter((document) => { + return !props.folder.deed?.document_types?.some((documentType) => documentType.uid === document.uid); + }) + .map((document) => { + return { + label: document.name, + value: document.uid, + }; + }); + setFormattedOptions(formattedOptions); + }, [props.folder.deed?.document_types]); + + const onVisibleDescriptionChange = (event: ChangeEvent) => { + setVisibleDescription(event.target.value); + }; + + const onDocumentNameChange = (event: ChangeEvent) => { + setDocumentName(event.target.value); + }; + + const onDocumentChangeHandler = useCallback((values: MultiValue) => { + setSelectedDocuments(values as IOption[]); + }, []); + + const handleClose = useCallback(() => { + setFormattedOptions([]); + setSelectedDocuments([]); + setAddOrEditDocument("edit"); + setVisibleDescription(""); + setDocumentName(""); + props.closeModal(); + }, [props]); + + const addDocument = useCallback(async () => { + if (addOrEditDocument === "add") { + try { + const documentType = await DocumentTypes.getInstance().post({ + name: documentName, + private_description: visibleDescription, + office: { + uid: props.folder.office!.uid!, + }, + public_description: visibleDescription, + }); + + const oldDocumentsType = props.folder.deed?.document_types!; + await Deeds.getInstance().put(props.folder.deed?.uid!, { + document_types: [...oldDocumentsType, documentType], + }); + + //await this.loadData(); + handleClose(); + } catch (e) { + console.error(e); + } + } else { + try { + const oldDocumentsType = props.folder.deed?.document_types!; + await Deeds.getInstance().put(props.folder.deed?.uid!, { + document_types: [ + ...oldDocumentsType, + ...selectedDocuments.map((document) => DocumentType.hydrate({ uid: document.value as string })), + ], + }); + + //await this.loadData(); + handleClose(); + } catch (e) { + console.error(e); + } + } + }, [addOrEditDocument, documentName, handleClose, props, selectedDocuments, visibleDescription]); + + const selectEditMode = () => { + setAddOrEditDocument("edit"); + }; + + const selectAddMode = () => { + setAddOrEditDocument("add"); + }; + + useEffect(() => { + getAvailableDocuments(); + }, [getAvailableDocuments, props.folder]); + + return ( + +
+
+ + Document existant + + + + Créer un document + +
+ {addOrEditDocument === "add" && ( + <> + + + + )} + {addOrEditDocument === "edit" && ( + + )} +
+
+ ); +} diff --git a/src/front/Components/Layouts/Folder/AskDocuments/classes.module.scss b/src/front/Components/Layouts/Folder/AskDocuments/classes.module.scss index 6afe823c..e52073a2 100644 --- a/src/front/Components/Layouts/Folder/AskDocuments/classes.module.scss +++ b/src/front/Components/Layouts/Folder/AskDocuments/classes.module.scss @@ -37,10 +37,4 @@ .buttons-container { margin-top: 32px; } - - .add-document-form-container { - display: flex; - flex-direction: column; - gap: 24px; - } } diff --git a/src/front/Components/Layouts/Folder/AskDocuments/index.tsx b/src/front/Components/Layouts/Folder/AskDocuments/index.tsx index c77eb4ba..1bfc9132 100644 --- a/src/front/Components/Layouts/Folder/AskDocuments/index.tsx +++ b/src/front/Components/Layouts/Folder/AskDocuments/index.tsx @@ -1,15 +1,10 @@ import PlusIcon from "@Assets/Icons/plus.svg"; -import Deeds from "@Front/Api/LeCoffreApi/Notary/Deeds/Deeds"; import Documents from "@Front/Api/LeCoffreApi/Notary/Documents/Documents"; -import DocumentTypes from "@Front/Api/LeCoffreApi/Notary/DocumentTypes/DocumentTypes"; import Folders from "@Front/Api/LeCoffreApi/Notary/Folders/Folders"; import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button"; import CheckBox from "@Front/Components/DesignSystem/CheckBox"; import Form from "@Front/Components/DesignSystem/Form"; import { IOption } from "@Front/Components/DesignSystem/Form/SelectField"; -import TextAreaField from "@Front/Components/DesignSystem/Form/TextareaField"; -import TextField from "@Front/Components/DesignSystem/Form/TextField"; -import Confirm from "@Front/Components/DesignSystem/Modal/Confirm"; import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography"; import BackArrow from "@Front/Components/Elements/BackArrow"; import DefaultNotaryDashboard from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard"; @@ -20,6 +15,7 @@ import React from "react"; import BasePage from "../../Base"; import classes from "./classes.module.scss"; +import ParameterDocuments from "./ParameterDocuments"; type IProps = {}; type IPropsClass = IProps & { @@ -29,8 +25,6 @@ type IPropsClass = IProps & { }; type IState = { isCreateDocumentModalVisible: boolean; - documentName: string; - visibleDescription: string; documentTypes: IOption[]; folder: OfficeFolder | null; }; @@ -41,8 +35,6 @@ class AskDocumentsClass extends BasePage { this.state = { isCreateDocumentModalVisible: false, - documentName: "", - visibleDescription: "", documentTypes: [], folder: null, }; @@ -50,11 +42,6 @@ class AskDocumentsClass extends BasePage { this.onFormSubmit = this.onFormSubmit.bind(this); this.closeModal = this.closeModal.bind(this); this.openModal = this.openModal.bind(this); - this.cancel = this.cancel.bind(this); - this.onVisibleDescriptionChange = this.onVisibleDescriptionChange.bind(this); - this.onDocumentNameChange = this.onDocumentNameChange.bind(this); - this.addDocument = this.addDocument.bind(this); - this.canAddDocument = this.canAddDocument.bind(this); } public override render(): JSX.Element { @@ -101,25 +88,14 @@ class AskDocumentsClass extends BasePage { - -
- - -
-
+ {this.state.folder && ( + + )} ); } @@ -128,6 +104,8 @@ class AskDocumentsClass extends BasePage { this.loadData(); } + private cancel() {} + private async loadData() { try { const folder = await Folders.getInstance().getByUid(this.props.folderUid, { @@ -186,72 +164,16 @@ class AskDocumentsClass extends BasePage { return documentTypesOptions; } - private canAddDocument() { - if (this.state.documentName === "" || this.state.visibleDescription === "") { - return false; - } - return true; - } - - private async addDocument() { - try { - const documentType = await DocumentTypes.getInstance().post({ - name: this.state.documentName, - private_description: this.state.visibleDescription, - office: { - uid: this.state.folder?.office!.uid!, - }, - public_description: this.state.visibleDescription, - }); - - const oldDocumentsType = this.state.folder?.deed?.document_types!; - await Deeds.getInstance().put(this.state.folder?.deed?.uid!, { - document_types: [...oldDocumentsType, documentType], - }); - - await this.loadData(); - this.setState({ - isCreateDocumentModalVisible: false, - documentName: "", - visibleDescription: "", - }); - } catch (e) { - console.error(e); - } - } - - private onVisibleDescriptionChange(e: React.ChangeEvent) { - this.setState({ - visibleDescription: e.target.value, - }); - } - - private onDocumentNameChange(e: React.ChangeEvent) { - this.setState({ - documentName: e.target.value, - }); - } - - private cancel() { - this.setState({ - visibleDescription: "", - documentName: "", - }); - } - private openModal() { this.setState({ isCreateDocumentModalVisible: true, - visibleDescription: "", - documentName: "", }); } private closeModal() { + this.loadData(); this.setState({ isCreateDocumentModalVisible: false, - visibleDescription: "", - documentName: "", }); }