diff --git a/src/front/Api/BaseApiService.ts b/src/front/Api/BaseApiService.ts index 2992b609..166621bc 100644 --- a/src/front/Api/BaseApiService.ts +++ b/src/front/Api/BaseApiService.ts @@ -119,17 +119,18 @@ export default abstract class BaseApiService { responseJson = await response.json(); } catch (err: unknown) { responseJson = null; + return Promise.reject(err); } if (!response.ok) { - return Promise.reject(response); + return Promise.reject(responseJson); } return responseJson as T; } protected onError(error: unknown) { - console.error(error); + //console.error(error); } } diff --git a/src/front/Components/DesignSystem/Form/BaseField.tsx b/src/front/Components/DesignSystem/Form/BaseField.tsx index 109a776e..43898868 100644 --- a/src/front/Components/DesignSystem/Form/BaseField.tsx +++ b/src/front/Components/DesignSystem/Form/BaseField.tsx @@ -1,6 +1,7 @@ import React from "react"; import { FormContext, IFormContext } from "."; import { ValidationError } from "class-validator"; +import Typography, { ITypo, ITypoColor } from "../Typography"; export type IProps = { value?: string; @@ -34,6 +35,10 @@ export default abstract class BaseField

) { + this.context?.onFieldFocusChange(this.props.name, this, true); + } + + protected onBlur(event: React.FocusEvent) { + this.context?.onFieldFocusChange(this.props.name, this, false); + } + protected onChange(event: React.ChangeEvent) { this.context?.onFieldChange(this.props.name, this); - this.setState({ value: event.currentTarget.value }); + this.setState({ value: event.currentTarget.value, validationError: null }); if (this.props.onChange) { this.props.onChange(event); } } + + protected hasError(): boolean { + return this.state.validationError !== null; + // if(!this.context) return false; + // if(!this.context.hasOneFocusedInput() && this.state.validationError !== null) return true; + // return this.state.validationError !== null && this.context.isInputFocused(this.props.name); + } + + protected renderErrors(): JSX.Element[] | null { + if (!this.state.validationError || !this.state.validationError.constraints) return null; + let errors: JSX.Element[] = []; + Object.entries(this.state.validationError.constraints).forEach(([key, value]) => { + errors.push( + + {value} + , + ); + }); + return errors; + } } diff --git a/src/front/Components/DesignSystem/Form/SelectField/index.tsx b/src/front/Components/DesignSystem/Form/SelectField/index.tsx index 6abd2dbe..bf08aa3b 100644 --- a/src/front/Components/DesignSystem/Form/SelectField/index.tsx +++ b/src/front/Components/DesignSystem/Form/SelectField/index.tsx @@ -15,7 +15,7 @@ type IProps = { hasBorderRightCollapsed?: boolean; placeholder?: string; className?: string; - name?: string; + name: string; disabled: boolean; errors?: ValidationError; }; @@ -60,53 +60,58 @@ export default class SelectField extends React.Component { public override render(): JSX.Element { const selectedOption = this.state.selectedOption ?? this.props.selectedOption; return ( -

- {selectedOption && } - +
+
+ {selectedOption && } + -
    - {this.props.options.map((option, index) => ( -
  • this.onSelect(option, e)}> -
    {option.icon}
    - {option.label} -
  • - ))} -
+
    + {this.props.options.map((option, index) => ( +
  • this.onSelect(option, e)}> +
    {option.icon}
    + {option.label} +
  • + ))} +
- {this.state.isOpen &&
} + {this.state.isOpen &&
} +
{this.state.errors !== null &&
{this.renderErrors()}
}
); @@ -172,16 +177,12 @@ export default class SelectField extends React.Component { this.toggle(e); } - private renderErrors(): JSX.Element[] | null { - if (!this.state.errors || !this.state.errors.constraints) return null; - let errors: JSX.Element[] = []; - Object.entries(this.state.errors.constraints).forEach(([key, value]) => { - errors.push( - - {value} - , - ); - }); - return errors; + private renderErrors(): JSX.Element | null { + if (!this.state.errors) return null; + return ( + + {this.props.placeholder} est requis + + ); } } diff --git a/src/front/Components/DesignSystem/Form/TextField/index.tsx b/src/front/Components/DesignSystem/Form/TextField/index.tsx index 76cb1e53..366e6f61 100644 --- a/src/front/Components/DesignSystem/Form/TextField/index.tsx +++ b/src/front/Components/DesignSystem/Form/TextField/index.tsx @@ -18,34 +18,23 @@ export default class TextField extends BaseField { const value = this.state.value ?? ""; return ( -
+
{this.props.placeholder} {!this.props.required && " (Facultatif)"}
- {this.state.validationError !== null &&
{this.renderErrors()}
} + {this.hasError() &&
{this.renderErrors()}
} ); } - - private renderErrors(): JSX.Element[] | null { - if (!this.state.validationError || !this.state.validationError.constraints) return null; - let errors: JSX.Element[] = []; - Object.entries(this.state.validationError.constraints).forEach(([key, value]) => { - errors.push( - - {value} - , - ); - }); - return errors; - } } diff --git a/src/front/Components/DesignSystem/Form/TextareaField/index.tsx b/src/front/Components/DesignSystem/Form/TextareaField/index.tsx index 4115c557..43dd9c75 100644 --- a/src/front/Components/DesignSystem/Form/TextareaField/index.tsx +++ b/src/front/Components/DesignSystem/Form/TextareaField/index.tsx @@ -18,7 +18,7 @@ export default class TextAreaField extends BaseField { const value = this.state.value ?? ""; return ( -
+