From 5f134a765acdbd2c301376c37b8219e7801837f1 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Tue, 9 May 2023 13:48:45 +0200 Subject: [PATCH] :sparkles: Creating document type working --- .../SuperAdmin/DocumentTypes/DocumentTypes.ts | 13 ++- .../Layouts/Folder/AskDocuments/index.tsx | 85 +++++++++++++------ 2 files changed, 67 insertions(+), 31 deletions(-) diff --git a/src/front/Api/LeCoffreApi/SuperAdmin/DocumentTypes/DocumentTypes.ts b/src/front/Api/LeCoffreApi/SuperAdmin/DocumentTypes/DocumentTypes.ts index 0cb86bd3..55746b8f 100644 --- a/src/front/Api/LeCoffreApi/SuperAdmin/DocumentTypes/DocumentTypes.ts +++ b/src/front/Api/LeCoffreApi/SuperAdmin/DocumentTypes/DocumentTypes.ts @@ -14,7 +14,14 @@ export interface IGetDocumentTypesparams { export type IPutDocumentTypesParams = { }; -export interface IPostDocumentTypesParams {} +export interface IPostDocumentTypesParams { + name: string; + public_description: string; + private_description: string; + office: { + uid: string; + }; +} @Service() export default class DocumentTypes extends BaseSuperAdmin { @@ -47,10 +54,10 @@ export default class DocumentTypes extends BaseSuperAdmin { /** * @description : Create a Document */ - public async post(body: any): Promise { + public async post(body: IPostDocumentTypesParams): Promise { const url = new URL(this.baseURl); try { - return await this.postRequest(url, body); + return await this.postRequest(url, body as any); } catch (err) { this.onError(err); return Promise.reject(err); diff --git a/src/front/Components/Layouts/Folder/AskDocuments/index.tsx b/src/front/Components/Layouts/Folder/AskDocuments/index.tsx index fac5849e..cb9af00c 100644 --- a/src/front/Components/Layouts/Folder/AskDocuments/index.tsx +++ b/src/front/Components/Layouts/Folder/AskDocuments/index.tsx @@ -19,6 +19,8 @@ import classes from "./classes.module.scss"; import DeedTypes from "@Front/Api/LeCoffreApi/SuperAdmin/DeedTypes/DeedTypes"; import Documents from "@Front/Api/LeCoffreApi/SuperAdmin/Documents/Documents"; import Module from "@Front/Config/Module"; +import DocumentTypes from "@Front/Api/LeCoffreApi/SuperAdmin/DocumentTypes/DocumentTypes"; +import { OfficeFolder } from "le-coffre-resources/dist/Customer"; type IProps = {}; type IPropsClass = IProps & { @@ -31,6 +33,7 @@ type IState = { documentName: string; visibleDescription: string; documentTypes: IOption[]; + folder: OfficeFolder | null; }; class AskDocumentsClass extends BasePage { @@ -42,6 +45,7 @@ class AskDocumentsClass extends BasePage { documentName: "", visibleDescription: "", documentTypes: [], + folder: null, }; this.onFormSubmit = this.onFormSubmit.bind(this); @@ -118,6 +122,10 @@ class AskDocumentsClass extends BasePage { } public override async componentDidMount(): Promise { + this.loadData(); + } + + private async loadData(){ try{ const folder = await Folders.getInstance().getByUid(this.props.folderUid, { q:{ @@ -125,37 +133,44 @@ class AskDocumentsClass extends BasePage { include: { deed_type: true } - } + }, + office: true } }); if(!folder) return; - - const documentTypes = await DeedTypes.getInstance().getByUid(folder.deed!.deed_type!.uid!, { - q: { - deed_type_has_document_types: { - include: { - document_type: true - } - } - } - }) - - if(!documentTypes) return; - - const documentTypesOptions: IOption[] = documentTypes.deed_type_has_document_types!.map((documentType) => { - return { - label: documentType.document_type!.name!, - value: documentType.document_type!.uid!, - }; - }); - this.setState({ - documentTypes: documentTypesOptions, + folder, + documentTypes: await this.getAvailableDocuments(folder), }); }catch(e){ console.error(e); } } + + private async getAvailableDocuments(folder: OfficeFolder): Promise{ + const documentTypes = await DeedTypes.getInstance().getByUid(folder.deed!.deed_type!.uid!, { + q: { + deed_type_has_document_types: { + include: { + document_type: true + } + } + } + }) + + if(!documentTypes) return []; + + const documentTypesOptions: IOption[] = documentTypes.deed_type_has_document_types!.map((documentType) => { + return { + label: documentType.document_type!.name!, + value: documentType.document_type!.uid!, + }; + }); + + return documentTypesOptions + + } + private canAddDocument() { if (this.state.documentName === "" || this.state.visibleDescription === "") { return false; @@ -163,12 +178,26 @@ class AskDocumentsClass extends BasePage { return true; } - private addDocument() { - this.setState({ - isCreateDocumentModalVisible: false, - documentName: "", - visibleDescription: "", - }); + private async addDocument() { + try{ + 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 + }) + + await this.loadData(); + this.setState({ + isCreateDocumentModalVisible: false, + documentName: "", + visibleDescription: "", + }); + }catch(e){ + console.error(e); + } } private onVisibleDescriptionChange(e: React.ChangeEvent) {