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
15 changes: 11 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 @@ -11,7 +11,7 @@
"dependencies": {
"@emotion/react": "^11.14.0",
"@emotion/styled": "^11.14.0",
"@gridsuite/commons-ui": "0.102.0",
"@gridsuite/commons-ui": "file:../commons-ui/gridsuite-commons-ui-0.102.0.tgz",
"@hello-pangea/dnd": "^18.0.1",
"@hookform/resolvers": "^4.0.0",
"@mui/icons-material": "^5.16.14",
Expand Down
2 changes: 2 additions & 0 deletions src/components/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
NotificationsUrlKeys,
useNotificationsListener,
useSnackMessage,
useYupIntl,
} from '@gridsuite/commons-ui';
import PageNotFound from './page-not-found';
import { FormattedMessage } from 'react-intl';
Expand Down Expand Up @@ -77,6 +78,7 @@ const STUDY_VIEWS = [StudyView.MAP, StudyView.SPREADSHEET, StudyView.RESULTS, St

const App = () => {
const { snackError } = useSnackMessage();
useYupIntl();

const appTabIndex = useSelector((state) => state.appTabIndex);
const user = useSelector((state) => state.user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,32 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { type IntlShape } from 'react-intl';
import * as yup from 'yup';
import { DROOP, FREQUENCY_REGULATION } from '../../utils/field-constants';
import yup from '../../utils/yup-config';

export const getActivePowerControlEmptyFormData = (isEquipmentModification = false) => ({
[FREQUENCY_REGULATION]: isEquipmentModification ? null : false,
[DROOP]: null,
});

export const getActivePowerControlSchema = (isEquipmentModification = false) => ({
[FREQUENCY_REGULATION]: yup
.bool()
.nullable()
.when([], {
is: () => !isEquipmentModification,
then: (schema) => schema.required(),
}),
[DROOP]: yup
.number()
.nullable()
.min(0, 'NormalizedPercentage')
.max(100, 'NormalizedPercentage')
.when([FREQUENCY_REGULATION], {
is: (frequencyRegulation: boolean) => !isEquipmentModification && frequencyRegulation,
then: (schema) => schema.required(),
}),
});
export function getActivePowerControlSchema(intl: IntlShape, isEquipmentModification = false) {
return {
[FREQUENCY_REGULATION]: yup
.bool()
.nullable()
.when([], {
is: () => !isEquipmentModification,
then: (schema) => schema.required(),
}),
[DROOP]: yup
.number()
.nullable()
.min(0, intl.formatMessage({ id: 'NormalizedPercentage' }))
.max(100, intl.formatMessage({ id: 'NormalizedPercentage' }))
.when([FREQUENCY_REGULATION], {
is: (frequencyRegulation: boolean) => !isEquipmentModification && frequencyRegulation,
then: (schema) => schema.required(),
}),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
NAME,
VOLTAGE_LEVEL,
} from 'components/utils/field-constants';
import yup from '../../utils/yup-config';
import * as yup from 'yup';
import { VoltageLevelFormInfos } from '../network-modifications/voltage-level/voltage-level.type';

export const getConnectivityPropertiesValidationSchema = (isEquipmentModification = false) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import yup from '../../utils/yup-config';
import * as yup from 'yup';
import type { InferType } from 'yup';
import { useCallback, useEffect, useState } from 'react';
import { Dialog, DialogActions, DialogContent, DialogTitle } from '@mui/material';
import Typography from '@mui/material/Typography';
Expand All @@ -31,7 +32,7 @@ const formSchema = yup.object().shape({
[MAPPING]: yup.string().required(),
});

type MappingFormData = yup.InferType<typeof formSchema>;
type MappingFormData = InferType<typeof formSchema>;

const emptyFormData: MappingFormData = {
[MAPPING]: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { FORM_LOADING_DELAY } from '../../../network/constants';
import { DialogProps } from '@mui/material/Dialog/Dialog';
import { DynamicSimulationEventForm } from './dynamic-simulation-event-form';
import { Event, EventProperty, EventPropertyName, PrimitiveTypes } from './types/event.type';
import yup from 'components/utils/yup-config';
import * as yup from 'yup';
import { getSchema } from './util/event-yup';
import { eventDefinitions, getEventType } from './model/event.model';
import { fetchDynamicSimulationEvent, saveDynamicSimulationEvent } from '../../../../services/dynamic-simulation';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { EventPropertyDefinition, PrimitiveTypes } from '../types/event.type';
import { Schema } from 'yup';
import yup from '../../../../utils/yup-config';
import type { Schema } from 'yup';
import * as yup from 'yup';

export const getSchema = (eventPropertyDefinition: EventPropertyDefinition) => {
let schema: Schema;
Expand Down
139 changes: 59 additions & 80 deletions src/components/dialogs/limits/limits-pane-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { type IntlShape } from 'react-intl';
import { sanitizeString } from '../dialog-utils';
import {
CURRENT_LIMITS,
Expand All @@ -24,15 +25,17 @@ import {
TEMPORARY_LIMITS,
} from 'components/utils/field-constants';
import { areArrayElementsUnique, formatTemporaryLimits } from 'components/utils/utils';
import yup from 'components/utils/yup-config';
import * as yup from 'yup';
import { isNodeBuilt } from '../../graph/util/model-functions';
import { OperationalLimitsGroup, TemporaryLimit } from '../../../services/network-modification-types';
import { CurrentTreeNode } from '../../graph/tree-node.type';
import type { OperationalLimitsGroup, TemporaryLimit } from '../../../services/network-modification-types';
import type { CurrentTreeNode } from '../../graph/tree-node.type';

const limitsGroupValidationSchema = (isModification: boolean) => ({
[ID]: yup.string().nonNullable().required(),
[CURRENT_LIMITS]: yup.object().shape(currentLimitsValidationSchema(isModification)),
});
function limitsGroupValidationSchema(intl: IntlShape, isModification: boolean) {
return {
[ID]: yup.string().nonNullable().required(),
[CURRENT_LIMITS]: yup.object().shape(currentLimitsValidationSchema(intl, isModification)),
};
}

const temporaryLimitsValidationSchema = () => {
return yup.object().shape({
Expand All @@ -48,52 +51,56 @@ const temporaryLimitsValidationSchema = () => {
});
};

const currentLimitsValidationSchema = (isModification = false) => ({
[PERMANENT_LIMIT]: yup
.number()
.nullable()
.positive('permanentCurrentLimitMustBeGreaterThanZero')
// if there are valid (named) temporary limits, permanent limit is mandatory
.when([TEMPORARY_LIMITS], {
is: (temporaryLimits: TemporaryLimit[]) =>
temporaryLimits?.length > 0 && temporaryLimits.find((limit) => limit.name) && !isModification,
then: () =>
yup
.number()
.required('permanentCurrentLimitMandatory')
.positive('permanentCurrentLimitMustBeGreaterThanZero'),
}),
[TEMPORARY_LIMITS]: yup
.array()
.of(temporaryLimitsValidationSchema())
.test('distinctNames', 'TemporaryLimitNameUnicityError', (array) => {
const namesArray = !array
? []
: array.filter((l) => !!l[TEMPORARY_LIMIT_NAME]).map((l) => sanitizeString(l[TEMPORARY_LIMIT_NAME]));
return areArrayElementsUnique(namesArray);
})
.test('distinctDurations', 'TemporaryLimitDurationUnicityError', (array) => {
const durationsArray = !array ? [] : array.map((l) => l[TEMPORARY_LIMIT_DURATION]).filter((d) => d); // empty lines are ignored
return areArrayElementsUnique(durationsArray);
}),
});
function currentLimitsValidationSchema(intl: IntlShape, isModification = false) {
return {
[PERMANENT_LIMIT]: yup
.number()
.nullable()
.positive(intl.formatMessage({ id: 'permanentCurrentLimitMustBeGreaterThanZero' }))
// if there are valid (named) temporary limits, permanent limit is mandatory
.when([TEMPORARY_LIMITS], {
is: (temporaryLimits: TemporaryLimit[]) =>
temporaryLimits?.length > 0 && temporaryLimits.find((limit) => limit.name) && !isModification,
then: () =>
yup
.number()
.required(intl.formatMessage({ id: 'permanentCurrentLimitMandatory' }))
.positive(intl.formatMessage({ id: 'permanentCurrentLimitMustBeGreaterThanZero' })),
}),
[TEMPORARY_LIMITS]: yup
.array()
.of(temporaryLimitsValidationSchema())
.test('distinctNames', intl.formatMessage({ id: 'TemporaryLimitNameUnicityError' }), (array) => {
const namesArray = !array
? []
: array
.filter((l) => !!l[TEMPORARY_LIMIT_NAME])
.map((l) => sanitizeString(l[TEMPORARY_LIMIT_NAME]));
return areArrayElementsUnique(namesArray);
})
.test('distinctDurations', intl.formatMessage({ id: 'TemporaryLimitDurationUnicityError' }), (array) => {
const durationsArray = !array ? [] : array.map((l) => l[TEMPORARY_LIMIT_DURATION]).filter((d) => d); // empty lines are ignored
return areArrayElementsUnique(durationsArray);
}),
};
}

const limitsValidationSchema = (id: string, isModification: boolean = false) => {
export function getLimitsValidationSchema(intl: IntlShape, isModification: boolean = false, id: string = LIMITS) {
const selectedCurrentLimitsSchema = {
[CURRENT_LIMITS_1]: yup.object().shape(currentLimitsValidationSchema(isModification)),
[CURRENT_LIMITS_2]: yup.object().shape(currentLimitsValidationSchema(isModification)),
[CURRENT_LIMITS_1]: yup.object().shape(currentLimitsValidationSchema(intl, isModification)),
[CURRENT_LIMITS_2]: yup.object().shape(currentLimitsValidationSchema(intl, isModification)),
};

const completeLimitsGroupSchema = {
[OPERATIONAL_LIMITS_GROUPS_1]: yup
.array(yup.object().shape(limitsGroupValidationSchema(isModification)))
.test('distinctNames', 'LimitSetCreationDuplicateError', (array) => {
.array(yup.object().shape(limitsGroupValidationSchema(intl, isModification)))
.test('distinctNames', intl.formatMessage({ id: 'LimitSetCreationDuplicateError' }), (array) => {
const namesArray = !array ? [] : array.filter((o) => !!o[ID]).map((o) => sanitizeString(o[ID]));
return areArrayElementsUnique(namesArray);
}),
[OPERATIONAL_LIMITS_GROUPS_2]: yup
.array(yup.object().shape(limitsGroupValidationSchema(isModification)))
.test('distinctNames', 'LimitSetCreationDuplicateError', (array) => {
.array(yup.object().shape(limitsGroupValidationSchema(intl, isModification)))
.test('distinctNames', intl.formatMessage({ id: 'LimitSetCreationDuplicateError' }), (array) => {
const namesArray = !array ? [] : array.filter((o) => !!o[ID]).map((o) => sanitizeString(o[ID]));
return areArrayElementsUnique(namesArray);
}),
Expand All @@ -103,13 +110,9 @@ const limitsValidationSchema = (id: string, isModification: boolean = false) =>
// for now modifications only use the selected limits set while the creations use complete limits sets
// => this is temporary and will be removed once the modification use complete limit sets
return { [id]: yup.object().shape(isModification ? selectedCurrentLimitsSchema : completeLimitsGroupSchema) };
};

export const getLimitsValidationSchema = (isModification: boolean = false, id: string = LIMITS) => {
return limitsValidationSchema(id, isModification);
};
}

const limitsEmptyFormData = (id: string, onlySelectedLimits = true) => {
export const getLimitsEmptyFormData = (onlySelectedLimits = true, id = LIMITS) => {
const currentLimits = {
[CURRENT_LIMITS_1]: {
[PERMANENT_LIMIT]: null,
Expand All @@ -130,10 +133,6 @@ const limitsEmptyFormData = (id: string, onlySelectedLimits = true) => {
return { [id]: onlySelectedLimits ? currentLimits : limitsGroup };
};

export const getLimitsEmptyFormData = (onlySelectedLimits = true, id = LIMITS) => {
return limitsEmptyFormData(id, onlySelectedLimits);
};

/**
* used when the limit set data only contain the selected limit sets
*/
Expand Down Expand Up @@ -216,9 +215,7 @@ export const updateTemporaryLimits = (
//add temporary limits from previous modifications
temporaryLimitsToModify?.forEach((limit: TemporaryLimit) => {
if (findTemporaryLimit(updatedTemporaryLimits, limit) === undefined) {
updatedTemporaryLimits?.push({
...limit,
});
updatedTemporaryLimits?.push({ ...limit });
}
});

Expand Down Expand Up @@ -262,35 +259,20 @@ export const addModificationTypeToTemporaryLimits = (
isNodeBuilt(currentNode)) ||
currentLimitWithSameName?.modificationType === TEMPORARY_LIMIT_MODIFICATION_TYPE.ADDED
) {
return {
...limit,
modificationType: currentLimitWithSameName.modificationType,
};
return { ...limit, modificationType: currentLimitWithSameName.modificationType };
} else {
return limitWithSameName.value === limit.value
? {
...limit,
modificationType: null,
}
: {
...limit,
modificationType: TEMPORARY_LIMIT_MODIFICATION_TYPE.MODIFIED,
};
? { ...limit, modificationType: null }
: { ...limit, modificationType: TEMPORARY_LIMIT_MODIFICATION_TYPE.MODIFIED };
}
} else {
return {
...limit,
modificationType: TEMPORARY_LIMIT_MODIFICATION_TYPE.ADDED,
};
return { ...limit, modificationType: TEMPORARY_LIMIT_MODIFICATION_TYPE.ADDED };
}
});
//add deleted limits
formattedTemporaryLimitsToModify?.forEach((limit) => {
if (!findTemporaryLimit(temporaryLimits, limit)) {
updatedTemporaryLimits.push({
...limit,
modificationType: TEMPORARY_LIMIT_MODIFICATION_TYPE.DELETED,
});
updatedTemporaryLimits.push({ ...limit, modificationType: TEMPORARY_LIMIT_MODIFICATION_TYPE.DELETED });
}
});
//add previously deleted limits
Expand All @@ -299,10 +281,7 @@ export const addModificationTypeToTemporaryLimits = (
!findTemporaryLimit(updatedTemporaryLimits, limit) &&
limit.modificationType === TEMPORARY_LIMIT_MODIFICATION_TYPE.DELETED
) {
updatedTemporaryLimits.push({
...limit,
modificationType: TEMPORARY_LIMIT_MODIFICATION_TYPE.DELETED,
});
updatedTemporaryLimits.push({ ...limit, modificationType: TEMPORARY_LIMIT_MODIFICATION_TYPE.DELETED });
}
});
return updatedTemporaryLimits;
Expand Down
Loading