Skip to content

Commit 34f8df0

Browse files
committed
fix: [PROD-16012] fix dynamic table state sometimes stuck to loading
1 parent 5baeca6 commit 34f8df0

1 file changed

Lines changed: 31 additions & 33 deletions

File tree

  • src/components/ScenarioParameters/components/ScenarioParametersInputs

src/components/ScenarioParameters/components/ScenarioParametersInputs/GenericTable.jsx

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -223,31 +223,28 @@ export const GenericTable = ({
223223

224224
const isDataFetchedFromDataset = !!ConfigUtils.getParameterAttribute(parameterData, 'dynamicValues');
225225

226-
const _getDataFromDatasetPart = async (setClientFileDescriptor) => {
226+
const _getDataFromDatasetPart = async (setClientFileDescriptor, parameter) => {
227+
if (_checkForLock()) return;
228+
GenericTable.downloadLocked[lockId] = true;
229+
227230
const fileName = `${parameterData.id}.csv`;
228-
const _setClientFileDescriptorToError = () => {
229-
setClientFileDescriptor({
230-
value: null,
231-
serializedData: null,
232-
displayData: null,
233-
errors: null,
234-
displayStatus: TABLE_DATA_STATUS.ERROR,
235-
});
231+
const noFileDescriptor = { value: null, serializedData: null, displayData: null, errors: null };
232+
const _setErrorStatusAndFreeLock = () => {
233+
GenericTable.downloadLocked[lockId] = false;
234+
235+
// If the current status was already an error, add a short timeout to maintain the loading status and give visual
236+
// feedback to users
237+
const timeoutDelayInMs = parameter?.displayStatus === TABLE_DATA_STATUS.ERROR ? 100 : null;
238+
if (!timeoutDelayInMs) setClientFileDescriptor({ ...noFileDescriptor, displayStatus: TABLE_DATA_STATUS.ERROR });
239+
else {
240+
setTimeout(function () {
241+
setClientFileDescriptor({ ...noFileDescriptor, displayStatus: TABLE_DATA_STATUS.ERROR });
242+
}, timeoutDelayInMs);
243+
}
236244
};
237245

238-
setClientFileDescriptor({
239-
value: null,
240-
serializedData: null,
241-
displayData: null,
242-
errors: null,
243-
displayStatus: TABLE_DATA_STATUS.DOWNLOADING,
244-
});
245-
if (_checkForLock()) {
246-
return;
247-
}
248-
GenericTable.downloadLocked[lockId] = true;
246+
setClientFileDescriptor({ ...noFileDescriptor, displayStatus: TABLE_DATA_STATUS.DOWNLOADING });
249247
const sourceDataset = context.targetDataset;
250-
251248
if (!sourceDataset) {
252249
setPlaceholder({
253250
title: t('commoncomponents.banner.missingDataset', 'Dataset not found'),
@@ -256,7 +253,7 @@ export const GenericTable = ({
256253
'Impossible to fetch data from dataset because it does not exist or you do not have access to it. '
257254
),
258255
});
259-
_setClientFileDescriptorToError();
256+
_setErrorStatusAndFreeLock();
260257
return;
261258
}
262259

@@ -268,7 +265,7 @@ export const GenericTable = ({
268265
'Only datasets with parts of type "DB" can be used to fetch table data dynamically'
269266
),
270267
});
271-
_setClientFileDescriptorToError();
268+
_setErrorStatusAndFreeLock();
272269
return;
273270
}
274271

@@ -285,7 +282,7 @@ export const GenericTable = ({
285282
{ datasetName: sourceDataset?.name, datasetPartName }
286283
),
287284
});
288-
_setClientFileDescriptorToError();
285+
_setErrorStatusAndFreeLock();
289286
return;
290287
}
291288

@@ -302,7 +299,7 @@ export const GenericTable = ({
302299
),
303300
});
304301
console.error(error?.response?.data?.detail);
305-
_setClientFileDescriptorToError();
302+
_setErrorStatusAndFreeLock();
306303
return;
307304
}
308305

@@ -315,7 +312,7 @@ export const GenericTable = ({
315312
'Please, check your solution'
316313
),
317314
});
318-
_setClientFileDescriptorToError();
315+
_setErrorStatusAndFreeLock();
319316
return;
320317
}
321318

@@ -326,7 +323,6 @@ export const GenericTable = ({
326323
errors: agGridData.error,
327324
});
328325
} else {
329-
// FIXME import & use forgeFileParameter
330326
setClientFileDescriptor({
331327
name: fileName,
332328
value: null,
@@ -633,7 +629,7 @@ export const GenericTable = ({
633629
parameter.status === UPLOAD_FILE_STATUS_KEY.EMPTY &&
634630
!alreadyDownloaded
635631
) {
636-
_getDataFromDatasetPart(updateParameterValueWithReset);
632+
_getDataFromDatasetPart(updateParameterValueWithReset, parameter);
637633
}
638634
});
639635

@@ -711,21 +707,23 @@ export const GenericTable = ({
711707

712708
const revertTableWithDatasetData = useCallback(
713709
(isChecked) => {
714-
localStorage.setItem('dontAskAgainToRevertTableData', isChecked);
710+
localStorage.setItem('dontAskAgainToRevertTableData', JSON.stringify(isChecked));
715711
closeRevertDialog();
716712
// To trigger isDirty state when an already saved table was reverted and avoid it in other cases, we need to
717713
// updateParameterValue setter and updateOnFirstEdition function that triggers the start of edition; on the other
718714
// hand, updateParameterValueWithReset setter rollbacks modified values without triggering isDirty
719-
_getDataFromDatasetPart(parameter.serializedData ? updateParameterValue : updateParameterValueWithReset);
720-
if (parameter.serializedData) updateOnFirstEdition();
715+
if (parameter.serializedData != null) {
716+
_getDataFromDatasetPart(updateParameterValue, parameter);
717+
updateOnFirstEdition();
718+
} else _getDataFromDatasetPart(updateParameterValueWithReset, parameter);
721719
},
722720
// eslint-disable-next-line react-hooks/exhaustive-deps
723721
[parameter.serializedData, updateParameterValue, updateParameterValueWithReset, updateOnFirstEdition]
724722
);
725723

726724
const onRevertTableData = useCallback(() => {
727725
if (localStorage.getItem('dontAskAgainToRevertTableData') !== 'true') setIsRevertDialogOpen(true);
728-
else revertTableWithDatasetData('true');
726+
else revertTableWithDatasetData(true);
729727
}, [setIsRevertDialogOpen, revertTableWithDatasetData]);
730728

731729
return (
@@ -776,7 +774,7 @@ export const GenericTable = ({
776774
/>
777775
<TableRevertDataDialog
778776
onClose={closeRevertDialog}
779-
onConfirm={(isChecked) => revertTableWithDatasetData(isChecked)}
777+
onConfirm={revertTableWithDatasetData}
780778
open={isRevertDialogOpen}
781779
/>
782780
</>

0 commit comments

Comments
 (0)