forked from HSLdevcom/digitransit-ui
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathRouteStopListContainer.js
184 lines (173 loc) · 4.9 KB
/
RouteStopListContainer.js
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
import PropTypes from 'prop-types';
import React from 'react';
import { createRefetchContainer, graphql } from 'react-relay';
import { matchShape } from 'found';
import connectToStores from 'fluxible-addons-react/connectToStores';
import groupBy from 'lodash/groupBy';
import values from 'lodash/values';
import cx from 'classnames';
import { FormattedMessage } from 'react-intl';
import {
configShape,
relayShape,
vehicleShape,
patternShape,
} from '../util/shapes';
import RouteStop from './RouteStop';
import withBreakpoint from '../util/withBreakpoint';
import { getRouteMode } from '../util/modeUtils';
class RouteStopListContainer extends React.PureComponent {
static propTypes = {
pattern: patternShape.isRequired,
className: PropTypes.string,
vehicles: PropTypes.objectOf(vehicleShape),
currentTime: PropTypes.number.isRequired,
relay: relayShape.isRequired,
breakpoint: PropTypes.string.isRequired,
hideDepartures: PropTypes.bool,
};
static defaultProps = {
className: undefined,
vehicles: [],
hideDepartures: false,
};
static contextTypes = {
config: configShape.isRequired,
match: matchShape.isRequired,
};
getStops() {
const { stops } = this.props.pattern;
const mode = getRouteMode(this.props.pattern.route);
const vehicles = groupBy(
values(this.props.vehicles).filter(
vehicle => this.props.currentTime - vehicle.timestamp < 5 * 60,
),
vehicle => vehicle.next_stop,
);
const rowClassName = `bp-${this.props.breakpoint}`;
return stops.map((stop, i) => {
const idx = i;
const nextStop = stops[i + 1];
const prevStop = stops[i - 1];
return (
<RouteStop
color={
this.props.pattern.route && this.props.pattern.route.color
? `#${this.props.pattern.route.color}`
: null
}
key={`${stop.gtfsId}-${this.props.pattern}-${idx}`}
stop={stop}
nextStop={nextStop}
prevStop={prevStop}
mode={mode}
vehicle={vehicles[stop.gtfsId] ? vehicles[stop.gtfsId][0] : null}
currentTime={this.props.currentTime}
last={i === stops.length - 1}
first={i === 0}
className={rowClassName}
displayNextDeparture={this.context.config.displayNextDeparture}
shortName={
this.props.pattern.route && this.props.pattern.route.shortName
}
hideDepartures={this.props.hideDepartures}
/>
);
});
}
// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps({ relay, currentTime }) {
const curr = this.props.currentTime;
const next = currentTime;
if (curr !== next) {
relay.refetch(
{
currentTime: next,
patternId: this.context.match.params.patternId,
},
null,
);
}
}
render() {
return (
<div role="tabpanel" aria-labelledby="route-tab">
<span className="sr-only">
<FormattedMessage
id="stop-list-update.sr-instructions"
default="Departure times for each stop update in real time."
/>
</span>
<ul className={cx('route-stop-list', this.props.className)}>
{this.getStops()}
</ul>
</div>
);
}
}
const containerComponent = createRefetchContainer(
connectToStores(
withBreakpoint(RouteStopListContainer),
['RealTimeInformationStore', 'PositionStore', 'TimeStore'],
({ getStore }) => ({
vehicles: getStore('RealTimeInformationStore').vehicles,
currentTime: getStore('TimeStore').getCurrentTime(),
}),
),
{
pattern: graphql`
fragment RouteStopListContainer_pattern on Pattern
@argumentDefinitions(
currentTime: { type: "Long!", defaultValue: 0 }
patternId: { type: "String!", defaultValue: "0" }
) {
directionId
route {
mode
color
shortName
type
}
stops {
alerts {
alertSeverityLevel
effectiveEndDate
effectiveStartDate
}
stopTimesForPattern(id: $patternId, startTime: $currentTime) {
realtime
realtimeState
realtimeArrival
realtimeDeparture
serviceDay
scheduledDeparture
pickupType
stop {
platformCode
}
}
gtfsId
lat
lon
name
desc
code
platformCode
zoneId
}
}
`,
},
graphql`
query RouteStopListContainerQuery(
$patternId: String!
$currentTime: Long!
) {
pattern(id: $patternId) {
...RouteStopListContainer_pattern
@arguments(currentTime: $currentTime, patternId: $patternId)
}
}
`,
);
export { containerComponent as default, RouteStopListContainer as Component };