Skip to content

Commit

Permalink
front: remove opTimingData from StdcmOpSchedule and simplify date man…
Browse files Browse the repository at this point in the history
…agement

Signed-off-by: Clara Ni <[email protected]>
  • Loading branch information
clarani committed Dec 24, 2024
1 parent 20d51e7 commit 2c31a7d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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 { useOsrdConfSelectors } from 'common/osrdContext';
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
Expand All @@ -20,13 +17,6 @@ const StdcmDestination = ({ disabled = false }: StdcmConfigCardProps) => {

const destination = useSelector(getStdcmDestination);

const { destinationArrival } = useMemo(
() => ({
destinationArrival: getTimesInfoFromDate(destination.arrival),
}),
[destination]
);

return (
<StdcmCard
data-testid="destination-card"
Expand All @@ -40,12 +30,7 @@ const StdcmDestination = ({ disabled = false }: StdcmConfigCardProps) => {
pathStepId={destination.id}
disabled={disabled}
/>
<StdcmOpSchedule
pathStep={destination}
opTimingData={destinationArrival}
disabled={disabled}
opId="destination-arrival"
/>
<StdcmOpSchedule pathStep={destination} disabled={disabled} opId="destination-arrival" />
</StdcmCard>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,40 @@ 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 { formatDateString, isArrivalDateInSearchTimeWindow } from 'utils/date';
import { dateToHHMMSS, formatDateString } from 'utils/date';
import { createStringSelectOptions } from 'utils/uiCoreHelpers';

import type { ArrivalTimeTypes, ScheduleConstraint } from '../../types';

type StdcmOpScheduleProps = {
disabled: boolean;
pathStep: Extract<StdcmPathStep, { isVia: false }>;
opTimingData?: {
date: Date;
arrivalDate: string;
arrivalTime: string;
arrivalTimeHours: number;
arrivalTimeMinutes: number;
};
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,
pathStep,
opTimingData,
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 } = useMemo(() => {
const isArrivalDateValid =
opTimingData?.arrivalDate &&
isArrivalDateInSearchTimeWindow(opTimingData.date, searchDatetimeWindow);

const { arrivalTime, arrivalTimeHours, arrivalTimeMinutes } = useMemo(() => {
if (!pathStep.arrival) {
return {
arrivalTime: undefined,
arrivalTimeHours: undefined,
arrivalTimeMinutes: undefined,
};
}
return {
arrivalDate:
opTimingData && isArrivalDateValid
? opTimingData.date
: defaultDate(searchDatetimeWindow?.begin),
arrivalTime: opTimingData?.arrivalTime,
arrivalTimeHours: opTimingData?.arrivalTimeHours,
arrivalTimeMinutes: opTimingData?.arrivalTimeMinutes,
arrivalTime: dateToHHMMSS(pathStep.arrival, { withoutSeconds: true }),
arrivalTimeHours: pathStep.arrival.getHours(),
arrivalTimeMinutes: pathStep.arrival.getMinutes(),
};
}, [opTimingData, searchDatetimeWindow]);
}, [pathStep.arrival]);

const tolerances = useMemo(
() => ({
Expand Down Expand Up @@ -137,7 +117,7 @@ const StdcmOpSchedule = ({
disabled,
}}
selectableSlot={selectableSlot}
value={arrivalDate}
value={pathStep.arrival}
onDateChange={(e) => {
onArrivalChange({
date: e,
Expand All @@ -153,7 +133,11 @@ const StdcmOpSchedule = ({
hours={arrivalTimeHours}
minutes={arrivalTimeMinutes}
onTimeChange={({ hours, minutes }) => {
onArrivalChange({ date: arrivalDate, hours, minutes });
onArrivalChange({
date: pathStep.arrival || searchDatetimeWindow!.begin,
hours,
minutes,
});
}}
disabled={disabled}
value={arrivalTime}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { useMemo } from 'react';

import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';

import { getTimesInfoFromDate } from 'applications/stdcm/utils';
import OriginIcon from 'assets/pictures/mapMarkers/start.svg';
import { useOsrdConfSelectors } from 'common/osrdContext';
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
Expand All @@ -19,13 +16,6 @@ const StdcmOrigin = ({ disabled = false }: StdcmConfigCardProps) => {
const { getStdcmOrigin } = useOsrdConfSelectors() as StdcmConfSelectors;
const origin = useSelector(getStdcmOrigin);

const { originArrival } = useMemo(
() => ({
originArrival: getTimesInfoFromDate(origin.arrival),
}),
[origin]
);

return (
<StdcmCard
name={t('trainPath.origin')}
Expand All @@ -39,13 +29,7 @@ const StdcmOrigin = ({ disabled = false }: StdcmConfigCardProps) => {
pathStepId={origin.id}
disabled={disabled}
/>
<StdcmOpSchedule
pathStep={origin}
opTimingData={originArrival}
disabled={disabled}
opId="origin-arrival"
isOrigin
/>
<StdcmOpSchedule pathStep={origin} disabled={disabled} opId="origin-arrival" isOrigin />
</StdcmCard>
);
};
Expand Down

0 comments on commit 2c31a7d

Please sign in to comment.