Skip to content

Commit

Permalink
front: remove setCurrentSimulationInputs in stdcm form
Browse files Browse the repository at this point in the history
Signed-off-by: Clara Ni <clara.ni@outlook.fr>
  • Loading branch information
clarani committed Oct 21, 2024
1 parent 698b8f4 commit 02e76f0
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 84 deletions.
20 changes: 5 additions & 15 deletions front/src/applications/stdcmV2/components/StdcmConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -32,8 +32,6 @@ type StdcmConfigProps = {
isPending: boolean;
launchStdcmRequest: () => Promise<void>;
retainedSimulationIndex: number;
selectedSimulation?: StdcmSimulation;
setCurrentSimulationInputs: React.Dispatch<React.SetStateAction<StdcmSimulationInputs>>;
showBtnToLaunchSimulation: boolean;
};

Expand All @@ -42,8 +40,6 @@ const StdcmConfig = ({
isPending,
launchStdcmRequest,
retainedSimulationIndex,
selectedSimulation,
setCurrentSimulationInputs,
showBtnToLaunchSimulation,
}: StdcmConfigProps) => {
const { t } = useTranslation('stdcm');
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -144,15 +134,15 @@ const StdcmConfig = ({
<div className="d-flex">
<div className="stdcm-v2-simulation-inputs">
<div className="stdcm-v2-consist-container">
<StdcmConsist {...inputsProps} />
<StdcmConsist disabled={disabled} />
</div>
<div className="stdcm-v2__separator" />
<div className="stdcm-v2-simulation-itinerary">
{/* //TODO: use them when we implement this feature #403 */}
{/* <StdcmDefaultCard text="Indiquer le sillon antérieur" Icon={<ArrowUp size="lg" />} /> */}
<StdcmOrigin {...inputsProps} origin={origin} />
<StdcmVias {...inputsProps} />
<StdcmDestination {...inputsProps} destination={destination} />
<StdcmOrigin disabled={disabled} origin={origin} />
<StdcmVias disabled={disabled} />
<StdcmDestination disabled={disabled} destination={destination} />
{/* <StdcmDefaultCard text="Indiquer le sillon postérieur" Icon={<ArrowDown size="lg" />} /> */}
<div
className={cx('stdcm-v2-launch-request', {
Expand Down
24 changes: 4 additions & 20 deletions front/src/applications/stdcmV2/components/StdcmConsist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ConsistCardTitle = ({
);
};

const StdcmConsist = ({ setCurrentSimulationInputs, disabled = false }: StdcmConfigCardProps) => {
const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => {
const { t } = useTranslation('stdcm');
const { speedLimitByTag, speedLimitsByTags, dispatchUpdateSpeedLimitByTag } =
useStoreDataForSpeedLimitByTagSelector({ isStdcm: true });
Expand Down Expand Up @@ -79,12 +79,14 @@ const StdcmConsist = ({ setCurrentSimulationInputs, disabled = false }: StdcmCon
const onInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
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(() => {
Expand All @@ -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 (
<StdcmCard
name={t('consist.consist')}
Expand Down
21 changes: 2 additions & 19 deletions front/src/applications/stdcmV2/components/StdcmDestination.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useState } from 'react';
import { useMemo, useState } from 'react';

import { useTranslation } from 'react-i18next';

Expand All @@ -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';
Expand All @@ -17,7 +16,6 @@ import DEFAULT_TOLERANCE from '../consts';
import { ArrivalTimeTypes, type ScheduleConstraint, type StdcmConfigCardProps } from '../types';

const StdcmDestination = ({
setCurrentSimulationInputs,
disabled = false,
destination,
}: StdcmConfigCardProps & {
Expand All @@ -26,9 +24,7 @@ const StdcmDestination = ({
const { t } = useTranslation('stdcm');
const dispatch = useAppDispatch();

const [arrivalScheduleConstraint, setArrivalScheduleConstraint] = useState<
ScheduleConstraint | undefined
>();
const [arrivalScheduleConstraint, setArrivalScheduleConstraint] = useState<ScheduleConstraint>();

const {
updateDestination,
Expand All @@ -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));
Expand Down
17 changes: 2 additions & 15 deletions front/src/applications/stdcmV2/components/StdcmOrigin.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useState } from 'react';
import { useMemo, useState } from 'react';

import { useTranslation } from 'react-i18next';

Expand All @@ -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';
Expand All @@ -17,7 +16,6 @@ import DEFAULT_TOLERANCE from '../consts';
import { ArrivalTimeTypes, type ScheduleConstraint, type StdcmConfigCardProps } from '../types';

const StdcmOrigin = ({
setCurrentSimulationInputs,
disabled = false,
origin,
}: StdcmConfigCardProps & {
Expand All @@ -26,9 +24,7 @@ const StdcmOrigin = ({
const { t } = useTranslation('stdcm');
const dispatch = useAppDispatch();

const [arrivalScheduleConstraint, setArrivalScheduleConstraint] = useState<
ScheduleConstraint | undefined
>();
const [arrivalScheduleConstraint, setArrivalScheduleConstraint] = useState<ScheduleConstraint>();

const { updateOrigin, updateOriginArrival, updateOriginArrivalType, updateOriginTolerances } =
useOsrdConfActions() as StdcmConfSliceActions;
Expand All @@ -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));
Expand Down
9 changes: 1 addition & 8 deletions front/src/applications/stdcmV2/components/StdcmVias.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -103,13 +103,6 @@ const StdcmVias = ({ disabled = false, setCurrentSimulationInputs }: StdcmConfig
});
}, [stopTypes, pathSteps]);

useEffect(() => {
setCurrentSimulationInputs((prevState) => ({
...prevState,
pathSteps,
}));
}, [pathSteps]);

return (
<div className="stdcm-v2-vias-list">
{intermediatePoints.length > 0 &&
Expand Down
36 changes: 36 additions & 0 deletions front/src/applications/stdcmV2/hooks/useStdcmForm.ts
Original file line number Diff line number Diff line change
@@ -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;
1 change: 0 additions & 1 deletion front/src/applications/stdcmV2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export type StdcmSimulation = {

/** This type is used for StdcmConsist, StdcmOrigin, StdcmDestination and StdcmVias components */
export type StdcmConfigCardProps = {
setCurrentSimulationInputs: React.Dispatch<React.SetStateAction<StdcmSimulationInputs>>;
disabled?: boolean;
};

Expand Down
9 changes: 3 additions & 6 deletions front/src/applications/stdcmV2/views/StdcmViewV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<StdcmSimulationInputs>({
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<StdcmSimulation[]>([]);
const [selectedSimulationIndex, setSelectedSimulationIndex] = useState(-1);
const [showStatusBanner, setShowStatusBanner] = useState(false);
Expand Down Expand Up @@ -181,13 +180,11 @@ const StdcmViewV2 = () => {
) : (
<div>
<StdcmConfig
selectedSimulation={selectedSimulation}
isPending={isPending}
isDebugMode={isDebugMode}
showBtnToLaunchSimulation={showBtnToLaunchSimulation}
retainedSimulationIndex={retainedSimulationIndex}
launchStdcmRequest={launchStdcmRequest}
setCurrentSimulationInputs={setCurrentSimulationInputs}
/>

{isPending && <StdcmLoader cancelStdcmRequest={cancelStdcmRequest} ref={loaderRef} />}
Expand Down

0 comments on commit 02e76f0

Please sign in to comment.