Skip to content

Commit 9c536be

Browse files
store and retrieve csv filename in tabular creation/modification (#3078)
Signed-off-by: Franck LECUYER <[email protected]>
1 parent 4f733d9 commit 9c536be

File tree

10 files changed

+75
-31
lines changed

10 files changed

+75
-31
lines changed

src/components/dialogs/network-modifications/limit-sets/limit-sets-modification-dialog.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
7-
import { AMOUNT_TEMPORARY_LIMITS, MODIFICATIONS_TABLE, SIDE, TYPE } from '../../../utils/field-constants';
7+
import { AMOUNT_TEMPORARY_LIMITS, CSV_FILENAME, MODIFICATIONS_TABLE, SIDE, TYPE } from '../../../utils/field-constants';
88
import { useIntl } from 'react-intl';
99
import { CustomFormProvider, ModificationType, useSnackMessage } from '@gridsuite/commons-ui';
1010
import { SubmitHandler, useForm } from 'react-hook-form';
@@ -106,7 +106,8 @@ export function LimitSetsModificationDialog({
106106
modificationType,
107107
modifications,
108108
editData?.uuid,
109-
ModificationType.LIMIT_SETS_TABULAR_MODIFICATION
109+
ModificationType.LIMIT_SETS_TABULAR_MODIFICATION,
110+
formData[CSV_FILENAME]
110111
).catch((error) => {
111112
snackError({
112113
messageTxt: error.message,

src/components/dialogs/network-modifications/limit-sets/limit-sets-tabular-modification-form.tsx

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import {
1616
IntegerInput,
1717
LANG_FRENCH,
1818
} from '@gridsuite/commons-ui';
19-
import { AMOUNT_TEMPORARY_LIMITS, EQUIPMENT_ID, MODIFICATIONS_TABLE, TYPE } from 'components/utils/field-constants';
19+
import {
20+
AMOUNT_TEMPORARY_LIMITS,
21+
EQUIPMENT_ID,
22+
CSV_FILENAME,
23+
MODIFICATIONS_TABLE,
24+
TYPE,
25+
} from 'components/utils/field-constants';
2026
import { EQUIPMENT_TYPES } from 'components/utils/equipment-types';
2127
import CsvDownloader from 'react-csv-downloader';
2228
import { Alert, Button, Grid } from '@mui/material';
@@ -58,6 +64,15 @@ export function LimitSetsTabularModificationForm({ dataFetching }: Readonly<Tabu
5864
return [...LIMIT_SETS_TABULAR_MODIFICATION_FIXED_FIELDS, ...repeatableColumns];
5965
}, [repeatableColumns]);
6066

67+
const [typeChangedTrigger, setTypeChangedTrigger] = useState(false);
68+
const [selectedFile, FileField, selectedFileError, setAcceptedFile] = useCSVPicker({
69+
label: 'ImportModifications',
70+
header: csvColumns.map((column) => column.id),
71+
disabled: !csvColumns,
72+
resetTrigger: typeChangedTrigger,
73+
language: language,
74+
});
75+
6176
const handleComplete = useCallback(
6277
(results: Papa.ParseResult<any>) => {
6378
clearErrors(MODIFICATIONS_TABLE);
@@ -118,23 +133,16 @@ export function LimitSetsTabularModificationForm({ dataFetching }: Readonly<Tabu
118133
setValue(MODIFICATIONS_TABLE, results.data, {
119134
shouldDirty: true,
120135
});
136+
setValue(CSV_FILENAME, selectedFile?.name);
121137
setIsFetching(false);
122138
},
123-
[clearErrors, csvColumns, intl, setError, setValue]
139+
[clearErrors, csvColumns, intl, setError, setValue, selectedFile]
124140
);
125141

126142
const amountTemporaryLimits = useWatch({
127143
name: AMOUNT_TEMPORARY_LIMITS,
128144
});
129145

130-
const equipmentType = useWatch({
131-
name: TYPE,
132-
});
133-
134-
const watchTable = useWatch({
135-
name: MODIFICATIONS_TABLE,
136-
});
137-
138146
const computeRepeatableColumns = useCallback(() => {
139147
const columns: TabularField[] = [];
140148
trigger(AMOUNT_TEMPORARY_LIMITS).then((valid) => {
@@ -176,25 +184,27 @@ export function LimitSetsTabularModificationForm({ dataFetching }: Readonly<Tabu
176184
return commentData;
177185
}, [intl, csvTranslatedColumns, language]);
178186

179-
const [typeChangedTrigger, setTypeChangedTrigger] = useState(false);
180-
const [selectedFile, FileField, selectedFileError] = useCSVPicker({
181-
label: 'ImportModifications',
182-
header: csvColumns.map((column) => column.id),
183-
disabled: !csvColumns || !equipmentType,
184-
resetTrigger: typeChangedTrigger,
185-
language: language,
186-
});
187-
188187
const handleChange = useCallback(() => {
189188
setTypeChangedTrigger(!typeChangedTrigger);
190189
clearErrors(MODIFICATIONS_TABLE);
191190
setValue(MODIFICATIONS_TABLE, []);
191+
setValue(CSV_FILENAME, undefined);
192192
}, [clearErrors, setValue, typeChangedTrigger]);
193193

194+
const watchTable = useWatch({
195+
name: MODIFICATIONS_TABLE,
196+
});
197+
198+
const csvFilename = getValues(CSV_FILENAME);
199+
194200
useEffect(() => {
195201
computeRepeatableColumns();
196202
}, [computeRepeatableColumns]);
197203

204+
useEffect(() => {
205+
setAcceptedFile(csvFilename ? new File([], csvFilename) : undefined);
206+
}, [setAcceptedFile, csvFilename]);
207+
198208
useEffect(() => {
199209
setIsFetching(dataFetching);
200210
}, [dataFetching]);
@@ -208,9 +218,10 @@ export function LimitSetsTabularModificationForm({ dataFetching }: Readonly<Tabu
208218
useEffect(() => {
209219
if (selectedFileError) {
210220
setValue(MODIFICATIONS_TABLE, []);
221+
setValue(CSV_FILENAME, undefined);
211222
clearErrors(MODIFICATIONS_TABLE);
212223
setIsFetching(false);
213-
} else if (selectedFile) {
224+
} else if (selectedFile && selectedFile.size > 0) {
214225
setIsFetching(true);
215226
Papa.parse(selectedFile, {
216227
header: true,

src/components/dialogs/network-modifications/limit-sets/limit-sets-tabular-modification-utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
import {
88
AMOUNT_TEMPORARY_LIMITS,
9+
CSV_FILENAME,
910
EQUIPMENT_ID,
1011
LIMIT_GROUP_NAME,
1112
MODIFICATION_TYPE,
@@ -63,6 +64,7 @@ export type LimitSetModificationMetadata = {
6364
stashed: boolean;
6465
type: string;
6566
uuid: UUID;
67+
csvFilename: string;
6668
};
6769

6870
const getAmountTemporaryLimits = (editData: LimitSetModificationMetadata) => {
@@ -135,6 +137,7 @@ export const formatBackToFront = (editData: LimitSetModificationMetadata) => {
135137
[TYPE]: getEquipmentTypeFromLimitSetModificationType(editData.modificationType),
136138
[AMOUNT_TEMPORARY_LIMITS]: getAmountTemporaryLimits(editData),
137139
[MODIFICATIONS_TABLE]: operationalLimitGroups,
140+
[CSV_FILENAME]: editData.csvFilename,
138141
};
139142
};
140143

@@ -176,6 +179,7 @@ export const formSchema = yup
176179
[TYPE]: yup.string().nullable().required(),
177180
[AMOUNT_TEMPORARY_LIMITS]: yup.number().positive().max(50).required(),
178181
[MODIFICATIONS_TABLE]: yup.array().min(1, 'ModificationsRequiredTabError').required(),
182+
[CSV_FILENAME]: yup.string().nullable().required(),
179183
})
180184
.required();
181185
export type SchemaType = yup.InferType<typeof formSchema>;
@@ -185,4 +189,5 @@ export const emptyFormData: SchemaType = {
185189
[TYPE]: EQUIPMENT_TYPES.LINE,
186190
[AMOUNT_TEMPORARY_LIMITS]: 1,
187191
[MODIFICATIONS_TABLE]: [],
192+
[CSV_FILENAME]: '',
188193
};

src/components/dialogs/network-modifications/tabular/creation/tabular-creation-dialog.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8+
import { ModificationDialog } from 'components/dialogs/commons/modificationDialog.js';
89
import { yupResolver } from '@hookform/resolvers/yup';
910
import { CustomFormProvider, useSnackMessage } from '@gridsuite/commons-ui';
1011
import { useForm } from 'react-hook-form';
1112
import { useCallback, useEffect, useMemo } from 'react';
1213
import PropTypes from 'prop-types';
1314
import { useOpenShortWaitFetching } from 'components/dialogs/commons/handle-modification-form.js';
1415
import { FORM_LOADING_DELAY } from 'components/network/constants.js';
15-
import { MODIFICATIONS_TABLE, TABULAR_PROPERTIES, TYPE } from 'components/utils/field-constants.js';
16-
import { ModificationDialog } from 'components/dialogs/commons/modificationDialog.js';
16+
import { MODIFICATIONS_TABLE, CSV_FILENAME, TABULAR_PROPERTIES, TYPE } from 'components/utils/field-constants.js';
1717
import { createTabularCreation } from 'services/study/network-modifications.js';
1818
import { FetchStatus } from 'services/utils.js';
1919
import {
@@ -80,6 +80,7 @@ const TabularCreationDialog = ({ studyUuid, currentNode, editData, isUpdate, edi
8080
[TYPE]: equipmentType,
8181
[MODIFICATIONS_TABLE]: creations,
8282
[TABULAR_PROPERTIES]: editData[TABULAR_PROPERTIES],
83+
[CSV_FILENAME]: editData.csvFilename,
8384
});
8485
}
8586
}, [editData, reset, intl]);
@@ -116,7 +117,8 @@ const TabularCreationDialog = ({ studyUuid, currentNode, editData, isUpdate, edi
116117
creations,
117118
formData[TABULAR_PROPERTIES],
118119
!!editData,
119-
editData?.uuid
120+
editData?.uuid,
121+
formData[CSV_FILENAME]
120122
).catch((error) => {
121123
snackError({
122124
messageTxt: error.message,

src/components/dialogs/network-modifications/tabular/modification/tabular-modification-dialog.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { useCallback, useEffect, useMemo } from 'react';
1212
import PropTypes from 'prop-types';
1313
import { useOpenShortWaitFetching } from 'components/dialogs/commons/handle-modification-form.js';
1414
import { FORM_LOADING_DELAY } from 'components/network/constants.js';
15-
import { TABULAR_PROPERTIES, MODIFICATIONS_TABLE, TYPE } from 'components/utils/field-constants.js';
15+
import { TABULAR_PROPERTIES, MODIFICATIONS_TABLE, CSV_FILENAME, TYPE } from 'components/utils/field-constants.js';
1616
import { ModificationDialog } from 'components/dialogs/commons/modificationDialog.js';
1717
import { createTabularModification } from 'services/study/network-modifications.js';
1818
import { FetchStatus } from 'services/utils.js';
@@ -91,6 +91,7 @@ const TabularModificationDialog = ({
9191
[TYPE]: getEquipmentTypeFromModificationType(modificationType),
9292
[MODIFICATIONS_TABLE]: modifications,
9393
[TABULAR_PROPERTIES]: editData[TABULAR_PROPERTIES],
94+
[CSV_FILENAME]: editData.csvFilename,
9495
});
9596
}
9697
}, [editData, reset, intl]);
@@ -110,6 +111,7 @@ const TabularModificationDialog = ({
110111
modifications,
111112
editData?.uuid,
112113
ModificationType.TABULAR_MODIFICATION,
114+
formData[CSV_FILENAME],
113115
formData[TABULAR_PROPERTIES]
114116
).catch((error) => {
115117
snackError({

src/components/dialogs/network-modifications/tabular/tabular-common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ReactiveCapabilityCurvePoints } from 'components/dialogs/reactive-limit
88
import { createPropertyModification, Property } from '../common/properties/property-utils';
99
import { propertiesSchema, PROPERTY_CSV_COLUMN_PREFIX } from './properties/property-utils';
1010
import {
11+
CSV_FILENAME,
1112
MODIFICATIONS_TABLE,
1213
REACTIVE_CAPABILITY_CURVE,
1314
REACTIVE_CAPABILITY_CURVE_P_0,
@@ -43,6 +44,7 @@ export const tabularFormSchema = yup
4344
.shape({
4445
[TYPE]: yup.string().nullable().required(),
4546
[MODIFICATIONS_TABLE]: yup.array().min(1, 'ModificationsRequiredTabError').required(),
47+
[CSV_FILENAME]: yup.string().nullable().required(),
4648
})
4749
.concat(propertiesSchema)
4850
.required();
@@ -51,6 +53,7 @@ export const emptyTabularFormData = {
5153
[TYPE]: null,
5254
[MODIFICATIONS_TABLE]: [],
5355
[TABULAR_PROPERTIES]: [],
56+
[CSV_FILENAME]: undefined,
5457
};
5558

5659
export interface Modification {

src/components/dialogs/network-modifications/tabular/tabular-form.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ import {
1818
useSnackMessage,
1919
useStateBoolean,
2020
} from '@gridsuite/commons-ui';
21-
import { EQUIPMENT_ID, MODIFICATIONS_TABLE, TABULAR_PROPERTIES, TYPE } from 'components/utils/field-constants';
21+
import {
22+
EQUIPMENT_ID,
23+
MODIFICATIONS_TABLE,
24+
CSV_FILENAME,
25+
TABULAR_PROPERTIES,
26+
TYPE,
27+
} from 'components/utils/field-constants';
2228
import { EQUIPMENT_TYPES } from 'components/utils/equipment-types';
2329
import CsvDownloader from 'react-csv-downloader';
2430
import { Alert, Button, Grid } from '@mui/material';
@@ -248,7 +254,7 @@ export function TabularForm({ dataFetching, dialogMode }: Readonly<TabularFormPr
248254
}, [csvFields, selectedProperties, intl, equipmentType, language, dialogMode, predefinedEquipmentProperties]);
249255

250256
const [typeChangedTrigger, setTypeChangedTrigger] = useState(false);
251-
const [selectedFile, FileField, selectedFileError, resetFile] = useCSVPicker({
257+
const [selectedFile, FileField, selectedFileError, setAcceptedFile, resetFile] = useCSVPicker({
252258
label: dialogMode === TabularModificationType.CREATION ? 'ImportCreations' : 'ImportModifications',
253259
header: csvColumns,
254260
disabled: !csvColumns?.length,
@@ -267,9 +273,11 @@ export function TabularForm({ dataFetching, dialogMode }: Readonly<TabularFormPr
267273
handleTabularModificationParsingError(results);
268274
}
269275
setValue(MODIFICATIONS_TABLE, results.data, { shouldDirty: true });
276+
setValue(CSV_FILENAME, selectedFile?.name);
270277
} else {
271278
// If the file is undefined we don't update the values because it's outdated
272279
setValue(MODIFICATIONS_TABLE, []);
280+
setValue(CSV_FILENAME, undefined);
273281
}
274282
setIsFetching(false);
275283
},
@@ -289,16 +297,21 @@ export function TabularForm({ dataFetching, dialogMode }: Readonly<TabularFormPr
289297
});
290298
}, []);
291299

300+
useEffect(() => {
301+
setAcceptedFile(getValues(CSV_FILENAME) ? new File([], getValues(CSV_FILENAME)) : undefined);
302+
}, [setAcceptedFile, getValues]);
303+
292304
useEffect(() => {
293305
setIsFetching(dataFetching);
294306
}, [dataFetching]);
295307

296308
useEffect(() => {
297309
if (selectedFileError) {
298310
setValue(MODIFICATIONS_TABLE, []);
311+
setValue(CSV_FILENAME, undefined);
299312
clearErrors(MODIFICATIONS_TABLE);
300313
setIsFetching(false);
301-
} else if (selectedFile) {
314+
} else if (selectedFile && selectedFile.size > 0) {
302315
setIsFetching(true);
303316
// @ts-ignore
304317
Papa.parse(selectedFile as unknown as File, {
@@ -323,6 +336,7 @@ export function TabularForm({ dataFetching, dialogMode }: Readonly<TabularFormPr
323336
setTypeChangedTrigger(!typeChangedTrigger);
324337
clearErrors(MODIFICATIONS_TABLE);
325338
setValue(MODIFICATIONS_TABLE, []);
339+
setValue(CSV_FILENAME, undefined);
326340
setValue(TABULAR_PROPERTIES, []);
327341
resetFile();
328342
}, [clearErrors, setValue, typeChangedTrigger, resetFile]);
@@ -383,6 +397,7 @@ export function TabularForm({ dataFetching, dialogMode }: Readonly<TabularFormPr
383397
// new columns => reset table
384398
clearErrors(MODIFICATIONS_TABLE);
385399
setValue(MODIFICATIONS_TABLE, []);
400+
setValue(CSV_FILENAME, undefined);
386401
}
387402
setValue(TABULAR_PROPERTIES, formData[TABULAR_PROPERTIES], { shouldDirty: true });
388403
};

src/components/utils/field-constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export const TEMPORARY_LIMITS_MODIFICATION_TYPE = 'temporaryLimitsModificationTy
6060
export const SIDE = 'side';
6161
export const LIMIT_GROUP_NAME = 'limitGroupName';
6262
export const MODIFICATIONS_TABLE = 'modificationsTable';
63+
export const CSV_FILENAME = 'csvFilename';
6364

6465
export const CHARACTERISTICS = 'characteristics';
6566
export const R = 'r';

src/components/utils/inputs/input-hooks.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,5 @@ export const useCSVPicker = ({
162162
);
163163
}, [_acceptedFile, disabled, header, intl, label, maxTapNumber, CSVReader, language]);
164164

165-
return [_acceptedFile, field, fileError, resetFile] as const;
165+
return [_acceptedFile, field, fileError, setAcceptedFile, resetFile] as const;
166166
};

src/services/study/network-modifications.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,7 @@ export function createTabularModification(
10951095
modifications: any,
10961096
modificationUuid: UUID,
10971097
type: ModificationType,
1098+
csvFilename: string,
10981099
properties?: TabularProperty[]
10991100
) {
11001101
let createTabularModificationUrl = getNetworkModificationUrl(studyUuid, nodeUuid);
@@ -1117,6 +1118,7 @@ export function createTabularModification(
11171118
modificationType: modificationType,
11181119
modifications: modifications,
11191120
properties: properties,
1121+
csvFilename: csvFilename,
11201122
}),
11211123
});
11221124
}
@@ -2001,7 +2003,8 @@ export function createTabularCreation(
20012003
creations: any,
20022004
properties: TabularProperty[],
20032005
isUpdate: boolean,
2004-
modificationUuid: UUID
2006+
modificationUuid: UUID,
2007+
csvFilename: string
20052008
) {
20062009
let createTabularCreationUrl = getNetworkModificationUrl(studyUuid, nodeUuid);
20072010

@@ -2023,6 +2026,7 @@ export function createTabularCreation(
20232026
creationType: creationType,
20242027
creations: creations,
20252028
properties: properties,
2029+
csvFilename: csvFilename,
20262030
}),
20272031
});
20282032
}

0 commit comments

Comments
 (0)