-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathItinerary.tsx
182 lines (168 loc) · 6.05 KB
/
Itinerary.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
171
172
173
174
175
176
177
178
179
180
181
182
import { useEffect, useState } from 'react';
import { ArrowSwitch, Route, Plus, Rocket, Trash } from '@osrd-project/ui-icons';
import bbox from '@turf/bbox';
import type { Position } from 'geojson';
import { isNil } from 'lodash';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { useManageTrainScheduleContext } from 'applications/operationalStudies/hooks/useManageTrainScheduleContext';
import { useModal } from 'common/BootstrapSNCF/ModalSNCF';
import { computeBBoxViewport } from 'common/Map/WarpedMap/core/helpers';
import { useOsrdConfSelectors } from 'common/osrdContext';
import Tipped from 'common/Tipped';
import Pathfinding from 'modules/pathfinding/components/Pathfinding/Pathfinding';
import TypeAndPath from 'modules/pathfinding/components/Pathfinding/TypeAndPath';
import { setWarning } from 'reducers/main';
import { updateViewport } from 'reducers/map';
import { getMap } from 'reducers/map/selectors';
import { useAppDispatch } from 'store';
import { isEmptyArray } from 'utils/array';
import Destination from './DisplayItinerary/Destination';
import Origin from './DisplayItinerary/Origin';
import Vias from './DisplayItinerary/Vias';
import ModalSuggestedVias from './ModalSuggestedVias';
const Itinerary = () => {
const { getPathSteps, getOrigin, getDestination, getPowerRestriction } = useOsrdConfSelectors();
const origin = useSelector(getOrigin);
const destination = useSelector(getDestination);
const pathSteps = useSelector(getPathSteps);
const powerRestrictions = useSelector(getPowerRestriction);
const [displayTypeAndPath, setDisplayTypeAndPath] = useState(false);
const dispatch = useAppDispatch();
const map = useSelector(getMap);
const { t } = useTranslation('operationalStudies/manageTrainSchedule');
const { openModal } = useModal();
const { pathProperties, setPathProperties, launchPathfinding, pathStepsAndSuggestedOPs } =
useManageTrainScheduleContext();
const zoomToFeaturePoint = (lngLat?: Position) => {
if (lngLat) {
const newViewport = {
...map.viewport,
longitude: lngLat[0],
latitude: lngLat[1],
zoom: 16,
};
dispatch(updateViewport(newViewport));
}
};
const seeWholeItinerary = () => {
if (pathProperties) {
const newViewport = computeBBoxViewport(bbox(pathProperties.geometry), map.viewport);
dispatch(updateViewport(newViewport));
}
};
const notifyRestrictionResetWarning = () => {
if (!isEmptyArray(powerRestrictions)) {
dispatch(
setWarning({
title: t('warningMessages.pathfindingChange'),
text: t('warningMessages.powerRestrictionsReset'),
})
);
}
};
const inverseOD = () => {
notifyRestrictionResetWarning();
const newPathSteps = [...pathSteps].reverse();
launchPathfinding(newPathSteps);
};
const resetPathfinding = () => {
setPathProperties(undefined);
notifyRestrictionResetWarning();
launchPathfinding([null, null]);
};
useEffect(() => {
seeWholeItinerary();
}, [pathProperties]);
return (
<div className="osrd-config-item">
<div className="mb-2 d-flex">
<Pathfinding />
<button
type="button"
className="btn btn-sm btn-only-icon btn-white px-3 ml-2"
aria-label={t('toggleTrigramSearch')}
title={t('toggleTrigramSearch')}
onClick={() => setDisplayTypeAndPath(!displayTypeAndPath)}
data-testid="rocket-button"
>
<Rocket />
</button>
</div>
{displayTypeAndPath && (
<div className="mb-2">
<TypeAndPath setDisplayTypeAndPath={setDisplayTypeAndPath} />
</div>
)}
{origin && destination && (
<div className="d-flex flex-row flex-wrap">
<button
className="col my-1 btn bg-white btn-sm"
type="button"
aria-label={t('viewItineraryOnMap')}
title={t('viewItineraryOnMap')}
onClick={seeWholeItinerary}
disabled={isNil(pathProperties)}
>
<Route />
</button>
{pathStepsAndSuggestedOPs && (
<button
data-testid="add-waypoints-button"
className="col ml-1 my-1 text-white btn bg-info btn-sm"
type="button"
onClick={() =>
openModal(
<ModalSuggestedVias
suggestedVias={pathStepsAndSuggestedOPs}
launchPathfinding={launchPathfinding}
/>
)
}
>
<span className="mr-1">{t('addVias')}</span>
<Plus />
</button>
)}
<button
data-testid="reverse-itinerary-button"
className="col ml-1 my-1 btn bg-warning btn-sm"
type="button"
onClick={inverseOD}
>
<span className="mr-1">{t('inverseOD')}</span>
<ArrowSwitch />
</button>
<Tipped mode="right">
<button
data-testid="delete-itinerary-button"
type="button"
className="ml-1 mt-1 btn-danger btn btn-sm"
aria-label={t('deleteRoute')}
onClick={resetPathfinding}
>
<Trash />
</button>
<span>{t('deleteRoute')}</span>
</Tipped>
</div>
)}
<div className="osrd-config-item-container pathfinding-details" data-testid="itinerary">
<div data-testid="display-itinerary">
<Origin zoomToFeaturePoint={zoomToFeaturePoint} />
<div className="vias-list mb-2" data-testid="itinerary-vias">
{pathSteps.length > 2 ? (
<Vias zoomToFeaturePoint={zoomToFeaturePoint} />
) : (
<small data-testid="no-waypoint-chosen-text" className="ml-4">
{t('noPlaceChosen')}
</small>
)}
</div>
<Destination zoomToFeaturePoint={zoomToFeaturePoint} />
</div>
</div>
</div>
);
};
export default Itinerary;