Skip to content

Commit

Permalink
fix: fix scenario parameters changes loss on file upload
Browse files Browse the repository at this point in the history
Bug was probably caused by writing/reading during render, it seems to be fixed by using
a lazy-initialization pattern

c.f. facebook/react#18545
c.f. https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily
  • Loading branch information
csm-thu committed Jan 6, 2022
1 parent 0fd4375 commit 5c302f4
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/components/ScenarioParameters/ScenarioParameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,34 +101,39 @@ const ScenarioParameters = ({
);

// Store the reset values for the run template parameters, based on defaultParametersValues and scenario data.
const parametersValuesRef = useRef({});
parametersValuesRef.current = parametersValuesForReset;
const parametersValuesRef = useRef(null);
function getParametersValuesRef() {
if (parametersValuesRef.current === null) {
parametersValuesRef.current = parametersValuesForReset;
}
return parametersValuesRef.current;
}

const generateParametersValuesToRenderFromParametersValuesRef = () => {
const newParametersValuesToRender = {};
for (const parameterId in parametersValuesRef.current) {
for (const parameterId in getParametersValuesRef()) {
if (parametersMetadata[parameterId]?.varType === DATASET_ID_VARTYPE) {
const datasetId = parametersValuesRef.current[parameterId];
const datasetId = getParametersValuesRef()[parameterId];
newParametersValuesToRender[parameterId] = FileManagementUtils.buildClientFileDescriptorFromDataset(
datasets,
datasetId
);
} else {
newParametersValuesToRender[parameterId] = parametersValuesRef.current[parameterId];
newParametersValuesToRender[parameterId] = getParametersValuesRef()[parameterId];
}
}
return newParametersValuesToRender;
};

const setParametersValuesToRenderFromParametersValuesRef = () => {
setParametersValuesToRender(generateParametersValuesToRenderFromParametersValuesRef());
};

// Add scenario parameters data in state
const [parametersValuesToRender, setParametersValuesToRender] = useState(
generateParametersValuesToRenderFromParametersValuesRef()
);

const setParametersValuesToRenderFromParametersValuesRef = () => {
setParametersValuesToRender(generateParametersValuesToRenderFromParametersValuesRef());
};

// Generate input components for each scenario parameters tab
for (const parametersGroupMetadata of parametersGroupsMetadata) {
parametersGroupMetadata.tab = ScenarioParametersTabFactory.create(
Expand All @@ -154,10 +159,9 @@ const ScenarioParameters = ({
}
}
parametersValuesRef.current = {
...parametersValuesRef.current,
...getParametersValuesRef(),
...newParametersValuesToPatch,
};

await FileManagementUtils.applyPendingOperationsOnFileParameters(
solution,
parametersMetadata,
Expand All @@ -174,14 +178,15 @@ const ScenarioParameters = ({
}, [parametersValuesRef]);

useEffect(() => {
parametersValuesRef.current = parametersValuesForReset;
discardLocalChanges();
// eslint-disable-next-line
}, [currentScenario]);

const getParametersForUpdate = () => {
const parametersData = ScenarioParametersUtils.buildParametersForUpdate(
solution,
parametersValuesRef.current,
getParametersValuesRef(),
runTemplateParametersIds
);
const additionalParameters = ScenarioParametersUtils.buildAdditionalParameters(currentScenario, scenarioList);
Expand Down

0 comments on commit 5c302f4

Please sign in to comment.