Skip to content

Commit

Permalink
Merge branch 'main' into limit-sets-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu-Deharbe authored Jan 10, 2025
2 parents 55c7369 + 1503e55 commit c739d00
Show file tree
Hide file tree
Showing 30 changed files with 293 additions and 224 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": {
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@gridsuite/commons-ui": "0.76.2",
"@gridsuite/commons-ui": "0.77.0",
"@hookform/resolvers": "^3.3.4",
"@mui/icons-material": "^5.15.14",
"@mui/lab": "5.0.0-alpha.169",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

import type { ReadonlyDeep } from 'type-fest';
import { DataType, FieldOptionType, FieldType } from './assignment.type';
import { DataType, FieldOptionType } from './assignment.type';
import { LOAD_TYPES } from '../../../../../network/constants';
import { EquipmentType, kiloUnitToUnit, microUnitToUnit, unitToKiloUnit, unitToMicroUnit } from '@gridsuite/commons-ui';
import { KILO_AMPERE, MICRO_SIEMENS } from '../../../../../utils/field-constants';
import { EquipmentType, FieldType } from '@gridsuite/commons-ui';
import { KILO_AMPERE, MEGA_VAR, MICRO_SIEMENS, SIEMENS } from '../../../../../utils/field-constants';

export const FIELD_OPTIONS = {
PROPERTY: {
Expand Down Expand Up @@ -105,18 +105,14 @@ export const FIELD_OPTIONS = {
MAX_SUSCEPTANCE: {
id: FieldType.MAX_SUSCEPTANCE,
label: 'maxSusceptance',
unit: MICRO_SIEMENS,
unit: SIEMENS,
dataType: DataType.DOUBLE,
outputConverter: (value) => microUnitToUnit(value),
inputConverter: (value) => unitToMicroUnit(value),
},
MAX_Q_AT_NOMINAL_V: {
id: FieldType.MAX_Q_AT_NOMINAL_V,
label: 'maxQAtNominalV',
unit: MICRO_SIEMENS,
unit: MEGA_VAR,
dataType: DataType.DOUBLE,
outputConverter: (value) => microUnitToUnit(value),
inputConverter: (value) => unitToMicroUnit(value),
},
NOMINAL_VOLTAGE: {
id: FieldType.NOMINAL_VOLTAGE,
Expand All @@ -138,16 +134,12 @@ export const FIELD_OPTIONS = {
label: 'LowShortCircuitCurrentLimit',
unit: KILO_AMPERE,
dataType: DataType.DOUBLE,
outputConverter: (value) => kiloUnitToUnit(value),
inputConverter: (value) => unitToKiloUnit(value),
},
HIGH_SHORT_CIRCUIT_CURRENT_LIMIT: {
id: FieldType.HIGH_SHORT_CIRCUIT_CURRENT_LIMIT,
label: 'HighShortCircuitCurrentLimit',
unit: KILO_AMPERE,
dataType: DataType.DOUBLE,
outputConverter: (value) => kiloUnitToUnit(value),
inputConverter: (value) => unitToKiloUnit(value),
},
ACTIVE_POWER: {
id: FieldType.ACTIVE_POWER,
Expand All @@ -174,16 +166,12 @@ export const FIELD_OPTIONS = {
label: 'G',
unit: MICRO_SIEMENS,
dataType: DataType.DOUBLE,
outputConverter: (value) => microUnitToUnit(value),
inputConverter: (value) => unitToMicroUnit(value),
},
B: {
id: FieldType.B,
label: 'B',
unit: MICRO_SIEMENS,
dataType: DataType.DOUBLE,
outputConverter: (value) => microUnitToUnit(value),
inputConverter: (value) => unitToMicroUnit(value),
},
RATED_U1: {
id: FieldType.RATED_U1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import DensityLargeIcon from '@mui/icons-material/DensityLarge';
import { EDITED_FIELD, FILTERS, PROPERTY_NAME_FIELD, VALUE_FIELD } from '../../../../../utils/field-constants';
import { useFormContext, useWatch } from 'react-hook-form';
import { getIdOrValue } from '../../../../commons/utils';
import { useIntl } from 'react-intl';
import { DataType, FieldOptionType } from './assignment.type';
import { areIdsEqual, comparatorStrIgnoreCase } from '../../../../../utils/utils';
import { PredefinedProperties } from '../../../common/properties/property-utils';
import GridItem from '../../../../commons/grid-item';
import useFormatLabelWithUnit from '../../../../../../hooks/use-format-label-with-unit';

interface AssignmentFormProps {
name: string;
Expand All @@ -41,8 +41,6 @@ const AssignmentForm: FC<AssignmentFormProps> = ({
equipmentFields,
equipmentType,
}) => {
const intl = useIntl();

const { setValue } = useFormContext();

const watchEditedField = useWatch({
Expand Down Expand Up @@ -76,14 +74,7 @@ const AssignmentForm: FC<AssignmentFormProps> = ({
setValue(`${name}.${index}.${VALUE_FIELD}`, dataType === DataType.BOOLEAN ? false : null);
}

const formatLabelWithUnit = useMemo(() => {
return (value: string | { label: string; unit?: string }) => {
if (typeof value === 'string') {
return value;
}
return `${intl.formatMessage({ id: value.label })} ${value.unit ?? ''}`;
};
}, [intl]);
const formatLabelWithUnit = useFormatLabelWithUnit();

const filtersField = (
<DirectoryItemsInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ export const getFieldOption = (fieldName?: string | null) => {
return Object.values(FIELD_OPTIONS).find((fieldOption) => fieldOption.id === fieldName);
};

export const convertOutputValue = (fieldName?: string | null, fieldValue?: FieldValue) => {
const fieldOption = getFieldOption(fieldName);
// @ts-expect-error TODO TS2339: Property outputConverter does not exist on typeof FIELD_OPTIONS[*]
return fieldOption?.outputConverter ? fieldOption.outputConverter(Number(fieldValue)) : fieldValue;
};

export const convertInputValue = (fieldName?: string | null, fieldValue?: FieldValue) => {
const fieldOption = getFieldOption(fieldName);
// @ts-expect-error TODO TS2339: Property inputConverter does not exist on typeof FIELD_OPTIONS[*]
return fieldOption?.inputConverter ? fieldOption.inputConverter(Number(fieldValue)) : fieldValue;
};

// ("undefined" is accepted here in RHF, but it conflicts with MUI behaviour which does not like undefined values)
export const getAssignmentInitialValue = () => ({
[FILTERS]: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export enum FieldType {
PHASE_TARGET_DEADBAND = 'PHASE_TARGET_DEADBAND',
LOAD_TYPE = 'LOAD_TYPE',
}

// --- types for the form model --- //

export type Assignment = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

import { yupResolver } from '@hookform/resolvers/yup';
import yup from 'components/utils/yup-config';
import { CustomFormProvider, useSnackMessage } from '@gridsuite/commons-ui';
import {
convertInputValue,
convertOutputValue,
CustomFormProvider,
FieldType,
useSnackMessage,
} from '@gridsuite/commons-ui';
import { FC, useCallback, useEffect } from 'react';
import { FetchStatus } from '../../../../../services/utils';
import { useForm } from 'react-hook-form';
Expand All @@ -18,8 +24,6 @@ import ModificationByAssignmentForm from './modification-by-assignment-form';
import { ASSIGNMENTS, EDITED_FIELD, EQUIPMENT_TYPE_FIELD, VALUE_FIELD } from '../../../../utils/field-constants';
import { modifyByAssignment } from '../../../../../services/study/network-modifications';
import {
convertInputValue,
convertOutputValue,
getAssignmentFromEditData,
getAssignmentInitialValue,
getAssignmentsSchema,
Expand Down Expand Up @@ -72,7 +76,10 @@ const ModificationByAssignmentDialog: FC<any> = ({
const assignments: Assignment[] =
editData.assignmentInfosList?.map((info: Assignment) => {
const assignment = getAssignmentFromEditData(info);
const valueConverted = convertInputValue(assignment[EDITED_FIELD], assignment[VALUE_FIELD]);
const fieldKey = assignment[EDITED_FIELD] as keyof typeof FieldType;
const field = FieldType[fieldKey];
const value = assignment[VALUE_FIELD];
const valueConverted = convertInputValue(field, value);
return {
...assignment,
[VALUE_FIELD]: valueConverted,
Expand All @@ -93,7 +100,10 @@ const ModificationByAssignmentDialog: FC<any> = ({
(formData: ModificationByAssignment) => {
const assignmentsList = formData[ASSIGNMENTS].map((assignment) => {
const dataType = getDataType(assignment[EDITED_FIELD]);
const valueConverted = convertOutputValue(assignment[EDITED_FIELD], assignment[VALUE_FIELD]);
const fieldKey = assignment[EDITED_FIELD] as keyof typeof FieldType;
const field = FieldType[fieldKey];
const value = assignment[VALUE_FIELD];
const valueConverted = convertOutputValue(field, value);
return {
...assignment,
dataType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

import { yupResolver } from '@hookform/resolvers/yup';
import yup from 'components/utils/yup-config';
import { CustomFormProvider, useSnackMessage } from '@gridsuite/commons-ui';
import {
convertInputValue,
convertOutputValue,
CustomFormProvider,
FieldType,
useSnackMessage,
} from '@gridsuite/commons-ui';
import { useCallback, useEffect } from 'react';
import { FetchStatus } from '../../../../../services/utils';
import { useForm } from 'react-hook-form';
Expand All @@ -29,11 +35,11 @@ import {
import { modifyByFormula } from '../../../../../services/study/network-modifications';
import { getFormulaInitialValue, getFormulaSchema } from './formula/formula-utils';

function getFieldOrValue(input) {
function getFieldOrConvertedUnitValue(input, fieldType) {
const value = input.replace(',', '.');
const isNumber = !isNaN(parseFloat(value));
return {
[VALUE]: isNumber ? value : null,
[VALUE]: isNumber ? convertOutputValue(fieldType, value) : null,
[EQUIPMENT_FIELD]: isNumber ? null : input,
};
}
Expand Down Expand Up @@ -71,8 +77,16 @@ const ByFormulaDialog = ({ editData, currentNode, studyUuid, isUpdate, editDataF
useEffect(() => {
if (editData) {
const formulas = editData.formulaInfosList?.map((formula) => {
const ref1 = formula?.fieldOrValue1?.value?.toString() ?? formula?.fieldOrValue1?.equipmentField;
const ref2 = formula?.fieldOrValue2?.value?.toString() ?? formula?.fieldOrValue2?.equipmentField;
const valueConverted1 = convertInputValue(
FieldType[formula[EDITED_FIELD]],
formula?.fieldOrValue1?.value
);
const valueConverted2 = convertInputValue(
FieldType[formula[EDITED_FIELD]],
formula?.fieldOrValue2?.value
);
const ref1 = valueConverted1?.toString() ?? formula?.fieldOrValue1?.equipmentField;
const ref2 = valueConverted2?.toString() ?? formula?.fieldOrValue2?.equipmentField;
return {
[REFERENCE_FIELD_OR_VALUE_1]: ref1,
[REFERENCE_FIELD_OR_VALUE_2]: ref2,
Expand All @@ -95,8 +109,14 @@ const ByFormulaDialog = ({ editData, currentNode, studyUuid, isUpdate, editDataF
const onSubmit = useCallback(
(data) => {
const formulas = data[FORMULAS].map((formula) => {
const fieldOrValue1 = getFieldOrValue(formula[REFERENCE_FIELD_OR_VALUE_1]);
const fieldOrValue2 = getFieldOrValue(formula[REFERENCE_FIELD_OR_VALUE_2]);
const fieldOrValue1 = getFieldOrConvertedUnitValue(
formula[REFERENCE_FIELD_OR_VALUE_1],
FieldType[formula[EDITED_FIELD]]
);
const fieldOrValue2 = getFieldOrConvertedUnitValue(
formula[REFERENCE_FIELD_OR_VALUE_2],
FieldType[formula[EDITED_FIELD]]
);
return {
fieldOrValue1,
fieldOrValue2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { EQUIPMENTS_FIELDS } from './formula-utils';
import ReferenceAutocompleteInput from './reference-autocomplete-input';
import DragHandleIcon from '@mui/icons-material/DragHandle';
import { getIdOrValue, getLabelOrValue } from '../../../../commons/utils';
import { useIntl } from 'react-intl';
import { Grid } from '@mui/material';
import GridItem from '../../../../commons/grid-item';
import useFormatLabelWithUnit from '../../../../../../hooks/use-format-label-with-unit';

interface FormulaProps {
name: string;
Expand All @@ -40,13 +40,12 @@ const FormulaForm: FunctionComponent<FormulaProps> = ({ name, index }) => {
const equipmentTypeWatch = useWatch({
name: EQUIPMENT_TYPE_FIELD,
});

const intl = useIntl();

const equipmentFields: { id: string; label: string }[] =
const equipmentFields: { id: string; label: string; unit: string }[] =
// @ts-expect-error TODO: missing type in context
EQUIPMENTS_FIELDS?.[equipmentTypeWatch] ?? [];

const formatLabelWithUnit = useFormatLabelWithUnit();

const filtersField = (
<DirectoryItemsInput
name={`${name}.${index}.${FILTERS}`}
Expand All @@ -66,7 +65,7 @@ const FormulaForm: FunctionComponent<FormulaProps> = ({ name, index }) => {
size={'small'}
inputTransform={(value: any) => equipmentFields.find((option) => option?.id === value) || value}
outputTransform={(option: any) => getIdOrValue(option) ?? null}
getOptionLabel={(option: any) => intl.formatMessage({ id: getLabelOrValue(option) })}
getOptionLabel={(option: any) => formatLabelWithUnit(option)}
/>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import {
EQUIPMENT_TYPE_FIELD,
FILTERS,
ID,
KILO_AMPERE,
MEGA_VAR,
MICRO_SIEMENS,
NAME,
OPERATOR,
REFERENCE_FIELD_OR_VALUE_1,
REFERENCE_FIELD_OR_VALUE_2,
SIEMENS,
SPECIFIC_METADATA,
TYPE,
} from '../../../../../utils/field-constants';
Expand All @@ -24,6 +28,7 @@ import { AnyObject, TestContext, TestFunction } from 'yup';
export type EquipmentField = {
id: string;
label: string;
unit?: string;
};
type EquipmentFieldsKeys =
| EQUIPMENT_TYPES.GENERATOR
Expand Down Expand Up @@ -69,8 +74,8 @@ export const EQUIPMENTS_FIELDS: EquipmentFields = {
[EQUIPMENT_TYPES.SHUNT_COMPENSATOR]: [
{ id: 'MAXIMUM_SECTION_COUNT', label: 'maximumSectionCount' },
{ id: 'SECTION_COUNT', label: 'sectionCount' },
{ id: 'MAX_SUSCEPTANCE', label: 'maxSusceptance' },
{ id: 'MAX_Q_AT_NOMINAL_V', label: 'maxQAtNominalV' },
{ id: 'MAX_SUSCEPTANCE', label: 'maxSusceptance', unit: SIEMENS },
{ id: 'MAX_Q_AT_NOMINAL_V', label: 'maxQAtNominalV', unit: MEGA_VAR },
],
[EQUIPMENT_TYPES.VOLTAGE_LEVEL]: [
{ id: 'NOMINAL_VOLTAGE', label: 'NominalVoltage' },
Expand All @@ -79,10 +84,12 @@ export const EQUIPMENTS_FIELDS: EquipmentFields = {
{
id: 'LOW_SHORT_CIRCUIT_CURRENT_LIMIT',
label: 'LowShortCircuitCurrentLimit',
unit: KILO_AMPERE,
},
{
id: 'HIGH_SHORT_CIRCUIT_CURRENT_LIMIT',
label: 'HighShortCircuitCurrentLimit',
unit: KILO_AMPERE,
},
],
[EQUIPMENT_TYPES.LOAD]: [
Expand All @@ -92,8 +99,8 @@ export const EQUIPMENTS_FIELDS: EquipmentFields = {
[EQUIPMENT_TYPES.TWO_WINDINGS_TRANSFORMER]: [
{ id: 'R', label: 'SeriesResistanceText' },
{ id: 'X', label: 'SeriesReactanceText' },
{ id: 'G', label: 'G' },
{ id: 'B', label: 'B' },
{ id: 'G', label: 'G', unit: MICRO_SIEMENS },
{ id: 'B', label: 'B', unit: MICRO_SIEMENS },
{ id: 'RATED_U1', label: 'RatedU1' },
{ id: 'RATED_U2', label: 'RatedU2' },
{ id: 'RATED_S', label: 'RatedNominalPowerText' },
Expand Down
Loading

0 comments on commit c739d00

Please sign in to comment.