-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathRoutePatternSelectContainer.js
More file actions
312 lines (289 loc) · 8.85 KB
/
RoutePatternSelectContainer.js
File metadata and controls
312 lines (289 loc) · 8.85 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import {
createRefetchContainer,
fetchQuery,
graphql,
ReactRelayContext,
} from 'react-relay';
import cx from 'classnames';
import sortBy from 'lodash/sortBy';
import { routerShape } from 'found';
import enrichPatterns from '@digitransit-util/digitransit-util-enrich-patterns';
import { FormattedMessage, intlShape } from 'react-intl';
import { routeShape, relayShape, configShape } from '../../util/shapes';
import Icon from '../Icon';
import { routePagePath, PREFIX_STOPS } from '../../util/path';
import RoutePatternSelect, { patternTextWithIcon } from './RoutePatternSelect';
function filterSimilarRoutes(routes, currentRoute) {
const withoutCurrent = routes.filter(r => r.gtfsId !== currentRoute.gtfsId);
let routeBasename = currentRoute.shortName;
if (Number.isNaN(Number(routeBasename))) {
routeBasename = routeBasename.replace(/\D/g, ''); // Delete all non-digits from the string
}
const onlyRelatedRoutes = withoutCurrent.filter(r =>
Number.isNaN(Number(r.shortName.replace(routeBasename, '')[0])),
);
return sortBy(onlyRelatedRoutes, 'shortName');
}
class RoutePatternSelectContainer extends Component {
constructor(props, context) {
super(props, context);
this.state = {
similarRoutes: [],
loadingSimilar: true,
};
if (this.context.config.showSimilarRoutesOnRouteDropDown) {
this.fetchSimilarRoutes(this.props.route);
}
}
static propTypes = {
params: PropTypes.shape({
patternId: PropTypes.string.isRequired,
}).isRequired,
className: PropTypes.string.isRequired,
route: routeShape.isRequired,
onSelectChange: PropTypes.func.isRequired,
gtfsId: PropTypes.string.isRequired,
relayEnvironment: relayShape.isRequired,
};
static contextTypes = {
router: routerShape.isRequired,
config: configShape,
getStore: PropTypes.func.isRequired,
intl: intlShape.isRequired,
};
fetchSimilarRoutes = route => {
let searchSimilarTo = route.shortName;
const c = route.shortName.length ? route.shortName[0] : '';
if (c < '0' || c > '9') {
// must start with number
return;
}
if (Number.isNaN(Number(route.shortName))) {
searchSimilarTo = route.shortName.replace(/\D/g, ''); // Delete all non-digits from the string
}
if (!searchSimilarTo) {
// Dont try to search similar routes for routes that are named with letters (eg. P train)
return;
}
const query = graphql`
query RoutePatternSelectContainer_similarRoutesQuery($name: String) {
routes(name: $name) {
gtfsId
shortName
longName
mode
color
}
}
`;
const params = { name: searchSimilarTo };
fetchQuery(this.props.relayEnvironment, query, params, {
force: true,
})
.toPromise()
.then(results => {
this.setState({
similarRoutes: filterSimilarRoutes(results.routes, this.props.route),
loadingSimilar: false,
});
});
};
getOptions = () => {
const { gtfsId, params, route } = this.props;
const { router } = this.context;
const { patterns } = route;
if (patterns.length === 0) {
return null;
}
const futureTrips = enrichPatterns(
patterns,
false,
this.context.config.itinerary.serviceTimeRange,
);
if (futureTrips.length === 0) {
return null;
}
const options = sortBy(
sortBy(futureTrips, 'inFuture').reverse(),
'countTripsForDate',
).reverse();
if (options.every(o => o.code !== params.patternId)) {
router.replace(routePagePath(gtfsId, PREFIX_STOPS, options[0].code));
}
return options;
};
render() {
const { intl } = this.context;
const options = this.getOptions();
const currentPattern = options.find(
o => o.code === this.props.params.patternId,
);
const possibleMainRoutes = options.slice(0, 2).filter(o => !o.inFuture);
let mainRoutes = options.slice(0, 2).filter(o => !o.inFuture);
if (
possibleMainRoutes.every(o => o.directionId === 0) ||
possibleMainRoutes.every(o => o.directionId === 1)
) {
mainRoutes = possibleMainRoutes.slice(0, 1);
}
const specialRoutes = options
.slice(mainRoutes.length)
.filter(o => !o.inFuture);
const futureRoutes = options
.slice(mainRoutes.length)
.filter(o => o.inFuture);
const noSpecialRoutes = !specialRoutes.length;
const noFutureRoutes = !futureRoutes.length;
// If similar-route loading is enabled, avoid treating "no similar routes" as true
// until loading finishes. This prevents an initial button-only render that jumps
// to a dropdown once similar routes load.
const noSimilarRoutes = this.context.config.showSimilarRoutesOnRouteDropDown
? !this.state.similarRoutes?.length && !this.state.loadingSimilar
: true;
const renderButtonOnly =
mainRoutes.length &&
mainRoutes.length <= 2 &&
noSpecialRoutes &&
noFutureRoutes &&
noSimilarRoutes;
const directionSwap = mainRoutes.length === 2;
if (renderButtonOnly) {
const otherPattern = mainRoutes.find(
o => o.code !== this.props.params.patternId,
);
return (
<div
className={cx('route-pattern-select', this.props.className)}
aria-atomic="true"
>
<h3 className="route-pattern-select-title">
<FormattedMessage
id="route-page.choose-direction"
defaultMessage="Choose direction"
/>
</h3>
<label htmlFor="route-pattern-toggle-button">
{directionSwap && (
<span className="sr-only">
<FormattedMessage id="swap-order-button-label" />
</span>
)}
<button
id="route-pattern-toggle-button"
className="route-pattern-toggle"
type="button"
onClick={() =>
directionSwap
? this.props.onSelectChange(otherPattern.code)
: null
}
>
{patternTextWithIcon(currentPattern)}
{directionSwap && (
<Icon className="toggle-icon" img="icon_direction-c" />
)}
</button>
</label>
</div>
);
}
const optionArray = [];
if (mainRoutes.length > 0) {
optionArray.push({ options: mainRoutes, name: '' });
}
if (specialRoutes.length > 0) {
optionArray.push({
options: specialRoutes,
name: intl.formatMessage({
id: 'route-page.special-routes',
}),
});
}
if (futureRoutes.length > 0) {
optionArray.push({
options: futureRoutes,
name: intl.formatMessage({
id: 'route-page.future-routes',
}),
});
}
if (
this.context.config.showSimilarRoutesOnRouteDropDown &&
!this.state.loadingSimilar &&
this.state.similarRoutes?.length > 0
) {
optionArray.push({
options: this.state.similarRoutes,
name: intl.formatMessage({
id: 'route-page.similar-routes',
}),
});
}
return (
<div className={cx('route-pattern-select', this.props.className)}>
<h3 className="route-pattern-select-title">
<FormattedMessage
id="route-page.choose-direction"
defaultMessage="Choose direction"
/>
</h3>
<RoutePatternSelect
currentPattern={currentPattern}
optionArray={optionArray}
onSelectChange={this.props.onSelectChange}
className={this.props.className}
/>
</div>
);
}
}
const withStore = createRefetchContainer(
props => (
<ReactRelayContext.Consumer>
{({ environment }) => (
<RoutePatternSelectContainer
{...props}
relayEnvironment={environment}
/>
)}
</ReactRelayContext.Consumer>
),
{
route: graphql`
fragment RoutePatternSelectContainer_route on Route
@argumentDefinitions(date: { type: "String" }) {
shortName
mode
gtfsId
patterns {
code
directionId
headsign
stops {
name
}
activeDates: trips {
serviceId
day: activeDates
}
tripsForDate: tripsForDate(serviceDate: $date) {
stoptimes: stoptimesForDate(serviceDate: $date) {
scheduledDeparture
serviceDay
}
}
}
}
`,
},
graphql`
query RoutePatternSelectContainerQuery($routeId: String!, $date: String!) {
route(id: $routeId) {
...RoutePatternSelectContainer_route @arguments(date: $date)
}
}
`,
);
export { withStore as default, RoutePatternSelectContainer as Component };