Skip to content

Commit

Permalink
fix(actions/api): Split findRoutes() into OTP1/2-specific implementat…
Browse files Browse the repository at this point in the history
…ions.
  • Loading branch information
binh-dam-ibigroup committed Oct 11, 2022
1 parent c479916 commit d9d394c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 23 deletions.
29 changes: 6 additions & 23 deletions lib/actions/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,29 +701,8 @@ export function findStopTimesForStop(params) {

// Routes lookup query

const findRoutesResponse = createAction('FIND_ROUTES_RESPONSE')
const findRoutesError = createAction('FIND_ROUTES_ERROR')

export function findRoutes(params) {
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 const findRoutesResponse = createAction('FIND_ROUTES_RESPONSE')
export const findRoutesError = createAction('FIND_ROUTES_ERROR')

// Patterns for Route lookup query
// TODO: replace with GraphQL query for route => patterns => geometry
Expand All @@ -743,6 +722,10 @@ export function findRoute(params) {
return executeOTPAction('findRoute', params)
}

export function findRoutes(params) {
return executeOTPAction('findRoutes', params)
}

export function findPatternsForRoute(params) {
return executeOTPAction('findPatternsForRoute', params)
}
Expand Down
24 changes: 24 additions & 0 deletions lib/actions/apiV1.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
findRouteError,
findRouteResponse,
findRoutesAtStop,
findRoutesError,
findRoutesResponse,
findStopError,
findStopResponse,
findStopsForPattern,
Expand Down Expand Up @@ -234,10 +236,32 @@ export const findRoute = (params) =>
}
)

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 default {
fetchStopInfo,
findPatternsForRoute,
findRoute,
findRoutes,
findTrip,
getVehiclePositionsForRoute,
vehicleRentalQuery
Expand Down
65 changes: 65 additions & 0 deletions lib/actions/apiV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
findNearbyAmenitiesResponse,
findRouteError,
findRouteResponse,
findRoutesError,
findRoutesResponse,
findStopError,
findStopResponse,
findStopTimesForTrip,
Expand Down Expand Up @@ -610,6 +612,68 @@ export const findRoute = (params) =>
)
}

export function findRoutes() {
return function (dispatch) {
dispatch(
createGraphQLQueryAction(
`{
routes {
id: gtfsId
desc
agency {
id: gtfsId
name
url
timezone
lang
phone
}
longName
shortName
type
mode
color
textColor
bikesAllowed
routeBikesAllowed: bikesAllowed
}
}
`,
{},
findRoutesResponse,
findRoutesError,
{
noThrottle: true,
// TODO: avoid re-writing OTP2 route object to match OTP1 style
rewritePayload: (payload) => {
if (payload.errors) {
return dispatch(findRoutesError(payload.errors))
}
const { routes } = payload?.data
if (!routes) return

return routes.map(
({ agency, color, id, longName, mode, shortName }) => {
const agencyParts = agency.id.split(':')
return {
agencyId: agencyParts[agencyParts.length === 2 ? 1 : 0],
agencyName: agency.name,
color,
id,
longName,
mode,
shortName,
v2: true
}
}
)
}
}
)
)
}
}

export const findPatternsForRoute = (params) =>
function (dispatch, getState) {
const state = getState()
Expand All @@ -628,6 +692,7 @@ export default {
fetchStopInfo,
findPatternsForRoute,
findRoute,
findRoutes,
findTrip,
getVehiclePositionsForRoute,
vehicleRentalQuery
Expand Down

0 comments on commit d9d394c

Please sign in to comment.