-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathapiV1.js
172 lines (160 loc) · 4.05 KB
/
apiV1.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
import qs from 'qs'
import {
createQueryAction,
findGeometryForPattern,
findGeometryForTrip,
findPatternsForRouteError,
findPatternsForRouteResponse,
findRouteError,
findRouteResponse,
findRoutesError,
findRoutesResponse,
findStopsForPattern,
findStopsForTrip,
findStopTimesForTrip,
findTripError,
findTripResponse,
receivedVehiclePositions,
receivedVehiclePositionsError
} from './api'
import { setViewedStop } from './ui'
// Import must be done like this as maplibregl is incompatible with jest
let maplibregl = null
if (typeof jest === 'undefined') maplibregl = require('maplibre-gl')
const findTrip = (params) =>
createQueryAction(
`index/trips/${params.tripId}`,
findTripResponse,
findTripError,
{
noThrottle: true,
postprocess: (payload, dispatch) => {
dispatch(findStopsForTrip({ tripId: params.tripId }))
dispatch(findStopTimesForTrip({ tripId: params.tripId }))
dispatch(findGeometryForTrip({ tripId: params.tripId }))
}
}
)
export function vehicleRentalQuery(
params,
responseAction,
errorAction,
options
) {
const paramsString = qs.stringify(params)
const endpoint = `vehicle_rental${paramsString ? `?${paramsString}` : ''}`
return createQueryAction(endpoint, responseAction, errorAction, options)
}
/**
* Stop times for stop query (used in stop viewer).
*/
export function findStopTimesForStop(params) {
return function (dispatch, getState) {
console.warn('OTP1 is not supported.')
}
}
const getVehiclePositions = (routeId) =>
createQueryAction(
`index/routes/${routeId}/vehicles`,
receivedVehiclePositions,
receivedVehiclePositionsError,
{
noThrottle: true,
rewritePayload: (payload) => {
return {
routeId: routeId,
vehicles: payload
}
}
}
)
export const findPatternsForRoute = (params) =>
createQueryAction(
`index/routes/${params.routeId}/patterns?includeGeometry=true`,
findPatternsForRouteResponse,
findPatternsForRouteError,
{
noThrottle: true,
postprocess: (payload, dispatch) => {
// load geometry for each pattern
payload.forEach((ptn) => {
// Some OTP instances don't support includeGeometry.
// We need to manually fetch geometry in these cases.
if (!ptn.geometry) {
dispatch(
findGeometryForPattern({
patternId: ptn.id,
routeId: params.routeId
})
)
}
dispatch(
findStopsForPattern({
patternId: ptn.id,
routeId: params.routeId
})
)
})
},
rewritePayload: (payload) => {
// convert pattern array to ID-mapped object
const patterns = {}
payload.forEach((ptn) => {
patterns[ptn.id] = ptn
})
return {
patterns,
routeId: params.routeId
}
}
}
)
export const findRoute = (params) =>
createQueryAction(
`index/routes/${params.routeId}`,
findRouteResponse,
findRouteError,
{
noThrottle: true,
postprocess: (payload, dispatch) => {
// load patterns
dispatch(findPatternsForRoute({ routeId: params.routeId }))
dispatch(setViewedStop(null))
}
}
)
export function findRoutes() {
return createQueryAction(
'index/routes',
findRoutesResponse,
findRoutesError,
{
postprocess: (payload, dispatch) => {
dispatch(setViewedStop(null))
},
rewritePayload: (payload) => {
const routes = {}
payload.forEach((rte) => {
routes[rte.id] = rte
})
return routes
},
serviceId: 'routes'
}
)
}
export function routingQuery(searchId = null, updateSearchInReducer) {
return function (dispatch, getState) {
console.warn('OTP1 is not supported.')
}
}
export default {
findPatternsForRoute,
findRoute,
findRoutes,
findStopTimesForStop,
findTrip,
getVehiclePositions,
routingQuery,
vehicleRentalQuery
}