Skip to content

Commit

Permalink
Fix typescript nullable arguments in criteria form schema (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH authored Nov 28, 2024
1 parent 577053a commit 7fa8aec
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 136 deletions.
29 changes: 16 additions & 13 deletions src/components/dialogs/customMuiDialog/CustomMuiDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import React, { useCallback, useState } from 'react';
import { type MouseEvent, type ReactNode, useCallback, useState } from 'react';
import { FieldErrors, FieldValues, SubmitHandler, UseFormReturn } from 'react-hook-form';
import { FormattedMessage } from 'react-intl';
import { Dialog, DialogActions, DialogContent, DialogTitle, Grid, LinearProgress } from '@mui/material';
import * as yup from 'yup';
import { type ObjectSchema } from 'yup';
import { SubmitButton } from '../../inputs/reactHookForm/utils/SubmitButton';
import { CancelButton } from '../../inputs/reactHookForm/utils/CancelButton';
import { CustomFormProvider, MergedFormContextProps } from '../../inputs/reactHookForm/provider/CustomFormProvider';
import { CustomFormProvider } from '../../inputs/reactHookForm/provider/CustomFormProvider';
import { PopupConfirmationDialog } from '../popupConfirmationDialog/PopupConfirmationDialog';

export interface CustomMuiDialogProps<T extends FieldValues = FieldValues> {
open: boolean;
formSchema: yup.AnySchema;
formMethods: UseFormReturn<T> | MergedFormContextProps;
onClose: (event?: React.MouseEvent) => void;
formSchema: ObjectSchema<T>;
formMethods: UseFormReturn<T>;
onClose: (event?: MouseEvent) => void;
onSave: SubmitHandler<T>;
onValidationError?: (errors: FieldErrors) => void;
titleId: string;
disabledSave?: boolean;
removeOptional?: boolean;
onCancel?: () => void;
children: React.ReactNode;
children: ReactNode;
isDataFetching?: boolean;
language?: string;
confirmationMessageKey?: string;
Expand Down Expand Up @@ -98,14 +98,14 @@ export function CustomMuiDialog<T extends FieldValues = FieldValues>({
const { handleSubmit } = formMethods;

const handleCancel = useCallback(
(event: React.MouseEvent) => {
(event: MouseEvent) => {
onCancel?.();
onClose(event);
},
[onCancel, onClose]
);

const handleClose = (event: React.MouseEvent, reason?: string) => {
const handleClose = (event: MouseEvent, reason?: string) => {
if (reason === 'backdropClick') {
return;
}
Expand Down Expand Up @@ -139,12 +139,15 @@ export function CustomMuiDialog<T extends FieldValues = FieldValues>({
}
}, [validate, validatedData]);

const handleValidationError = (errors: FieldErrors) => {
onValidationError?.(errors);
};
const handleValidationError = useCallback(
(errors: FieldErrors) => {
onValidationError?.(errors);
},
[onValidationError]
);

return (
<CustomFormProvider
<CustomFormProvider<T>
{...formMethods}
validationSchema={formSchema}
removeOptional={removeOptional}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const criteriaBasedFilterSchema = getCriteriaBasedSchema({
...filterPropertiesYupSchema,
});

export const criteriaBasedFilterEmptyFormData = getCriteriaBasedFormData(null, {
export const criteriaBasedFilterEmptyFormData = getCriteriaBasedFormData(undefined, {
[FieldConstants.ENERGY_SOURCE]: null,
[FreePropertiesTypes.SUBSTATION_FILTER_PROPERTIES]: [],
[FreePropertiesTypes.FREE_FILTER_PROPERTIES]: [],
Expand Down
116 changes: 62 additions & 54 deletions src/components/filter/criteriaBased/criteriaBasedFilterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,65 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { type ObjectSchema } from 'yup';
import { FieldConstants } from '../../../utils/constants/fieldConstants';
import { PROPERTY_NAME, PROPERTY_VALUES, PROPERTY_VALUES_1, PROPERTY_VALUES_2 } from './FilterProperty';
import { FilterType } from '../constants/FilterConstants';
import { PredefinedProperties } from '../../../utils/types/types';
import yup from '../../../utils/yupConfig';
import {
DEFAULT_RANGE_VALUE,
getRangeInputDataForm,
getRangeInputSchema,
type RangeInputData,
} from '../../inputs/reactHookForm/numbers/RangeInput';
import { FreePropertiesTypes } from './FilterFreeProperties';

export const getCriteriaBasedSchema = (extraFields: Record<string, yup.AnyObject | null>) => ({
[FieldConstants.CRITERIA_BASED]: yup.object().shape({
[FieldConstants.COUNTRIES]: yup.array().of(yup.string()),
[FieldConstants.COUNTRIES_1]: yup.array().of(yup.string()),
[FieldConstants.COUNTRIES_2]: yup.array().of(yup.string()),
...getRangeInputSchema(FieldConstants.NOMINAL_VOLTAGE),
...getRangeInputSchema(FieldConstants.NOMINAL_VOLTAGE_1),
...getRangeInputSchema(FieldConstants.NOMINAL_VOLTAGE_2),
...getRangeInputSchema(FieldConstants.NOMINAL_VOLTAGE_3),
...extraFields,
}),
});
export const getCriteriaBasedFormData = (criteriaValues: any, extraFields: Record<string, yup.AnyObject | null>) => ({
[FieldConstants.CRITERIA_BASED]: {
[FieldConstants.COUNTRIES]: criteriaValues?.[FieldConstants.COUNTRIES] ?? [],
[FieldConstants.COUNTRIES_1]: criteriaValues?.[FieldConstants.COUNTRIES_1] ?? [],
[FieldConstants.COUNTRIES_2]: criteriaValues?.[FieldConstants.COUNTRIES_2] ?? [],
...getRangeInputDataForm(
FieldConstants.NOMINAL_VOLTAGE,
criteriaValues?.[FieldConstants.NOMINAL_VOLTAGE] ?? DEFAULT_RANGE_VALUE
),
...getRangeInputDataForm(
FieldConstants.NOMINAL_VOLTAGE_1,
criteriaValues?.[FieldConstants.NOMINAL_VOLTAGE_1] ?? DEFAULT_RANGE_VALUE
),
...getRangeInputDataForm(
FieldConstants.NOMINAL_VOLTAGE_2,
criteriaValues?.[FieldConstants.NOMINAL_VOLTAGE_2] ?? DEFAULT_RANGE_VALUE
),
...getRangeInputDataForm(
FieldConstants.NOMINAL_VOLTAGE_3,
criteriaValues?.[FieldConstants.NOMINAL_VOLTAGE_3] ?? DEFAULT_RANGE_VALUE
),
...extraFields,
},
});
export type CriteriaBasedData = {
[FieldConstants.COUNTRIES]?: string[];
[FieldConstants.COUNTRIES_1]?: string[];
[FieldConstants.COUNTRIES_2]?: string[];
[FieldConstants.NOMINAL_VOLTAGE]?: RangeInputData | null;
[FieldConstants.NOMINAL_VOLTAGE_1]?: RangeInputData | null;
[FieldConstants.NOMINAL_VOLTAGE_2]?: RangeInputData | null;
[FieldConstants.NOMINAL_VOLTAGE_3]?: RangeInputData | null;
[key: string]: any;
};

export function getCriteriaBasedSchema(extraFields: Record<string, yup.AnyObject | null> = {}) {
return {
[FieldConstants.CRITERIA_BASED]: yup.object().shape({
[FieldConstants.COUNTRIES]: yup.array().of(yup.string().required()),
[FieldConstants.COUNTRIES_1]: yup.array().of(yup.string().required()),
[FieldConstants.COUNTRIES_2]: yup.array().of(yup.string().required()),
...getRangeInputSchema(FieldConstants.NOMINAL_VOLTAGE),
...getRangeInputSchema(FieldConstants.NOMINAL_VOLTAGE_1),
...getRangeInputSchema(FieldConstants.NOMINAL_VOLTAGE_2),
...getRangeInputSchema(FieldConstants.NOMINAL_VOLTAGE_3),
...extraFields,
}),
} as const satisfies Record<FieldConstants.CRITERIA_BASED, ObjectSchema<CriteriaBasedData>>;
}

export function getCriteriaBasedFormData(
criteriaValues?: Record<string, any>,
extraFields: Record<string, yup.AnyObject | null> = {}
) {
return {
[FieldConstants.CRITERIA_BASED]: {
[FieldConstants.COUNTRIES]: criteriaValues?.[FieldConstants.COUNTRIES] ?? [],
[FieldConstants.COUNTRIES_1]: criteriaValues?.[FieldConstants.COUNTRIES_1] ?? [],
[FieldConstants.COUNTRIES_2]: criteriaValues?.[FieldConstants.COUNTRIES_2] ?? [],
[FieldConstants.NOMINAL_VOLTAGE]: criteriaValues?.[FieldConstants.NOMINAL_VOLTAGE] ?? DEFAULT_RANGE_VALUE,
[FieldConstants.NOMINAL_VOLTAGE_1]:
criteriaValues?.[FieldConstants.NOMINAL_VOLTAGE_1] ?? DEFAULT_RANGE_VALUE,
[FieldConstants.NOMINAL_VOLTAGE_2]:
criteriaValues?.[FieldConstants.NOMINAL_VOLTAGE_2] ?? DEFAULT_RANGE_VALUE,
[FieldConstants.NOMINAL_VOLTAGE_3]:
criteriaValues?.[FieldConstants.NOMINAL_VOLTAGE_3] ?? DEFAULT_RANGE_VALUE,
...extraFields,
},
} as const;
}

/**
* Transform
Expand All @@ -65,7 +76,7 @@ export const getCriteriaBasedFormData = (criteriaValues: any, extraFields: Recor
* {name_property:namesB, prop_values:valuesB}]
* @author Laurent LAUGARN modified by Florent MILLOT
*/
export const backToFrontTweak = (response: any) => {
export function backToFrontTweak(response: any) {
const subProps = response.equipmentFilterForm.substationFreeProperties;
const freeProps = response.equipmentFilterForm.freeProperties;
const props1 = response.equipmentFilterForm.freeProperties1;
Expand Down Expand Up @@ -111,18 +122,17 @@ export const backToFrontTweak = (response: any) => {
filterFreeProperties.push(prop);
});

const ret = {
return {
[FieldConstants.EQUIPMENT_TYPE]: response[FieldConstants.EQUIPMENT_TYPE],
...getCriteriaBasedFormData(response.equipmentFilterForm, {
[FieldConstants.ENERGY_SOURCE]: response.equipmentFilterForm[FieldConstants.ENERGY_SOURCE],
[FreePropertiesTypes.SUBSTATION_FILTER_PROPERTIES]: filterSubstationProperties,
[FreePropertiesTypes.FREE_FILTER_PROPERTIES]: filterFreeProperties,
}),
};
return ret;
};
} as const;
}

function isNominalVoltageEmpty(nominalVoltage: Record<string, unknown>): boolean {
function isNominalVoltageEmpty(nominalVoltage: Record<string, unknown>) {
return nominalVoltage[FieldConstants.VALUE_1] === null && nominalVoltage[FieldConstants.VALUE_2] === null;
}

Expand Down Expand Up @@ -155,20 +165,13 @@ function cleanNominalVoltages(formValues: any) {
* freeProperties2.{nameA:valuesC}}
* @author Laurent LAUGARN modified by Florent MILLOT
*/
export const frontToBackTweak = (id?: string, filter?: any) => {
export function frontToBackTweak(id?: string, filter?: any) {
const filterSubstationProperties =
filter[FieldConstants.CRITERIA_BASED][FreePropertiesTypes.SUBSTATION_FILTER_PROPERTIES];
const ret = {
id,
type: FilterType.CRITERIA_BASED.id,
equipmentFilterForm: undefined,
};
const eff = {
[FieldConstants.EQUIPMENT_TYPE]: filter[FieldConstants.EQUIPMENT_TYPE],
...cleanNominalVoltages(filter[FieldConstants.CRITERIA_BASED]),
};
// in the back end we store everything in a field called equipmentFilterForm
ret.equipmentFilterForm = eff;
delete eff[FreePropertiesTypes.SUBSTATION_FILTER_PROPERTIES];
const props: any = {};
const props1: any = {};
Expand All @@ -194,13 +197,18 @@ export const frontToBackTweak = (id?: string, filter?: any) => {
const filterFreeProperties = filter[FieldConstants.CRITERIA_BASED][FreePropertiesTypes.FREE_FILTER_PROPERTIES];
// in the back end we store everything in a field called equipmentFilterForm
delete eff[FreePropertiesTypes.FREE_FILTER_PROPERTIES];
const freeProps: any = {};
const freeProps: Record<string, unknown> = {};
filterFreeProperties.forEach((prop: any) => {
const values = prop[PROPERTY_VALUES];
if (values) {
freeProps[prop[PROPERTY_NAME]] = values;
}
});
eff.freeProperties = freeProps;
return ret;
};
return {
id,
type: FilterType.CRITERIA_BASED.id,
// in the back end we store everything in a field called equipmentFilterForm
equipmentFilterForm: eff,
};
}
Loading

0 comments on commit 7fa8aec

Please sign in to comment.