diff --git a/src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts b/src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts index cc188375..b749519e 100644 --- a/src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts +++ b/src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts @@ -1,6 +1,7 @@ -import User, { Customer, DeedType, Office, OfficeFolder } from "le-coffre-resources/dist/Notary"; +import { type OfficeFolder } from "le-coffre-resources/dist/Notary"; + import BaseSuperAdmin from "../BaseSuperAdmin"; -import { EFolderStatus } from "le-coffre-resources/dist/Customer/OfficeFolder"; +import EFolderStatus from "le-coffre-resources/dist/Customer/EFolderStatus"; // TODO Type get query params -> Where + inclue + orderby export interface IGetFoldersParams { @@ -11,36 +12,6 @@ export interface IGetFoldersParams { }; } -export interface IPostFoldersParams { - folder_number: OfficeFolder["folder_number"]; - name: OfficeFolder["name"]; - description: OfficeFolder["description"]; - deed: { - deed_type: { - uid: DeedType["uid"]; - }; - }; - office: { - uid: Office["uid"]; - }; - office_folder_has_stakeholder: { - user_stakeholder: { - uid: User["uid"]; - }; - }[]; -} - -export type IPutFoldersParams = { - uid?: OfficeFolder["uid"]; - folder_number?: OfficeFolder["folder_number"]; - name?: OfficeFolder["name"]; - description?: OfficeFolder["description"]; - archived_description?: OfficeFolder["archived_description"]; - status?: OfficeFolder["status"]; - office_folder_has_stakeholder?: OfficeFolder["office_folder_has_stakeholder"]; - office_folder_has_customers?: { customer: { uid: Customer["uid"] } }[]; -}; - export default class Folders extends BaseSuperAdmin { private static instance: Folders; private readonly baseURl = this.namespaceUrl.concat("/folders"); @@ -88,10 +59,10 @@ export default class Folders extends BaseSuperAdmin { /** * @description : Create a folder */ - public async post(body: any): Promise { + public async post(officeFolder: Partial): Promise { const url = new URL(this.baseURl); try { - return await this.postRequest(url, body); + return await this.postRequest(url, officeFolder); } catch (err) { this.onError(err); return Promise.reject(err); @@ -101,10 +72,10 @@ export default class Folders extends BaseSuperAdmin { /** * @description : Update the folder description */ - public async put(uid: string, body: IPutFoldersParams): Promise { + public async put(uid: string, body: Partial): Promise { const url = new URL(this.baseURl.concat(`/${uid}`)); try { - return await this.putRequest(url, body); + return await this.putRequest(url, body); } catch (err) { this.onError(err); return Promise.reject(err); @@ -119,14 +90,14 @@ export default class Folders extends BaseSuperAdmin { const targetedFolder = await this.getByUid(uid); if (targetedFolder.office_folder_has_customers) return Promise.reject(`The folder ${uid} contains customers`); try { - return await this.deleteRequest(url); + return await this.deleteRequest(url); } catch (err) { this.onError(err); return Promise.reject(err); } } - public async archive(uid: string, body: IPutFoldersParams): Promise { + public async archive(uid: string, body: Partial): Promise { body.status = EFolderStatus.ARCHIVED; try { return await this.put(uid, body); @@ -136,7 +107,7 @@ export default class Folders extends BaseSuperAdmin { } } - public async restore(uid: string, body: IPutFoldersParams): Promise { + public async restore(uid: string, body: Partial): Promise { body.status = EFolderStatus.LIVE; try { return await this.put(uid, body); diff --git a/src/front/Components/DesignSystem/CheckBox/index.tsx b/src/front/Components/DesignSystem/CheckBox/index.tsx index 06a50c27..36d5d001 100644 --- a/src/front/Components/DesignSystem/CheckBox/index.tsx +++ b/src/front/Components/DesignSystem/CheckBox/index.tsx @@ -1,6 +1,6 @@ import React from "react"; -import { IOption } from "../Select"; +import { IOption } from "../Form/SelectField"; import Tooltip from "../ToolTip"; import Typography, { ITypo, ITypoColor } from "../Typography"; import classes from "./classes.module.scss"; diff --git a/src/front/Components/DesignSystem/DepositDocument/index.tsx b/src/front/Components/DesignSystem/DepositDocument/index.tsx index aac4c284..8d1a75e1 100644 --- a/src/front/Components/DesignSystem/DepositDocument/index.tsx +++ b/src/front/Components/DesignSystem/DepositDocument/index.tsx @@ -14,9 +14,9 @@ import Files from "@Front/Api/LeCoffreApi/SuperAdmin/Files/Files"; import { EDocumentStatus } from "le-coffre-resources/dist/Customer/Document"; import classNames from "classnames"; import Confirm from "../Modal/Confirm"; -import InputField from "../Form/Elements/InputField"; import GreenCheckIcon from "@Assets/Icons/green-check.svg"; import Loader from "../Loader"; +import TextAreaField from "../Form/TextareaField"; type IProps = { defaultFiles?: FileCustomer[]; @@ -69,7 +69,6 @@ export default class DepositDocument extends React.Component { } public override render(): JSX.Element { - console.log("Loading :", this.state.loading); return (
{ Votre document a été refusé pour la raison suivante : - +
diff --git a/src/front/Components/DesignSystem/Form/BaseField.tsx b/src/front/Components/DesignSystem/Form/BaseField.tsx new file mode 100644 index 00000000..109a776e --- /dev/null +++ b/src/front/Components/DesignSystem/Form/BaseField.tsx @@ -0,0 +1,78 @@ +import React from "react"; +import { FormContext, IFormContext } from "."; +import { ValidationError } from "class-validator"; + +export type IProps = { + value?: string; + onChange?: (event: React.ChangeEvent) => void; + name: string; + required?: boolean; + placeholder?: string; + readonly?: boolean; + className?: string; + defaultValue?: string; + disableValidation?: boolean; + validationError?: ValidationError; + disabled?: boolean; +}; + +type IState = { + value: string; + validationError: ValidationError | null; +}; + +export default abstract class BaseField

extends React.Component { + public static override contextType = FormContext; + public override context: IFormContext | null = null; + public fieldRef: React.RefObject = React.createRef(); + + static defaultProps: Partial = { + disableValidation: false, + required: true, + }; + + constructor(props: P) { + super(props); + this.onChange = this.onChange.bind(this); + } + + public override componentDidMount() { + this.context?.setField(this.props.name, this); + this.setState({ + value: this.props.defaultValue ?? "", + }); + } + + public override componentDidUpdate(prevProps: IProps) { + if (this.props.value !== prevProps.value) { + this.setState({ + value: this.props.value ?? "", + }); + } + + if (this.props.validationError !== prevProps.validationError) { + this.setState({ + validationError: this.props.validationError ?? null, + }); + } + } + + public override componentWillUnmount() { + this.context?.unSetField(this.props.name); + } + + protected getDefaultState(): IState { + return { + value: this.props.value ?? "", + validationError: this.props.validationError ?? null, + }; + } + + protected onChange(event: React.ChangeEvent) { + this.context?.onFieldChange(this.props.name, this); + this.setState({ value: event.currentTarget.value }); + if (this.props.onChange) { + this.props.onChange(event); + } + } +} diff --git a/src/front/Components/DesignSystem/Form/Elements/BaseField.tsx b/src/front/Components/DesignSystem/Form/Elements/BaseField.tsx deleted file mode 100644 index 4c2f40e8..00000000 --- a/src/front/Components/DesignSystem/Form/Elements/BaseField.tsx +++ /dev/null @@ -1,154 +0,0 @@ -import { ChangeEvent, Component, createRef } from "react"; - -import { FormContext, IFormContext } from ".."; -// elements -import Validators, { IValidationTypes } from "../Validators/Validators"; - -export type IError = { - message: string; - validator: string; - value: string | number | readonly string[]; - args: any[]; - isErrored?: (hasError: boolean) => void; -}; - -export type INewBasefieldProps = { - onChange?: (event: ChangeEvent) => void; - name: string; - regex?: RegExp; - onCancel?: () => void; - disableValidation?: boolean; - onErrors?: (errors: IError[]) => void; - fieldRef?: React.RefObject; -}; - -export type IProps = IValidationTypes & React.InputHTMLAttributes & INewBasefieldProps; - -type IState = { - value?: string | number | readonly string[]; - errors: IError[]; -}; - -export default abstract class BaseField

extends Component { - public static override contextType = FormContext; - public override context: IFormContext | null = null; - public fieldRef: React.RefObject = createRef(); - - static defaultProps: Partial = { - disableValidation: false, - }; - - constructor(props: P) { - super(props); - this.onChange = this.onChange.bind(this); - this.validate = this.validate.bind(this); - - this.state = { - value: this.props.value ?? this.props.defaultValue ?? "", - errors: [], - }; - } - - public override componentDidMount() { - this.context?.setField(this.props.name, this); - } - - public override componentDidUpdate(prevProps: P) { - if (prevProps.value !== this.props.value || prevProps.defaultValue !== this.props.defaultValue) { - this.setState({ value: this.props.value ?? this.props.defaultValue ?? "" }); - } - } - - public override componentWillUnmount() { - this.context?.unSetField(this.props.name); - } - - public async onBlur(event: React.FocusEvent) { - // this.validate(); - // if (this.props.onBlur) { - // this.props.onBlur(event); - // } - } - - public async validate(isOnSubmit?: boolean) { - if (this.props.disableValidation) return; - if (this.props.readOnly) return; - - const errorArray: IError[] = []; - const props: { [key: string]: any } = this.props; - const validators = Object.entries(Validators).filter(([key]) => props[key]); - - const isValidable = isOnSubmit - ? this.props.required || (this.state.value && this.state.value !== "") - : this.state.value && this.state.value !== ""; - - if (isValidable) { - const validations = await Promise.all( - validators.map(async ([key, validator]) => { - const validation = await (validator.validate as any)(this.state.value, ...(props[key].args ?? [])); - if (props[key].isErrored) { - props[key].isErrored(!validation); - } - return [key, validator, validation]; - }), - ); - - const unValidateds = validations.filter(([key, validator, validation]) => !validation); - const errors: IError[] = unValidateds.map(([key, unValidated]) => { - let message = unValidated.message; - if (typeof props[key] === "object" && props[key].message) message = props[key].message; - return { message, validator: key, value: this.state.value!, args: props[key].args ?? [] }; - }); - - errorArray.push(...errors); - } else { - validators.forEach(async ([key]) => { - if (props[key].isErrored) { - props[key].isErrored(false); - } - }); - } - - this.setState({ errors: errorArray }); - this.onErrors(errorArray); - return errorArray; - } - - public setErrors(errors: IError[]) { - this.setState({ ...this.state, errors }); - } - - /** - * It is automatically called by the parent form when the user cancelled the - * form and all of its changes. - * - * Override the method for custom cancelling logic, or pass a custom onCancel - * callback. - */ - public cancel() { - if (this.props.onCancel) { - this.props.onCancel(); - } - } - - public onErrors(errors: IError[]) { - if (this.props.onErrors) { - this.props.onErrors(errors); - } - } - - protected onChange(event: ChangeEvent) { - if (this.props.regex) { - if (!this.props.regex.test(event.currentTarget.value)) { - event.currentTarget.value = event.currentTarget.value.substring(0, event.currentTarget.value.length - 1); - } - } - this.setState({ value: event.currentTarget.value }, () => { - this.validate(); - this.context?.onFieldChange(this.props.name, this); - }); - if (this.props.onChange) { - this.props.onChange(event); - } - } -} diff --git a/src/front/Components/DesignSystem/Form/Elements/InputField/index.tsx b/src/front/Components/DesignSystem/Form/Elements/InputField/index.tsx deleted file mode 100644 index 7c91b6d1..00000000 --- a/src/front/Components/DesignSystem/Form/Elements/InputField/index.tsx +++ /dev/null @@ -1,111 +0,0 @@ -import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography"; -import { ReactNode } from "react"; - -import Validators from "../../Validators/Validators"; -import BaseField, { IProps as IBaseFieldProps } from "../BaseField"; -import classes from "./classes.module.scss"; - -export type IProps = IBaseFieldProps & { - fakeplaceholder: string; - textarea?: boolean; -}; - -// @ts-ignore TODO: typing error on IProps (validator class?? cf Massi 22/02/23) -export default class InputField extends BaseField { - static override defaultProps: Partial = { - ...BaseField.defaultProps, - required: true, - }; - - public override render(): ReactNode { - let pattern; - - if (this.props.type === "number") { - pattern = "(^[0-9]*)(\\.{0,1})([0-9]*)$"; - } - - if (this.props.pattern) { - pattern = this.props.pattern; - } - - if (this.props.fieldRef) { - this.fieldRef = this.props.fieldRef; - } - - // we always need to control the input so we need to set the value as "" by default - const value = this.state.value ?? ""; - - if (this.props.textarea === true) { - return ( - -

-