-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStdcmDestination.tsx
103 lines (90 loc) · 3.31 KB
/
StdcmDestination.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
import { useAppDispatch } from 'store';
import StdcmCard from './StdcmCard';
import StdcmOperationalPoint from './StdcmOperationalPoint';
import StdcmOpSchedule from './StdcmOpSchedule';
import { DEFAULT_TOLERANCE } from '../../consts';
import type { ArrivalTimeTypes, ScheduleConstraint, 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 !== undefined
? destination.tolerances.before
: DEFAULT_TOLERANCE,
arrivalToleranceAfter:
destination.tolerances?.after !== undefined
? destination.tolerances.after
: DEFAULT_TOLERANCE,
},
}),
[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 (
<StdcmCard
name={t('trainPath.destination')}
title={<img src={DestinationIcon} alt="destination" className="stdcm-destination-icon" />}
disabled={disabled}
className="extremity"
>
{(!destination.location || 'uic' in destination.location) && (
<StdcmOperationalPoint
location={destination.location}
pathStepId={destination.id}
disabled={disabled}
/>
)}
<StdcmOpSchedule
onArrivalChange={onArrivalChange}
onArrivalTypeChange={onArrivalTypeChange}
onArrivalToleranceChange={onToleranceChange}
opTimingData={destinationArrival}
opToleranceValues={destinationToleranceValues}
opScheduleTimeType={destination.arrivalType}
disabled={disabled}
opId="destination-arrival"
/>
</StdcmCard>
);
};
export default StdcmDestination;