-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStdcmViewV2.tsx
172 lines (152 loc) · 6.09 KB
/
StdcmViewV2.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, { useEffect, useState } from 'react';
import { isEqual, isNil } from 'lodash';
import useStdcm from 'applications/stdcm/hooks/useStdcm';
import { LoaderFill } from 'common/Loaders';
import { useOsrdConfActions } from 'common/osrdContext';
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import { useAppDispatch } from 'store';
import StdcmConfig from '../components/StdcmConfig';
import StdcmEmptyConfigError from '../components/StdcmEmptyConfigError';
import StdcmHeader from '../components/StdcmHeader';
import StdcmResults from '../components/StdcmResults';
import useStdcmEnvironment, { NO_CONFIG_FOUND_MSG } from '../hooks/useStdcmEnv';
import type { StdcmSimulation, StdcmSimulationInputs } from '../types';
const StdcmViewV2 = () => {
const { loading, error } = useStdcmEnvironment();
// TODO : refacto. state useStdcm. Maybe we can merge some state together in order to reduce the number of refresh
const {
launchStdcmRequest,
cancelStdcmRequest,
isPending,
isRejected,
isStdcmResultsEmpty,
stdcmV2Results,
pathProperties,
} = useStdcm(false);
const [currentSimulationInputs, setCurrentSimulationInputs] = useState<StdcmSimulationInputs>({
pathSteps: [null, null], // origin and destination are not set yet. We use the same logic as in the store.
});
const [simulationsList, setSimulationsList] = useState<StdcmSimulation[]>([]);
const [selectedSimulationIndex, setSelectedSimulationIndex] = useState(-1);
const [showStatusBanner, setShowStatusBanner] = useState(false);
const [retainedSimulationIndex, setRetainedSimulationIndex] = useState(-1);
const [showBtnToLaunchSimulation, setShowBtnToLaunchSimulation] = useState(false);
const dispatch = useAppDispatch();
const { resetStdcmConfig, updateStdcmConfigWithData } =
useOsrdConfActions() as StdcmConfSliceActions;
const selectedSimulation = simulationsList[selectedSimulationIndex];
const isCalculationFailed = isRejected && !isStdcmResultsEmpty;
const showResults = !isPending && (showStatusBanner || simulationsList.length > 0);
const handleRetainSimulation = () => setRetainedSimulationIndex(selectedSimulationIndex);
const handleSelectSimulation = (index: number) => {
if (retainedSimulationIndex === -1) {
setSelectedSimulationIndex(index);
setShowBtnToLaunchSimulation(false);
}
};
const handleStartNewQuery = () => {
setSimulationsList([]);
setSelectedSimulationIndex(-1);
setRetainedSimulationIndex(-1);
dispatch(resetStdcmConfig());
};
// reset config data with the selected simulation data
useEffect(() => {
if (selectedSimulation) {
const { departureDate, departureTime, pathSteps, consist } = selectedSimulation.inputs;
dispatch(
updateStdcmConfigWithData({
rollingStockID: consist?.tractionEngine?.id,
speedLimitByTag: consist?.speedLimitByTag,
pathSteps: [...pathSteps],
originDate: departureDate,
originTime: departureTime,
})
);
}
}, [selectedSimulation]);
useEffect(() => {
setShowBtnToLaunchSimulation(!isEqual(currentSimulationInputs, selectedSimulation?.inputs));
}, [currentSimulationInputs]);
useEffect(() => {
if (isPending) {
setShowBtnToLaunchSimulation(false);
}
}, [isPending]);
useEffect(() => {
if (currentSimulationInputs && (stdcmV2Results?.stdcmResponse || isStdcmResultsEmpty)) {
setSimulationsList((prevSimulationList) => [
...prevSimulationList,
{
id: prevSimulationList.length + 1,
creationDate: new Date(),
inputs: currentSimulationInputs,
...(stdcmV2Results?.stdcmResponse &&
pathProperties && {
outputs: {
results: stdcmV2Results.stdcmResponse,
pathProperties,
},
}),
},
]);
setShowStatusBanner(true);
}
}, [pathProperties, isStdcmResultsEmpty]);
// We have a simulation with an error.
useEffect(() => {
if (isRejected && !isStdcmResultsEmpty) {
setShowStatusBanner(true);
}
}, [isRejected, isStdcmResultsEmpty]);
// select the last simulation in the list
useEffect(() => {
if (simulationsList.length > 0) {
setSelectedSimulationIndex(simulationsList.length - 1);
}
}, [simulationsList]);
// If we've got an error during the loading of the stdcm env which is not the "no config error" message,
// we let the error boundary manage it
if (error && error.message !== NO_CONFIG_FOUND_MSG) throw error;
return (
<div role="button" tabIndex={0} className="stdcm-v2" onClick={() => setShowStatusBanner(false)}>
<StdcmHeader />
{!isNil(error) ? (
<StdcmEmptyConfigError />
) : (
<div>
<StdcmConfig
selectedSimulation={selectedSimulation}
currentSimulationInputs={currentSimulationInputs}
isPending={isPending}
showBtnToLaunchSimulation={showBtnToLaunchSimulation}
retainedSimulationIndex={retainedSimulationIndex}
showStatusBanner={showStatusBanner}
isCalculationFailed={isCalculationFailed}
launchStdcmRequest={launchStdcmRequest}
cancelStdcmRequest={cancelStdcmRequest}
setCurrentSimulationInputs={setCurrentSimulationInputs}
/>
{showResults && (
<div className="stdcm-v2-results">
{selectedSimulationIndex > -1 && (
<StdcmResults
simulationsList={simulationsList}
selectedSimulationIndex={selectedSimulationIndex}
retainedSimulationIndex={retainedSimulationIndex}
showStatusBanner={showStatusBanner}
isCalculationFailed={isCalculationFailed}
onRetainSimulation={handleRetainSimulation}
onSelectSimulation={handleSelectSimulation}
onStartNewQuery={handleStartNewQuery}
/>
)}
</div>
)}
</div>
)}
{loading && <LoaderFill />}
</div>
);
};
export default StdcmViewV2;