From 3c0bccf397c24de05a506120954515b900fd6aa5 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Mon, 24 Jul 2023 16:40:38 +0200 Subject: [PATCH] :sparkles: Create document type working --- .../Admin/DocumentTypes/DocumentTypes.ts | 2 +- .../DocumentTypesCreate/index.tsx | 28 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/front/Api/LeCoffreApi/Admin/DocumentTypes/DocumentTypes.ts b/src/front/Api/LeCoffreApi/Admin/DocumentTypes/DocumentTypes.ts index b5237f87..05003ce4 100644 --- a/src/front/Api/LeCoffreApi/Admin/DocumentTypes/DocumentTypes.ts +++ b/src/front/Api/LeCoffreApi/Admin/DocumentTypes/DocumentTypes.ts @@ -52,7 +52,7 @@ export default class DocumentTypes extends BaseAdmin { /** * @description : Create a Document */ - public async post(body: IPostDocumentTypesParams): Promise { + public async post(body: DocumentType): Promise { const url = new URL(this.baseURl); try { return await this.postRequest(url, body as any); diff --git a/src/front/Components/Layouts/DocumentTypes/DocumentTypesCreate/index.tsx b/src/front/Components/Layouts/DocumentTypes/DocumentTypesCreate/index.tsx index 8254c0ca..47668a92 100644 --- a/src/front/Components/Layouts/DocumentTypes/DocumentTypesCreate/index.tsx +++ b/src/front/Components/Layouts/DocumentTypes/DocumentTypesCreate/index.tsx @@ -1,16 +1,42 @@ +import DocumentTypes from "@Front/Api/LeCoffreApi/Admin/DocumentTypes/DocumentTypes"; import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button"; import Form from "@Front/Components/DesignSystem/Form"; import TextAreaField from "@Front/Components/DesignSystem/Form/TextareaField"; import TextField from "@Front/Components/DesignSystem/Form/TextField"; import Typography, { ITypo } from "@Front/Components/DesignSystem/Typography"; import DefaultDocumentTypesDashboard from "@Front/Components/LayoutTemplates/DefaultDocumentTypesDashboard"; +import Module from "@Front/Config/Module"; +import { validateOrReject } from "class-validator"; +import { DocumentType, Office } from "le-coffre-resources/dist/Admin"; +import { useRouter } from "next/router"; import { useCallback } from "react"; import classes from "./classes.module.scss"; type IProps = {}; export default function DocumentTypesCreate(props: IProps) { - const onSubmitHandler = useCallback(async (e: React.FormEvent | null, values: { [key: string]: string }) => {}, []); + const router = useRouter(); + const onSubmitHandler = useCallback( + async (e: React.FormEvent | null, values: { [key: string]: string }) => { + try { + const documentToCreate = DocumentType.hydrate({ + ...values, + office: Office.hydrate({ + uid: "6981326f-8a0a-4437-b15c-4cd5c4d80f6e", + }), + }); + await validateOrReject(documentToCreate, { groups: ["createDocumentType"] }); + const documentTypeCreated = await DocumentTypes.getInstance().post(documentToCreate); + + router.push( + Module.getInstance().get().modules.pages.DocumentTypes.pages.Edit.props.path.replace("[uid]", documentTypeCreated.uid!), + ); + } catch (e) { + console.log(e); + } + }, + [router], + ); return (