diff --git a/front/src/applications/stdcmV2/components/StdcmConfig.tsx b/front/src/applications/stdcmV2/components/StdcmConfig.tsx index fdd5f5441c..356f96a76a 100644 --- a/front/src/applications/stdcmV2/components/StdcmConfig.tsx +++ b/front/src/applications/stdcmV2/components/StdcmConfig.tsx @@ -20,7 +20,7 @@ import StdcmVias from './StdcmVias'; import StdcmWarningBox from './StdcmWarningBox'; import useStaticPathfinding from '../hooks/useStaticPathfinding'; import { ArrivalTimeTypes, StdcmConfigErrorTypes } from '../types'; -import type { StdcmConfigErrors, StdcmSimulation, StdcmSimulationInputs } from '../types'; +import type { StdcmConfigErrors } from '../types'; import checkStdcmConfigErrors from '../utils/checkStdcmConfigErrors'; /** @@ -32,8 +32,6 @@ type StdcmConfigProps = { isPending: boolean; launchStdcmRequest: () => Promise; retainedSimulationIndex: number; - selectedSimulation?: StdcmSimulation; - setCurrentSimulationInputs: React.Dispatch>; showBtnToLaunchSimulation: boolean; }; @@ -42,8 +40,6 @@ const StdcmConfig = ({ isPending, launchStdcmRequest, retainedSimulationIndex, - selectedSimulation, - setCurrentSimulationInputs, showBtnToLaunchSimulation, }: StdcmConfigProps) => { const { t } = useTranslation('stdcm'); @@ -73,12 +69,6 @@ const StdcmConfig = ({ const disabled = isPending || retainedSimulationIndex > -1; - const inputsProps = { - disabled, - selectedSimulation, - setCurrentSimulationInputs, - }; - const startSimulation = () => { const isPathfindingFailed = !!pathfinding && pathfinding.status !== 'success'; const formErrorsStatus = checkStdcmConfigErrors( @@ -144,15 +134,15 @@ const StdcmConfig = ({
- +
{/* //TODO: use them when we implement this feature #403 */} {/* } /> */} - - - + + + {/* } /> */}
{ +const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => { const { t } = useTranslation('stdcm'); const { speedLimitByTag, speedLimitsByTags, dispatchUpdateSpeedLimitByTag } = useStoreDataForSpeedLimitByTagSelector({ isStdcm: true }); @@ -79,12 +79,14 @@ const StdcmConsist = ({ setCurrentSimulationInputs, disabled = false }: StdcmCon const onInputChange = (e: React.ChangeEvent) => { searchRollingStock(e.target.value); if (e.target.value.trim().length === 0) { - updateRollingStockID(undefined); + dispatch(updateRollingStockID(undefined)); + dispatchUpdateSpeedLimitByTag(null); } }; const onSelectSuggestion = (option?: LightRollingStockWithLiveries) => { dispatch(updateRollingStockID(option?.id)); + dispatchUpdateSpeedLimitByTag(null); }; useEffect(() => { @@ -93,26 +95,8 @@ const StdcmConsist = ({ setCurrentSimulationInputs, disabled = false }: StdcmCon } else { searchRollingStock(''); } - setCurrentSimulationInputs((prevState) => ({ - ...prevState, - consist: { - tractionEngine: rollingStock, - }, - })); }, [rollingStock]); - useEffect(() => { - if (speedLimitByTag) { - setCurrentSimulationInputs((prevState) => ({ - ...prevState, - consist: { - ...prevState?.consist, - speedLimitByTag, - }, - })); - } - }, [speedLimitByTag]); - return ( (); + const [arrivalScheduleConstraint, setArrivalScheduleConstraint] = useState(); const { updateDestination, @@ -50,19 +46,6 @@ const StdcmDestination = ({ [destination] ); - useEffect(() => { - setCurrentSimulationInputs((prevState) => ({ - ...prevState, - pathSteps: replaceElementAtIndex( - prevState?.pathSteps, - prevState?.pathSteps.length - 1, - destination - ), - destination, - destinationTime: destinationArrival?.arrivalTime, - })); - }, [destination]); - const updateDestinationPoint = (pathStep: PathStep | null) => { if (!pathStep || !arrivalScheduleConstraint) { dispatch(updateDestination(pathStep)); diff --git a/front/src/applications/stdcmV2/components/StdcmOrigin.tsx b/front/src/applications/stdcmV2/components/StdcmOrigin.tsx index bea8609f58..54058ebab6 100644 --- a/front/src/applications/stdcmV2/components/StdcmOrigin.tsx +++ b/front/src/applications/stdcmV2/components/StdcmOrigin.tsx @@ -1,4 +1,4 @@ -import { useEffect, useMemo, useState } from 'react'; +import { useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; @@ -7,7 +7,6 @@ import { useOsrdConfActions } from 'common/osrdContext'; import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf'; import type { PathStep } from 'reducers/osrdconf/types'; import { useAppDispatch } from 'store'; -import { replaceElementAtIndex } from 'utils/array'; import { extractDateAndTimefromISO, generateISODateFromDateTime } from 'utils/date'; import StdcmCard from './StdcmCard'; @@ -17,7 +16,6 @@ import DEFAULT_TOLERANCE from '../consts'; import { ArrivalTimeTypes, type ScheduleConstraint, type StdcmConfigCardProps } from '../types'; const StdcmOrigin = ({ - setCurrentSimulationInputs, disabled = false, origin, }: StdcmConfigCardProps & { @@ -26,9 +24,7 @@ const StdcmOrigin = ({ const { t } = useTranslation('stdcm'); const dispatch = useAppDispatch(); - const [arrivalScheduleConstraint, setArrivalScheduleConstraint] = useState< - ScheduleConstraint | undefined - >(); + const [arrivalScheduleConstraint, setArrivalScheduleConstraint] = useState(); const { updateOrigin, updateOriginArrival, updateOriginArrivalType, updateOriginTolerances } = useOsrdConfActions() as StdcmConfSliceActions; @@ -44,15 +40,6 @@ const StdcmOrigin = ({ [origin] ); - useEffect(() => { - setCurrentSimulationInputs((prevState) => ({ - ...prevState, - pathSteps: replaceElementAtIndex(prevState?.pathSteps, 0, origin), - departureDate: originArrival?.arrivalDate, - departureTime: originArrival?.arrivalTime, - })); - }, [origin]); - const updateOriginPoint = (pathStep: PathStep | null) => { if (!pathStep || !arrivalScheduleConstraint) { dispatch(updateOrigin(pathStep)); diff --git a/front/src/applications/stdcmV2/components/StdcmVias.tsx b/front/src/applications/stdcmV2/components/StdcmVias.tsx index 8e9ecfe8ea..93bf517da9 100644 --- a/front/src/applications/stdcmV2/components/StdcmVias.tsx +++ b/front/src/applications/stdcmV2/components/StdcmVias.tsx @@ -22,7 +22,7 @@ import StdcmStopType from './StdcmStopType'; import { StdcmStopTypes } from '../types'; import type { StdcmConfigCardProps } from '../types'; -const StdcmVias = ({ disabled = false, setCurrentSimulationInputs }: StdcmConfigCardProps) => { +const StdcmVias = ({ disabled = false }: StdcmConfigCardProps) => { const { t } = useTranslation('stdcm'); const dispatch = useAppDispatch(); const { getPathSteps } = useOsrdConfSelectors(); @@ -103,13 +103,6 @@ const StdcmVias = ({ disabled = false, setCurrentSimulationInputs }: StdcmConfig }); }, [stopTypes, pathSteps]); - useEffect(() => { - setCurrentSimulationInputs((prevState) => ({ - ...prevState, - pathSteps, - })); - }, [pathSteps]); - return (
{intermediatePoints.length > 0 && diff --git a/front/src/applications/stdcmV2/hooks/useStdcmForm.ts b/front/src/applications/stdcmV2/hooks/useStdcmForm.ts new file mode 100644 index 0000000000..d2d026bafc --- /dev/null +++ b/front/src/applications/stdcmV2/hooks/useStdcmForm.ts @@ -0,0 +1,36 @@ +import { useMemo } from 'react'; + +import { useSelector } from 'react-redux'; + +import { useOsrdConfSelectors } from 'common/osrdContext'; +import { useStoreDataForRollingStockSelector } from 'modules/rollingStock/components/RollingStockSelector/useStoreDataForRollingStockSelector'; +import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors'; +import { extractDateAndTimefromISO } from 'utils/date'; + +import type { StdcmSimulationInputs } from '../types'; + +const useStdcmForm = (): StdcmSimulationInputs => { + const { getPathSteps, getSpeedLimitByTag } = useOsrdConfSelectors() as StdcmConfSelectors; + const pathSteps = useSelector(getPathSteps); + const speedLimitByTag = useSelector(getSpeedLimitByTag); + const { rollingStock } = useStoreDataForRollingStockSelector(); + + const currentSimulationInputs = useMemo(() => { + const origin = pathSteps.at(0); + const originArrival = origin?.arrival ? extractDateAndTimefromISO(origin.arrival) : undefined; + + return { + pathSteps, + departureDate: originArrival?.arrivalDate, + departureTime: originArrival?.arrivalTime, + consist: { + tractionEngine: rollingStock, + speedLimitByTag, + }, + }; + }, [pathSteps, rollingStock, speedLimitByTag]); + + return currentSimulationInputs; +}; + +export default useStdcmForm; diff --git a/front/src/applications/stdcmV2/types.ts b/front/src/applications/stdcmV2/types.ts index e066714241..95324d0d96 100644 --- a/front/src/applications/stdcmV2/types.ts +++ b/front/src/applications/stdcmV2/types.ts @@ -29,7 +29,6 @@ export type StdcmSimulation = { /** This type is used for StdcmConsist, StdcmOrigin, StdcmDestination and StdcmVias components */ export type StdcmConfigCardProps = { - setCurrentSimulationInputs: React.Dispatch>; disabled?: boolean; }; diff --git a/front/src/applications/stdcmV2/views/StdcmViewV2.tsx b/front/src/applications/stdcmV2/views/StdcmViewV2.tsx index 3eaecda891..aa688fdd5c 100644 --- a/front/src/applications/stdcmV2/views/StdcmViewV2.tsx +++ b/front/src/applications/stdcmV2/views/StdcmViewV2.tsx @@ -16,13 +16,12 @@ import StdcmLoader from '../components/StdcmLoader'; import StdcmResults from '../components/StdcmResults'; import StdcmStatusBanner from '../components/StdcmStatusBanner'; import useStdcmEnvironment, { NO_CONFIG_FOUND_MSG } from '../hooks/useStdcmEnv'; -import type { StdcmSimulation, StdcmSimulationInputs } from '../types'; +import useStdcmForm from '../hooks/useStdcmForm'; +import type { StdcmSimulation } from '../types'; const StdcmViewV2 = () => { // TODO : refacto. state useStdcm. Maybe we can merge some state together in order to reduce the number of refresh - const [currentSimulationInputs, setCurrentSimulationInputs] = useState({ - pathSteps: [null, null], // origin and destination are not set yet. We use the same logic as in the store. - }); + const currentSimulationInputs = useStdcmForm(); const [simulationsList, setSimulationsList] = useState([]); const [selectedSimulationIndex, setSelectedSimulationIndex] = useState(-1); const [showStatusBanner, setShowStatusBanner] = useState(false); @@ -181,13 +180,11 @@ const StdcmViewV2 = () => { ) : (
{isPending && }