Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/dialogs/csv-import-filter-creation-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Alert from '@mui/material/Alert';
import PropTypes from 'prop-types';
import { DialogContentText } from '@mui/material';
import { ElementType } from '../../utils/elementType';
import { Generator, Load } from '../../utils/equipment-types';
import { EquipmentType } from '../../utils/equipment-types';
import { CancelButton } from '@gridsuite/commons-ui';

const CsvImportFilterCreationDialog = ({
Expand All @@ -41,8 +41,8 @@ const CsvImportFilterCreationDialog = ({
// TODO This is temporary : should be refactored to remove the business logic.
if (formType === ElementType.FILTER) {
if (
equipmentType === Generator.type ||
equipmentType === Load.type
equipmentType === EquipmentType.GENERATOR ||
equipmentType === EquipmentType.LOAD
) {
return [
intl.formatMessage({ id: 'equipmentID' }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from 'components/utils/field-constants';
import { useFieldArray, useWatch } from 'react-hook-form';
import { FormattedMessage } from 'react-intl';
import { Hvdc, Line } from 'utils/equipment-types';
import { EquipmentType } from 'utils/equipment-types';
import { FreePropertiesTypes } from './filter-properties';
import FilterProperty, {
PROPERTY_NAME,
Expand All @@ -37,8 +37,8 @@ function FilterFreeProperties({
name: EQUIPMENT_TYPE,
});
const isForLineOrHvdcLineSubstation =
(watchEquipmentType === Line.type ||
watchEquipmentType === Hvdc.type) &&
(watchEquipmentType === EquipmentType.LINE ||
watchEquipmentType === EquipmentType.HVDC_LINE) &&
freePropertiesType === FreePropertiesTypes.SUBSTATION_FILTER_PROPERTIES;

const fieldName = `${CRITERIA_BASED}.${freePropertiesType}`;
Expand Down
79 changes: 37 additions & 42 deletions src/components/dialogs/filter/criteria-based/filter-properties.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { useSnackMessage } from '@gridsuite/commons-ui';
import Grid from '@mui/material/Grid';
import { useEffect, useState } from 'react';
import { useEffect, useMemo } from 'react';
import { useWatch } from 'react-hook-form';
import { FormattedMessage } from 'react-intl';
import { fetchAppsAndUrls } from 'utils/rest-api';
import { FilterType } from '../../../../utils/elementType';
import {
Hvdc,
Line,
Load,
Substation,
} from '../../../../utils/equipment-types';
import { EquipmentType } from '../../../../utils/equipment-types';
import { areArrayElementsUnique } from '../../../../utils/functions';
import { EQUIPMENT_TYPE, FILTER_TYPE } from '../../../utils/field-constants';
import yup from '../../../utils/yup-config';
Expand All @@ -21,27 +14,13 @@ import {
PROPERTY_VALUES_1,
PROPERTY_VALUES_2,
} from './filter-property';
import { usePredefinedProperties } from '../../../../hooks/predefined-properties-hook';

export enum FreePropertiesTypes {
SUBSTATION_FILTER_PROPERTIES = 'substationFreeProperties',
FREE_FILTER_PROPERTIES = 'freeProperties',
}

function fetchPredefinedProperties() {
return fetchAppsAndUrls().then((res) => {
const studyMetadata = res.find(
(metadata: any) => metadata.name === 'Study'
);
if (!studyMetadata) {
return Promise.reject(
'Study entry could not be found in metadatas'
);
}

return studyMetadata?.predefinedEquipmentProperties?.substation;
});
}

function propertyValuesTest(
values: (string | undefined)[] | undefined,
context: yup.TestContext<yup.AnyObject>,
Expand All @@ -56,7 +35,8 @@ function propertyValuesTest(
}
const equipmentType = rootLevelForm.value[EQUIPMENT_TYPE];
const isForLineOrHvdcLine =
equipmentType === Line.type || equipmentType === Hvdc.type;
equipmentType === EquipmentType.LINE ||
equipmentType === EquipmentType.HVDC_LINE;
if (doublePropertyValues) {
return isForLineOrHvdcLine ? values?.length! > 0 : true;
} else {
Expand Down Expand Up @@ -152,24 +132,39 @@ export const filterPropertiesYupSchema = {
};

function FilterProperties() {
const watchEquipmentType = useWatch({
const watchEquipmentType: EquipmentType = useWatch({
name: EQUIPMENT_TYPE,
});
const isForSubstation = watchEquipmentType === Substation.type;
const isForLoad = watchEquipmentType === Load.type;
const [fieldProps, setFieldProps] = useState({});
const [equipmentPredefinedProps, setEquipmentType] =
usePredefinedProperties(watchEquipmentType);
const [substationPredefinedProps, setSubstationType] =
usePredefinedProperties(null);

const { snackError } = useSnackMessage();
const displayEquipmentProperties = useMemo(() => {
return (
watchEquipmentType === EquipmentType.SUBSTATION ||
watchEquipmentType === EquipmentType.LOAD
);
}, [watchEquipmentType]);

const displaySubstationProperties = useMemo(() => {
return (
watchEquipmentType !== EquipmentType.SUBSTATION &&
watchEquipmentType !== null
);
}, [watchEquipmentType]);

useEffect(() => {
fetchPredefinedProperties()
.then((p) => setFieldProps(p))
.catch((error) => {
snackError({
messageTxt: error.message ?? error,
});
});
}, [snackError]);
if (displayEquipmentProperties) {
setEquipmentType(watchEquipmentType);
}
}, [displayEquipmentProperties, watchEquipmentType, setEquipmentType]);

useEffect(() => {
if (displaySubstationProperties) {
setSubstationType(EquipmentType.SUBSTATION);
}
}, [displaySubstationProperties, setSubstationType]);

return (
watchEquipmentType && (
Expand All @@ -178,20 +173,20 @@ function FilterProperties() {
<FormattedMessage id={'FreePropsCrit'}>
{(txt) => <h3>{txt}</h3>}
</FormattedMessage>
{(isForSubstation || isForLoad) && (
{displayEquipmentProperties && (
<FilterFreeProperties
freePropertiesType={
FreePropertiesTypes.FREE_FILTER_PROPERTIES
}
predefined={fieldProps}
predefined={equipmentPredefinedProps}
/>
)}
{!isForSubstation && (
{displaySubstationProperties && (
<FilterFreeProperties
freePropertiesType={
FreePropertiesTypes.SUBSTATION_FILTER_PROPERTIES
}
predefined={fieldProps}
predefined={substationPredefinedProps}
/>
)}
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { FILTER_EQUIPMENTS } from '../../commons/criteria-based/criteria-based-u
import Grid from '@mui/material/Grid';
import { SelectInput, useSnackMessage } from '@gridsuite/commons-ui';
import { ValueParserParams } from 'ag-grid-community';
import { Generator, Load } from '../../../../utils/equipment-types';
import { EquipmentType } from '../../../../utils/equipment-types';
import { ElementType, FilterType } from '../../../../utils/elementType';
import { NumericEditor } from '../../../utils/rhf-inputs/ag-grid-table-rhf/cell-editors/numericEditor';
import { toFloatOrNullValue } from '../../../utils/dialog-utils';
Expand Down Expand Up @@ -80,7 +80,10 @@ export const explicitNamingFilterSchema = {
};

function isGeneratorOrLoad(equipmentType: string): boolean {
return equipmentType === Generator.type || equipmentType === Load.type;
return (
equipmentType === EquipmentType.GENERATOR ||
equipmentType === EquipmentType.LOAD
);
}

interface FilterTableRow {
Expand Down Expand Up @@ -111,6 +114,7 @@ export interface FilterForExplicitConversionProps {
id: UUID;
equipmentType: String;
}

interface ExplicitNamingFilterFormProps {
sourceFilterForExplicitNamingConversion?: FilterForExplicitConversionProps;
}
Expand Down
5 changes: 3 additions & 2 deletions src/components/dialogs/filter/filters-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createFilter, saveFilter } from '../../../utils/rest-api';
import { FilterType } from '../../../utils/elementType';
import { frontToBackTweak } from './criteria-based/criteria-based-filter-utils';
import { DESCRIPTION, EQUIPMENT_ID, NAME } from '../../utils/field-constants';
import { Generator, Load } from '../../../utils/equipment-types';
import { EquipmentType } from '../../../utils/equipment-types';
import { DISTRIBUTION_KEY } from './explicit-naming/explicit-naming-filter-form';
import { exportExpertRules } from './expert/expert-filter-utils';

Expand All @@ -26,7 +26,8 @@ export const saveExplicitNamingFilter = (
// we remove unnecessary fields from the table
let cleanedTableValues;
const isGeneratorOrLoad =
equipmentType === Generator.type || equipmentType === Load.type;
equipmentType === EquipmentType.GENERATOR ||
equipmentType === EquipmentType.LOAD;
if (isGeneratorOrLoad) {
cleanedTableValues = tableValues.map((row) => ({
[EQUIPMENT_ID]: row[EQUIPMENT_ID],
Expand Down
6 changes: 3 additions & 3 deletions src/components/utils/rqb-inputs/value-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import ElementValueEditor from './element-value-editor';
import { ElementType, FilterType } from '../../../utils/elementType';
import { EQUIPMENT_TYPE, FILTER_UUID } from '../field-constants';
import { useFormContext } from 'react-hook-form';
import { VoltageLevel } from '../../../utils/equipment-types';
import { EquipmentType } from '../../../utils/equipment-types';

const styles = {
noArrows: {
Expand Down Expand Up @@ -52,7 +52,7 @@ const ValueEditor = (props: ValueEditorProps) => {
props.field === FieldType.VOLTAGE_LEVEL_ID_1 ||
props.field === FieldType.VOLTAGE_LEVEL_ID_2) &&
value?.specificMetadata?.equipmentType ===
VoltageLevel.type))
EquipmentType.VOLTAGE_LEVEL))
);
}
return true;
Expand Down Expand Up @@ -87,7 +87,7 @@ const ValueEditor = (props: ValueEditorProps) => {
props.field === FieldType.VOLTAGE_LEVEL_ID_1 ||
props.field === FieldType.VOLTAGE_LEVEL_ID_2
) {
equipmentTypes = [VoltageLevel.type];
equipmentTypes = [EquipmentType.VOLTAGE_LEVEL];
} else if (props.field === FieldType.ID) {
equipmentTypes = [getValues(EQUIPMENT_TYPE)];
}
Expand Down
79 changes: 79 additions & 0 deletions src/hooks/predefined-properties-hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { Dispatch, SetStateAction, useEffect, useState } from 'react';
import { useSnackMessage } from '@gridsuite/commons-ui';
import { mapEquipmentTypeForPredefinedProperties } from '../utils/equipment-types-for-predefined-properties-mapper';
import { fetchAppsAndUrls } from '../utils/rest-api';
import { EquipmentType } from '../utils/equipment-types';

export type PredefinedProperties = {
[propertyName: string]: string[];
};

interface Metadata {
name: string;
url: string;
appColor: string;
hiddenInAppsMenu: boolean;
resources: unknown;
}

interface StudyMetadata extends Metadata {
name: 'Study';
predefinedEquipmentProperties: {
[networkElementType: string]: PredefinedProperties;
};
}

const isStudyMetadata = (metadata: Metadata): metadata is StudyMetadata => {
return metadata.name === 'Study';
};

const fetchPredefinedProperties = async (
equipmentType: EquipmentType
): Promise<PredefinedProperties | undefined> => {
const networkEquipmentType =
mapEquipmentTypeForPredefinedProperties(equipmentType);
if (networkEquipmentType === undefined) {
return Promise.resolve(undefined);
}
const res = await fetchAppsAndUrls();
const studyMetadata = res.filter(isStudyMetadata);
if (!studyMetadata) {
return Promise.reject('Study entry could not be found in metadata');
}
return studyMetadata[0].predefinedEquipmentProperties?.[
networkEquipmentType
];
};

export const usePredefinedProperties = (
initialType: EquipmentType | null
): [PredefinedProperties, Dispatch<SetStateAction<EquipmentType | null>>] => {
const [type, setType] = useState<EquipmentType | null>(initialType);
const [equipmentPredefinedProps, setEquipmentPredefinedProps] =
useState<PredefinedProperties>({});
const { snackError } = useSnackMessage();

useEffect(() => {
if (type !== null) {
fetchPredefinedProperties(type)
.then((p) => {
if (p !== undefined) {
setEquipmentPredefinedProps(p);
}
})
.catch((error) => {
snackError({
messageTxt: error.message ?? error,
});
});
}
}, [type, setEquipmentPredefinedProps, snackError]);

return [equipmentPredefinedProps, setType];
};
38 changes: 38 additions & 0 deletions src/utils/equipment-types-for-predefined-properties-mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { EquipmentType } from './equipment-types';

export const mapEquipmentTypeForPredefinedProperties = (
type: EquipmentType
): string | undefined => {
switch (type) {
case EquipmentType.SUBSTATION:
return 'substation';
case EquipmentType.LOAD:
return 'load';
case EquipmentType.GENERATOR:
return 'generator';
case EquipmentType.LINE:
return 'line';
case EquipmentType.TWO_WINDING_TRANSFORMER:
return 'twt';
case EquipmentType.BATTERY:
return 'battery';
case EquipmentType.SHUNT_COMPENSATOR:
return 'shuntCompensator';
case EquipmentType.VOLTAGE_LEVEL:
return 'voltageLevel';
case EquipmentType.BUSBAR_SECTION:
case EquipmentType.DANGLING_LINE:
case EquipmentType.HVDC_LINE:
case EquipmentType.LCC_CONVERTER_STATION:
case EquipmentType.THREE_WINDINGS_TRANSFORMER:
case EquipmentType.STATIC_VAR_COMPENSATOR:
case EquipmentType.VSC_CONVERTER_STATION:
return undefined;
}
};
Loading