Skip to content

Commit

Permalink
front: fix incorrect input saving when switching simulations during a…
Browse files Browse the repository at this point in the history
… running calculation

When a different simulation is selected while a calculation is still running,
the new simulation’s inputs overwrite the inputs of the ongoing calculation.
This causes a mismatch between the inputs used for the calculation and the results.

Now, the inputs for the simulation being calculated are stored in a different
state variable than the one used for the selected simulation.

Signed-off-by: nncluzu <[email protected]>
  • Loading branch information
kmer2016 committed Jan 23, 2025
1 parent 23a21ce commit 8016bc4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ declare global {
type StdcmConfigProps = {
isDebugMode: boolean;
isPending: boolean;
launchStdcmRequest: () => Promise<void>;
onStartSimulation: () => void;
retainedSimulationIndex: number;
showBtnToLaunchSimulation: boolean;
cancelStdcmRequest: () => void;
Expand All @@ -56,7 +56,7 @@ type StdcmConfigProps = {
const StdcmConfig = ({
isDebugMode,
isPending,
launchStdcmRequest,
onStartSimulation,
retainedSimulationIndex,
showBtnToLaunchSimulation,
cancelStdcmRequest,
Expand Down Expand Up @@ -129,7 +129,7 @@ const StdcmConfig = ({
const startSimulation = () => {
const formErrorsStatus = checkStdcmConfigErrors(pathSteps, t, pathfinding?.status);
if (pathfinding?.status === 'success' && !formErrorsStatus) {
launchStdcmRequest();
onStartSimulation();
} else {
// The console error is only for debugging the user tests (temporary)
console.warn('The form is not valid:', { pathfinding, formErrorsStatus });
Expand Down
16 changes: 11 additions & 5 deletions front/src/applications/stdcm/views/StdcmView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import StdcmResults from '../components/StdcmResults';
import StdcmStatusBanner from '../components/StdcmStatusBanner';
import useStdcmEnvironment, { NO_CONFIG_FOUND_MSG } from '../hooks/useStdcmEnv';
import useStdcmForm from '../hooks/useStdcmForm';
import type { StdcmSimulation } from '../types';
import type { StdcmSimulation, StdcmSimulationInputs } from '../types';

const StdcmView = () => {
// TODO : refacto. state useStdcm. Maybe we can merge some state together in order to reduce the number of refresh
const currentSimulationInputs = useStdcmForm();
const stdcmConf = useSelector(getStdcmConf);
const [simulationsList, setSimulationsList] = useState<StdcmSimulation[]>([]);
const [inProgressSimulationInputs, setInProgressSimulationInputs] = useState<StdcmSimulationInputs>();
const [selectedSimulationIndex, setSelectedSimulationIndex] = useState(-1);
const [showStatusBanner, setShowStatusBanner] = useState(false);
const [retainedSimulationIndex, setRetainedSimulationIndex] = useState(-1);
Expand Down Expand Up @@ -92,6 +93,11 @@ const StdcmView = () => {

const toggleHelpModule = () => setShowHelpModule((show) => !show);

const onStartSimulation = () => {
setInProgressSimulationInputs(currentSimulationInputs);
launchStdcmRequest();
};

// reset config data with the selected simulation data
useEffect(() => {
if (selectedSimulation) {
Expand Down Expand Up @@ -144,8 +150,8 @@ const StdcmView = () => {
* the existing one.
*/
const lastSimulation = simulationsList[simulationsList.length - 1];
const isSimulationAlreadyListed = isEqual(lastSimulation?.inputs, currentSimulationInputs);
const isSimulationOutputsComplete = stdcmResults?.stdcmResponse || hasConflicts;
const isSimulationAlreadyListed = isEqual(lastSimulation?.inputs, inProgressSimulationInputs);
const isSimulationOutputsComplete = stdcmResults?.stdcmResponse ?? hasConflicts;

if (isSimulationOutputsComplete) {
const newSimulation = {
Expand All @@ -154,7 +160,7 @@ const StdcmView = () => {
: {
id: simulationsList.length + 1,
creationDate: new Date(),
inputs: currentSimulationInputs,
inputs: inProgressSimulationInputs!,
}),
...(pathProperties && {
outputs: {
Expand Down Expand Up @@ -220,7 +226,7 @@ const StdcmView = () => {
isDebugMode={isDebugMode}
showBtnToLaunchSimulation={showBtnToLaunchSimulation}
retainedSimulationIndex={retainedSimulationIndex}
launchStdcmRequest={launchStdcmRequest}
onStartSimulation={onStartSimulation}
cancelStdcmRequest={cancelStdcmRequest}
/>

Expand Down

0 comments on commit 8016bc4

Please sign in to comment.