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
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function LimitSetsModificationDialog({
modificationType,
modifications,
modificationUuid: editData?.uuid,
type: ModificationType.LIMIT_SETS_TABULAR_MODIFICATION,
tabularType: ModificationType.LIMIT_SETS_TABULAR_MODIFICATION,
csvFilename: formData[CSV_FILENAME],
}).catch((error) => {
snackError({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,14 @@ import {
import yup from 'components/utils/yup-config';
import type { UUID } from 'node:crypto';

type TabularModificationCommonType = {
export type TabularModificationEditDataType = {
uuid: UUID;
type: ModificationType.TABULAR_MODIFICATION | ModificationType.TABULAR_CREATION;
properties: TabularProperty[];
csvFilename: string;
};
export type TabularModificationModificationType = TabularModificationCommonType & {
type: 'TABULAR_MODIFICATION';
modificationType: ModificationType;
modifications: Modification[];
};
export type TabularModificationCreationType = TabularModificationCommonType & {
type: 'TABULAR_CREATION';
creationType: ModificationType;
creations: Modification[];
};
export type TabularModificationEditDataType = TabularModificationModificationType | TabularModificationCreationType;

export enum TabularModificationType {
CREATION = 'creation',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useOpenShortWaitFetching } from 'components/dialogs/commons/handle-modi
import { FORM_LOADING_DELAY } from 'components/network/constants.js';
import { TABULAR_PROPERTIES, MODIFICATIONS_TABLE, CSV_FILENAME, TYPE } from 'components/utils/field-constants.js';
import { ModificationDialog } from 'components/dialogs/commons/modificationDialog.js';
import { createTabularCreation, createTabularModification } from 'services/study/network-modifications.js';
import { createTabularModification } from 'services/study/network-modifications.js';
import { FetchStatus } from 'services/utils.type';
import {
convertGeneratorOrBatteryModificationFromBackToFront,
Expand All @@ -32,9 +32,7 @@ import {
Modification,
tabularFormSchema,
TabularFormType,
TabularModificationCreationType,
TabularModificationEditDataType,
TabularModificationModificationType,
TabularModificationType,
transformProperties,
} from './tabular-common.js';
Expand All @@ -51,12 +49,12 @@ import { NetworkModificationDialogProps } from '../../../graph/menus/network-mod
function convertCreations(creations: Modification[]): Modification[] {
return creations.map((creat: Modification) => {
let creation: Modification = {};
Object.keys(formatModification(creat)).forEach((key) => {
for (const key of Object.keys(formatModification(creat))) {
const entry = convertCreationFieldFromBackToFront(key, creat[key]);
(Array.isArray(entry) ? entry : [entry]).forEach((item) => {
for (const item of Array.isArray(entry) ? entry : [entry]) {
creation[item.key] = item.value;
});
});
}
}
creation = addPropertiesFromBack(creation, creat?.[TABULAR_PROPERTIES]);
return creation;
});
Expand Down Expand Up @@ -98,7 +96,7 @@ export function TabularDialog({
const disableSave = Object.keys(errors).length > 0;

const initTabularModificationData = useCallback(
(editData: TabularModificationModificationType) => {
(editData: TabularModificationEditDataType) => {
const modificationType = editData.modificationType;
const modifications = editData.modifications.map((modif: Modification) => {
let modification = formatModification(modif);
Expand All @@ -108,9 +106,9 @@ export function TabularDialog({
) {
modification = convertGeneratorOrBatteryModificationFromBackToFront(modification);
} else {
Object.keys(modification).forEach((key) => {
for (const key of Object.keys(modification)) {
modification[key] = convertInputValues(getFieldType(modificationType, key), modif[key]);
});
}
}
modification = addPropertiesFromBack(modification, modif?.[TABULAR_PROPERTIES]);
return modification;
Expand All @@ -126,9 +124,9 @@ export function TabularDialog({
);

const initTabularCreationData = useCallback(
(editData: TabularModificationCreationType) => {
const equipmentType = getEquipmentTypeFromCreationType(editData?.creationType);
const creations = convertCreations(editData?.creations);
(editData: TabularModificationEditDataType) => {
const equipmentType = getEquipmentTypeFromCreationType(editData?.modificationType);
const creations = convertCreations(editData?.modifications);
reset({
[TYPE]: equipmentType,
[MODIFICATIONS_TABLE]: creations,
Expand All @@ -142,9 +140,9 @@ export function TabularDialog({
useEffect(() => {
if (editData) {
if (dialogMode === TabularModificationType.CREATION) {
initTabularCreationData(editData as TabularModificationCreationType);
initTabularCreationData(editData);
} else {
initTabularModificationData(editData as TabularModificationModificationType);
initTabularModificationData(editData);
}
}
}, [editData, dialogMode, initTabularCreationData, initTabularModificationData]);
Expand All @@ -162,7 +160,7 @@ export function TabularDialog({
modificationType,
modifications,
modificationUuid: editData?.uuid,
type: ModificationType.TABULAR_MODIFICATION,
tabularType: ModificationType.TABULAR_MODIFICATION,
csvFilename: formData[CSV_FILENAME],
properties: formData[TABULAR_PROPERTIES],
}).catch((error) => {
Expand All @@ -177,21 +175,24 @@ export function TabularDialog({

const submitTabularCreation = useCallback(
(formData: TabularFormType) => {
const creationType = TABULAR_CREATION_TYPES[formData[TYPE]];
const creations = formData[MODIFICATIONS_TABLE]?.map((row) => {
const modificationType = TABULAR_CREATION_TYPES[formData[TYPE]];
const modifications = formData[MODIFICATIONS_TABLE]?.map((row) => {
const creation: Modification = {
type: creationType,
type: modificationType,
};
// first transform and clean "property_*" fields
const propertiesModifications = transformProperties(row);

// then transform all other fields
Object.keys(row).forEach((key) => {
for (const key of Object.keys(row)) {
const entry = convertCreationFieldFromFrontToBack(key, row[key]);
creation[entry.key] = entry.value;
});
}
// For now, we do not manage reactive limits by diagram
if (creationType === 'GENERATOR_CREATION' || creationType === 'BATTERY_CREATION') {
if (
modificationType === ModificationType.GENERATOR_CREATION ||
modificationType === ModificationType.BATTERY_CREATION
) {
convertReactiveCapabilityCurvePointsFromFrontToBack(creation);
}

Expand All @@ -200,12 +201,13 @@ export function TabularDialog({
}
return creation;
});
createTabularCreation({
createTabularModification({
studyUuid,
nodeUuid: currentNodeUuid,
creationType,
creations,
modificationType,
modifications,
modificationUuid: editData?.uuid,
tabularType: ModificationType.TABULAR_CREATION,
csvFilename: formData[CSV_FILENAME],
properties: formData[TABULAR_PROPERTIES],
}).catch((error) => {
Expand Down
63 changes: 11 additions & 52 deletions src/services/study/network-modifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,10 @@ export interface CreateTabularModificationProps {
modificationType: string;
modifications: Modification[];
modificationUuid: UUID;
type: ModificationType;
tabularType:
| ModificationType.LIMIT_SETS_TABULAR_MODIFICATION
| ModificationType.TABULAR_MODIFICATION
| ModificationType.TABULAR_CREATION;
csvFilename?: string;
properties?: TabularProperty[];
}
Expand All @@ -1118,27 +1121,27 @@ export function createTabularModification({
modificationType,
modifications,
modificationUuid,
type,
tabularType,
csvFilename,
properties,
}: CreateTabularModificationProps) {
let createTabularModificationUrl = getNetworkModificationUrl(studyUuid, nodeUuid);
let tabularModificationUrl = getNetworkModificationUrl(studyUuid, nodeUuid);
const isUpdate = !!modificationUuid;
if (isUpdate) {
createTabularModificationUrl += '/' + encodeURIComponent(modificationUuid);
console.info('Updating tabular modification');
tabularModificationUrl += '/' + encodeURIComponent(modificationUuid);
console.info('Updating ' + tabularType);
} else {
console.info('Creating tabular modification');
console.info('Creating ' + tabularType);
}

return backendFetchText(createTabularModificationUrl, {
return backendFetchText(tabularModificationUrl, {
method: isUpdate ? 'PUT' : 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
type: type,
type: tabularType,
modificationType: modificationType,
modifications: modifications,
properties: properties,
Expand Down Expand Up @@ -2050,50 +2053,6 @@ export function modifyByAssignment(
});
}

export interface CreateTabularCreationProps {
studyUuid: UUID;
nodeUuid: UUID;
creationType: string;
creations: Modification[];
modificationUuid: UUID;
csvFilename?: string;
properties?: TabularProperty[];
}

export function createTabularCreation({
studyUuid,
nodeUuid,
creationType,
creations,
modificationUuid,
csvFilename,
properties,
}: CreateTabularCreationProps) {
let createTabularCreationUrl = getNetworkModificationUrl(studyUuid, nodeUuid);
const isUpdate = !!modificationUuid;
if (isUpdate) {
createTabularCreationUrl += '/' + encodeURIComponent(modificationUuid);
console.info('Updating tabular creation');
} else {
console.info('Creating tabular creation');
}

return backendFetchText(createTabularCreationUrl, {
method: isUpdate ? 'PUT' : 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
type: MODIFICATION_TYPES.TABULAR_CREATION.type,
creationType: creationType,
creations: creations,
properties: properties,
csvFilename: csvFilename,
}),
});
}

export function createCouplingDevice({
createCouplingDeviceInfos,
studyUuid,
Expand Down
Loading