-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStdcmOpSchedule.tsx
170 lines (156 loc) · 5.23 KB
/
StdcmOpSchedule.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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 { updateStdcmPathStep } from 'reducers/osrdconf/stdcmConf';
import { getSearchDatetimeWindow } from 'reducers/osrdconf/stdcmConf/selectors';
import type { StdcmPathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { formatDateString } from 'utils/date';
import { Duration } from 'utils/duration';
import { createStringSelectOptions } from 'utils/uiCoreHelpers';
import type { ArrivalTimeTypes, ScheduleConstraint } from '../../types';
type StdcmOpScheduleProps = {
disabled: boolean;
pathStep: Extract<StdcmPathStep, { isVia: false }>;
opId: string;
isOrigin?: boolean;
};
const StdcmOpSchedule = ({ disabled, pathStep, opId, isOrigin = false }: StdcmOpScheduleProps) => {
const { t } = useTranslation('stdcm');
const dispatch = useAppDispatch();
const searchDatetimeWindow = useSelector(getSearchDatetimeWindow);
const { arrivalTimeHours, arrivalTimeMinutes } = useMemo(() => {
if (!pathStep.arrival) {
return {
arrivalTimeHours: undefined,
arrivalTimeMinutes: undefined,
};
}
return {
arrivalTimeHours: pathStep.arrival.getHours(),
arrivalTimeMinutes: pathStep.arrival.getMinutes(),
};
}, [pathStep.arrival]);
const tolerances = useMemo(
() => ({
minusTolerance: pathStep.tolerances.before.total('second'),
plusTolerance: pathStep.tolerances.after.total('second'),
}),
[pathStep.tolerances]
);
const selectableSlot = useMemo(
() => ({
start: searchDatetimeWindow.begin,
end: searchDatetimeWindow.end,
}),
[searchDatetimeWindow]
);
const datePickerErrorMessages = useMemo(
() => ({
invalidInput: t('form.datePickerErrors.invalidInput'),
invalidDate: t('form.datePickerErrors.invalidDate', {
startDate: formatDateString(searchDatetimeWindow.begin),
endDate: formatDateString(searchDatetimeWindow.end),
}),
}),
[t, searchDatetimeWindow]
);
const onArrivalChange = ({ date, hours, minutes }: ScheduleConstraint) => {
// We need to create a new date object to avoid mutating the original one
// otherwise the useEffect/useMemo will not be triggered
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 (
<>
<div className="arrival-type-select">
<Select
id={`select-${opId}`}
value={pathStep.arrivalType}
onChange={(e) => {
if (e) {
onArrivalTypeChange(e as ArrivalTimeTypes);
}
}}
{...createStringSelectOptions(
isOrigin
? ['preciseTime', 'respectDestinationSchedule']
: ['preciseTime', 'asSoonAsPossible']
)}
getOptionLabel={(option) => t(`trainPath.${option}`)}
disabled={disabled}
/>
</div>
{pathStep.arrivalType === 'preciseTime' && (
<div className="schedule">
<DatePicker
inputProps={{
id: `date-${opId}`,
label: t('trainPath.date'),
name: 'op-date',
disabled,
}}
selectableSlot={selectableSlot}
value={pathStep.arrival}
onDateChange={(e) => {
onArrivalChange({
date: e,
hours: arrivalTimeHours || 0,
minutes: arrivalTimeMinutes || 0,
});
}}
errorMessages={datePickerErrorMessages}
/>
<TimePicker
id={`time-${opId}`}
label={t('trainPath.time')}
hours={arrivalTimeHours}
minutes={arrivalTimeMinutes}
onTimeChange={({ hours, minutes }) => {
onArrivalChange({
date: pathStep.arrival || searchDatetimeWindow.begin,
hours,
minutes,
});
}}
disabled={disabled}
readOnly={false}
/>
<div className="mr-n2 pr-1">
<TolerancePicker
id={`stdcm-tolerance-${opId}`}
label={t('trainPath.tolerance')}
toleranceValues={tolerances}
onChange={() => {}}
onToleranceChange={({ minusTolerance, plusTolerance }) => {
dispatch(
updateStdcmPathStep({
id: pathStep.id,
updates: {
tolerances: {
before: new Duration({ seconds: minusTolerance }),
after: new Duration({ seconds: plusTolerance }),
},
},
})
);
}}
disabled={disabled}
/>
</div>
</div>
)}
</>
);
};
export default StdcmOpSchedule;