diff --git a/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx b/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx index 31e0a185904..5da7ce58cfc 100644 --- a/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx +++ b/front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx @@ -1,70 +1,22 @@ -import { useMemo } from 'react'; - import { useTranslation } from 'react-i18next'; 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), - destinationToleranceValues: { - arrivalToleranceBefore: destination.tolerances?.before || 0, - arrivalToleranceAfter: destination.tolerances?.after || 0, - }, - }), - [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 ( { pathStepId={destination.id} disabled={disabled} /> - + ); }; diff --git a/front/src/applications/stdcm/components/StdcmForm/StdcmOpSchedule.tsx b/front/src/applications/stdcm/components/StdcmForm/StdcmOpSchedule.tsx index 06890423654..85491e1b87f 100644 --- a/front/src/applications/stdcm/components/StdcmForm/StdcmOpSchedule.tsx +++ b/front/src/applications/stdcm/components/StdcmForm/StdcmOpSchedule.tsx @@ -1,83 +1,55 @@ -import { useEffect, useMemo } from 'react'; +import { useMemo } from 'react'; import { DatePicker, Select, TimePicker, TolerancePicker } from '@osrd-project/ui-core'; import { useTranslation } from 'react-i18next'; import { useSelector } from 'react-redux'; -import { useOsrdConfSelectors } from 'common/osrdContext'; -import { formatDateString, isArrivalDateInSearchTimeWindow } from 'utils/date'; +import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext'; +import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf'; +import type { StdcmPathStep } from 'reducers/osrdconf/types'; +import { useAppDispatch } from 'store'; +import { dateToHHMMSS, formatDateString } from 'utils/date'; import { createStringSelectOptions } from 'utils/uiCoreHelpers'; import type { ArrivalTimeTypes, ScheduleConstraint } from '../../types'; type StdcmOpScheduleProps = { disabled: boolean; - onArrivalChange: ({ date, hours, minutes }: ScheduleConstraint) => void; - onArrivalTypeChange: (arrivalType: ArrivalTimeTypes) => void; - onArrivalToleranceChange: ({ - toleranceBefore, - toleranceAfter, - }: { - toleranceBefore: number; - toleranceAfter: number; - }) => void; - opScheduleTimeType: ArrivalTimeTypes; - opTimingData?: { - date: Date; - arrivalDate: string; - arrivalTime: string; - arrivalTimeHours: number; - arrivalTimeMinutes: number; - }; - opToleranceValues: { - arrivalToleranceBefore: number; - arrivalToleranceAfter: number; - }; + pathStep: Extract; opId: string; isOrigin?: boolean; }; -const defaultDate = (date?: Date) => { - const newDate = date ? new Date(date) : new Date(); - newDate.setHours(0, 0, 0); - return newDate; -}; - -const StdcmOpSchedule = ({ - disabled, - onArrivalChange, - onArrivalTypeChange, - onArrivalToleranceChange, - opTimingData, - opScheduleTimeType, - opToleranceValues, - opId, - isOrigin = false, -}: StdcmOpScheduleProps) => { +const StdcmOpSchedule = ({ disabled, pathStep, opId, isOrigin = false }: StdcmOpScheduleProps) => { const { t } = useTranslation('stdcm'); + const dispatch = useAppDispatch(); + + const { updateStdcmPathStep } = useOsrdConfActions() as StdcmConfSliceActions; const { getSearchDatetimeWindow } = useOsrdConfSelectors(); const searchDatetimeWindow = useSelector(getSearchDatetimeWindow); - const { arrivalDate, arrivalTime, arrivalTimeHours, arrivalTimeMinutes, arrivalToleranceValues } = - useMemo(() => { - const isArrivalDateValid = - opTimingData?.arrivalDate && - isArrivalDateInSearchTimeWindow(opTimingData.date, searchDatetimeWindow); - + const { arrivalTime, arrivalTimeHours, arrivalTimeMinutes } = useMemo(() => { + if (!pathStep.arrival) { 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, - }, + arrivalTime: undefined, + arrivalTimeHours: undefined, + arrivalTimeMinutes: undefined, }; - }, [opTimingData, opToleranceValues, searchDatetimeWindow]); + } + return { + arrivalTime: dateToHHMMSS(pathStep.arrival, { withoutSeconds: true }), + arrivalTimeHours: pathStep.arrival.getHours(), + arrivalTimeMinutes: pathStep.arrival.getMinutes(), + }; + }, [pathStep.arrival]); + + const tolerances = useMemo( + () => ({ + minusTolerance: pathStep.tolerances?.before || 0, + plusTolerance: pathStep.tolerances?.after || 0, + }), + [pathStep.tolerances] + ); const selectableSlot = useMemo( () => @@ -101,26 +73,27 @@ const StdcmOpSchedule = ({ [t, searchDatetimeWindow] ); - useEffect(() => { - if ( - (!isArrivalDateInSearchTimeWindow(arrivalDate, searchDatetimeWindow) || - !opTimingData?.arrivalDate) && - opScheduleTimeType === 'preciseTime' - ) { - onArrivalChange({ - date: defaultDate(searchDatetimeWindow?.begin), - hours: arrivalTimeHours || 0, - minutes: arrivalTimeMinutes || 0, - }); - } - }, [searchDatetimeWindow, opScheduleTimeType]); + const onArrivalChange = ({ date, hours, minutes }: ScheduleConstraint) => { + const newDate = new Date(date); + newDate.setHours(hours, minutes); + dispatch( + updateStdcmPathStep({ + id: pathStep.id, + updates: { arrival: newDate }, + }) + ); + }; + + const onArrivalTypeChange = (arrivalType: ArrivalTimeTypes) => { + dispatch(updateStdcmPathStep({ id: pathStep.id, updates: { arrivalType } })); + }; return ( <>