From c48617f142c8a1b8c3f29e1c2c97b0e3d6dc2bc8 Mon Sep 17 00:00:00 2001 From: Clara Ni Date: Tue, 24 Dec 2024 13:43:13 +0100 Subject: [PATCH 1/6] front: move main actions from parent component in StdcmOpSchedule Signed-off-by: Clara Ni --- .../components/StdcmForm/StdcmDestination.tsx | 42 ++--------------- .../components/StdcmForm/StdcmOpSchedule.tsx | 46 +++++++++++------- .../components/StdcmForm/StdcmOrigin.tsx | 47 ++----------------- 3 files changed, 35 insertions(+), 100 deletions(-) diff --git a/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx b/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx index 31e0a185904..5f7eada11cb 100644 --- a/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx +++ b/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx @@ -5,26 +5,21 @@ import { useSelector } from 'react-redux'; import { getTimesInfoFromDate } from 'applications/stdcm/utils'; import DestinationIcon from 'assets/pictures/mapMarkers/destination.svg'; -import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext'; -import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf'; +import { useOsrdConfSelectors } from 'common/osrdContext'; import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors'; -import { useAppDispatch } from 'store'; import StdcmCard from './StdcmCard'; import StdcmOperationalPoint from './StdcmOperationalPoint'; import StdcmOpSchedule from './StdcmOpSchedule'; -import type { ArrivalTimeTypes, ScheduleConstraint, StdcmConfigCardProps } from '../../types'; +import type { StdcmConfigCardProps } from '../../types'; const StdcmDestination = ({ disabled = false }: StdcmConfigCardProps) => { const { t } = useTranslation('stdcm'); - const dispatch = useAppDispatch(); const { getStdcmDestination } = useOsrdConfSelectors() as StdcmConfSelectors; const destination = useSelector(getStdcmDestination); - const { updateStdcmPathStep } = useOsrdConfActions() as StdcmConfSliceActions; - const { destinationArrival, destinationToleranceValues } = useMemo( () => ({ destinationArrival: getTimesInfoFromDate(destination.arrival), @@ -36,35 +31,6 @@ const StdcmDestination = ({ disabled = false }: StdcmConfigCardProps) => { [destination] ); - const onArrivalChange = ({ date, hours, minutes }: ScheduleConstraint) => { - date.setHours(hours, minutes); - dispatch( - updateStdcmPathStep({ - id: destination.id, - updates: { arrival: date }, - }) - ); - }; - - const onArrivalTypeChange = (arrivalType: ArrivalTimeTypes) => { - dispatch(updateStdcmPathStep({ id: destination.id, updates: { arrivalType } })); - }; - - const onToleranceChange = ({ - toleranceBefore, - toleranceAfter, - }: { - toleranceBefore: number; - toleranceAfter: number; - }) => { - dispatch( - updateStdcmPathStep({ - id: destination.id, - updates: { tolerances: { before: toleranceBefore, after: toleranceAfter } }, - }) - ); - }; - return ( { disabled={disabled} /> void; - onArrivalTypeChange: (arrivalType: ArrivalTimeTypes) => void; - onArrivalToleranceChange: ({ - toleranceBefore, - toleranceAfter, - }: { - toleranceBefore: number; - toleranceAfter: number; - }) => void; + pathStep: Extract; opScheduleTimeType: ArrivalTimeTypes; opTimingData?: { date: Date; @@ -45,9 +40,7 @@ const defaultDate = (date?: Date) => { const StdcmOpSchedule = ({ disabled, - onArrivalChange, - onArrivalTypeChange, - onArrivalToleranceChange, + pathStep, opTimingData, opScheduleTimeType, opToleranceValues, @@ -55,6 +48,9 @@ const StdcmOpSchedule = ({ isOrigin = false, }: StdcmOpScheduleProps) => { const { t } = useTranslation('stdcm'); + const dispatch = useAppDispatch(); + + const { updateStdcmPathStep } = useOsrdConfActions() as StdcmConfSliceActions; const { getSearchDatetimeWindow } = useOsrdConfSelectors(); const searchDatetimeWindow = useSelector(getSearchDatetimeWindow); @@ -101,6 +97,20 @@ const StdcmOpSchedule = ({ [t, searchDatetimeWindow] ); + const onArrivalChange = ({ date, hours, minutes }: ScheduleConstraint) => { + date.setHours(hours, minutes); + dispatch( + updateStdcmPathStep({ + id: pathStep.id, + updates: { arrival: date }, + }) + ); + }; + + const onArrivalTypeChange = (arrivalType: ArrivalTimeTypes) => { + dispatch(updateStdcmPathStep({ id: pathStep.id, updates: { arrivalType } })); + }; + useEffect(() => { if ( (!isArrivalDateInSearchTimeWindow(arrivalDate, searchDatetimeWindow) || @@ -174,10 +184,12 @@ const StdcmOpSchedule = ({ toleranceValues={arrivalToleranceValues} onChange={() => {}} onToleranceChange={({ minusTolerance, plusTolerance }) => { - onArrivalToleranceChange({ - toleranceBefore: minusTolerance, - toleranceAfter: plusTolerance, - }); + dispatch( + updateStdcmPathStep({ + id: pathStep.id, + updates: { tolerances: { before: minusTolerance, after: plusTolerance } }, + }) + ); }} disabled={disabled} /> diff --git a/front/src/applications/stdcm/components/StdcmForm/StdcmOrigin.tsx b/front/src/applications/stdcm/components/StdcmForm/StdcmOrigin.tsx index bc934810e45..d9724bae523 100644 --- a/front/src/applications/stdcm/components/StdcmForm/StdcmOrigin.tsx +++ b/front/src/applications/stdcm/components/StdcmForm/StdcmOrigin.tsx @@ -5,25 +5,20 @@ import { useSelector } from 'react-redux'; import { getTimesInfoFromDate } from 'applications/stdcm/utils'; import OriginIcon from 'assets/pictures/mapMarkers/start.svg'; -import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext'; -import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf'; +import { useOsrdConfSelectors } from 'common/osrdContext'; import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors'; -import { useAppDispatch } from 'store'; import StdcmCard from './StdcmCard'; import StdcmOperationalPoint from './StdcmOperationalPoint'; import StdcmOpSchedule from './StdcmOpSchedule'; -import type { ArrivalTimeTypes, ScheduleConstraint, StdcmConfigCardProps } from '../../types'; +import type { StdcmConfigCardProps } from '../../types'; const StdcmOrigin = ({ disabled = false }: StdcmConfigCardProps) => { const { t } = useTranslation('stdcm'); - const dispatch = useAppDispatch(); const { getStdcmOrigin } = useOsrdConfSelectors() as StdcmConfSelectors; const origin = useSelector(getStdcmOrigin); - const { updateStdcmPathStep } = useOsrdConfActions() as StdcmConfSliceActions; - const { originArrival, originToleranceValues } = useMemo( () => ({ originArrival: getTimesInfoFromDate(origin.arrival), @@ -35,40 +30,6 @@ const StdcmOrigin = ({ disabled = false }: StdcmConfigCardProps) => { [origin] ); - const onOriginArrivalChange = ({ date, hours, minutes }: ScheduleConstraint) => { - date.setHours(hours, minutes); - dispatch( - updateStdcmPathStep({ - id: origin.id, - updates: { arrival: date }, - }) - ); - }; - - const onOriginArrivalTypeChange = (arrivalType: ArrivalTimeTypes) => { - dispatch( - updateStdcmPathStep({ - id: origin.id, - updates: { arrivalType }, - }) - ); - }; - - const onOriginToleranceChange = ({ - toleranceBefore, - toleranceAfter, - }: { - toleranceBefore: number; - toleranceAfter: number; - }) => { - dispatch( - updateStdcmPathStep({ - id: origin.id, - updates: { tolerances: { before: toleranceBefore, after: toleranceAfter } }, - }) - ); - }; - return ( { disabled={disabled} /> Date: Tue, 24 Dec 2024 13:46:46 +0100 Subject: [PATCH 2/6] front: compute tolerances directly in StdcmOpSchedule Signed-off-by: Clara Ni --- .../components/StdcmForm/StdcmDestination.tsx | 7 +-- .../components/StdcmForm/StdcmOpSchedule.tsx | 50 +++++++++---------- .../components/StdcmForm/StdcmOrigin.tsx | 7 +-- front/src/reducers/osrdconf/types.ts | 2 +- 4 files changed, 27 insertions(+), 39 deletions(-) diff --git a/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx b/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx index 5f7eada11cb..9add2b3fdc3 100644 --- a/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx +++ b/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx @@ -20,13 +20,9 @@ const StdcmDestination = ({ disabled = false }: StdcmConfigCardProps) => { const destination = useSelector(getStdcmDestination); - const { destinationArrival, destinationToleranceValues } = useMemo( + const { destinationArrival } = useMemo( () => ({ destinationArrival: getTimesInfoFromDate(destination.arrival), - destinationToleranceValues: { - arrivalToleranceBefore: destination.tolerances?.before || 0, - arrivalToleranceAfter: destination.tolerances?.after || 0, - }, }), [destination] ); @@ -47,7 +43,6 @@ const StdcmDestination = ({ disabled = false }: StdcmConfigCardProps) => { { @@ -54,26 +49,29 @@ const StdcmOpSchedule = ({ const { getSearchDatetimeWindow } = useOsrdConfSelectors(); const searchDatetimeWindow = useSelector(getSearchDatetimeWindow); - const { arrivalDate, arrivalTime, arrivalTimeHours, arrivalTimeMinutes, arrivalToleranceValues } = - useMemo(() => { - const isArrivalDateValid = - opTimingData?.arrivalDate && - isArrivalDateInSearchTimeWindow(opTimingData.date, searchDatetimeWindow); - - return { - arrivalDate: - opTimingData && isArrivalDateValid - ? opTimingData.date - : defaultDate(searchDatetimeWindow?.begin), - arrivalTime: opTimingData?.arrivalTime, - arrivalTimeHours: opTimingData?.arrivalTimeHours, - arrivalTimeMinutes: opTimingData?.arrivalTimeMinutes, - arrivalToleranceValues: { - minusTolerance: opToleranceValues.arrivalToleranceBefore, - plusTolerance: opToleranceValues.arrivalToleranceAfter, - }, - }; - }, [opTimingData, opToleranceValues, searchDatetimeWindow]); + const { arrivalDate, arrivalTime, arrivalTimeHours, arrivalTimeMinutes } = useMemo(() => { + const isArrivalDateValid = + opTimingData?.arrivalDate && + isArrivalDateInSearchTimeWindow(opTimingData.date, searchDatetimeWindow); + + return { + arrivalDate: + opTimingData && isArrivalDateValid + ? opTimingData.date + : defaultDate(searchDatetimeWindow?.begin), + arrivalTime: opTimingData?.arrivalTime, + arrivalTimeHours: opTimingData?.arrivalTimeHours, + arrivalTimeMinutes: opTimingData?.arrivalTimeMinutes, + }; + }, [opTimingData, searchDatetimeWindow]); + + const tolerances = useMemo( + () => ({ + minusTolerance: pathStep.tolerances.before, + plusTolerance: pathStep.tolerances.after, + }), + [pathStep.tolerances] + ); const selectableSlot = useMemo( () => @@ -181,7 +179,7 @@ const StdcmOpSchedule = ({ {}} onToleranceChange={({ minusTolerance, plusTolerance }) => { dispatch( diff --git a/front/src/applications/stdcm/components/StdcmForm/StdcmOrigin.tsx b/front/src/applications/stdcm/components/StdcmForm/StdcmOrigin.tsx index d9724bae523..159aa5bb0d9 100644 --- a/front/src/applications/stdcm/components/StdcmForm/StdcmOrigin.tsx +++ b/front/src/applications/stdcm/components/StdcmForm/StdcmOrigin.tsx @@ -19,13 +19,9 @@ const StdcmOrigin = ({ disabled = false }: StdcmConfigCardProps) => { const { getStdcmOrigin } = useOsrdConfSelectors() as StdcmConfSelectors; const origin = useSelector(getStdcmOrigin); - const { originArrival, originToleranceValues } = useMemo( + const { originArrival } = useMemo( () => ({ originArrival: getTimesInfoFromDate(origin.arrival), - originToleranceValues: { - arrivalToleranceBefore: origin.tolerances?.before || 0, - arrivalToleranceAfter: origin.tolerances?.after || 0, - }, }), [origin] ); @@ -46,7 +42,6 @@ const StdcmOrigin = ({ disabled = false }: StdcmConfigCardProps) => { Date: Tue, 24 Dec 2024 13:48:35 +0100 Subject: [PATCH 3/6] front: remove opScheduleTimeType props from StdcmOpScheduleProps Signed-off-by: Clara Ni --- .../stdcm/components/StdcmForm/StdcmDestination.tsx | 1 - .../stdcm/components/StdcmForm/StdcmOpSchedule.tsx | 10 ++++------ .../stdcm/components/StdcmForm/StdcmOrigin.tsx | 1 - 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx b/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx index 9add2b3fdc3..4f46570a04f 100644 --- a/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx +++ b/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx @@ -43,7 +43,6 @@ const StdcmDestination = ({ disabled = false }: StdcmConfigCardProps) => { diff --git a/front/src/applications/stdcm/components/StdcmForm/StdcmOpSchedule.tsx b/front/src/applications/stdcm/components/StdcmForm/StdcmOpSchedule.tsx index 69c56996610..154408c954b 100644 --- a/front/src/applications/stdcm/components/StdcmForm/StdcmOpSchedule.tsx +++ b/front/src/applications/stdcm/components/StdcmForm/StdcmOpSchedule.tsx @@ -16,7 +16,6 @@ import type { ArrivalTimeTypes, ScheduleConstraint } from '../../types'; type StdcmOpScheduleProps = { disabled: boolean; pathStep: Extract; - opScheduleTimeType: ArrivalTimeTypes; opTimingData?: { date: Date; arrivalDate: string; @@ -38,7 +37,6 @@ const StdcmOpSchedule = ({ disabled, pathStep, opTimingData, - opScheduleTimeType, opId, isOrigin = false, }: StdcmOpScheduleProps) => { @@ -113,7 +111,7 @@ const StdcmOpSchedule = ({ if ( (!isArrivalDateInSearchTimeWindow(arrivalDate, searchDatetimeWindow) || !opTimingData?.arrivalDate) && - opScheduleTimeType === 'preciseTime' + pathStep.arrivalType === 'preciseTime' ) { onArrivalChange({ date: defaultDate(searchDatetimeWindow?.begin), @@ -121,14 +119,14 @@ const StdcmOpSchedule = ({ minutes: arrivalTimeMinutes || 0, }); } - }, [searchDatetimeWindow, opScheduleTimeType]); + }, [searchDatetimeWindow, pathStep.arrivalType]); return ( <>