Skip to content

Commit

Permalink
Merge branch 'remove-1-2-parallel' into percy
Browse files Browse the repository at this point in the history
  • Loading branch information
miles-grant-ibigroup committed Mar 18, 2022
2 parents faf415d + 24adf0b commit 2869ac2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 60 deletions.
44 changes: 25 additions & 19 deletions lib/actions/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ const { getRoutingParams, getTripOptionsFromQuery, getUrlParams } =
coreUtils.query
const { randId } = coreUtils.storage

// set actions variable that can be set to v2Actions if v2 server is set
let actions = v1Actions

// Generic API actions

/*
Expand Down Expand Up @@ -91,6 +88,24 @@ function getActiveItinerary(state) {
return activeItinerary
}

/**
* Dispatches a method from either v1actions or v2actions, depending on
* which version of OTP is specified in the config.
* @param {*} methodName the method to execute
* @param {...any} params varargs of params to send to the action
*/
function executeOTPAction(methodName, ...params) {
return function (dispatch, getState) {
const state = getState()
const { api } = state.otp.config
return dispatch(
api?.v2
? v2Actions[methodName](...params)
: v1Actions[methodName](...params)
)
}
}

/**
* Send a routing query to the OTP backend.
*
Expand Down Expand Up @@ -248,7 +263,6 @@ function getOtpFetchOptions(state, includeToken = false) {
let apiBaseUrl, apiKey, token

const { api, persistence } = state.otp.config
if (api.v2) actions = v2Actions
if (persistence && persistence.otp_middleware) {
// Prettier does not understand the syntax on this line
// eslint-disable-next-line prettier/prettier
Expand Down Expand Up @@ -350,7 +364,8 @@ export function vehicleRentalQuery(
errorAction = vehicleRentalError,
options = {}
) {
return actions.vehicleRentalQuery(
return executeOTPAction(
'vehicleRentalQuery',
params,
responseAction,
errorAction,
Expand All @@ -363,7 +378,7 @@ export const findStopResponse = createAction('FIND_STOP_RESPONSE')
export const findStopError = createAction('FIND_STOP_ERROR')

export function fetchStopInfo(stop) {
return actions.fetchStopInfo(stop)
return executeOTPAction('fetchStopInfo', stop)
}

// Single trip lookup query
Expand All @@ -372,7 +387,7 @@ export const findTripResponse = createAction('FIND_TRIP_RESPONSE')
export const findTripError = createAction('FIND_TRIP_ERROR')

export function findTrip(params) {
return actions.findTrip(params)
return executeOTPAction('findTrip', params)
}

// Stops for trip query
Expand Down Expand Up @@ -545,20 +560,11 @@ export const findRouteResponse = createAction('FIND_ROUTE_RESPONSE')
export const findRouteError = createAction('FIND_ROUTE_ERROR')

export function findRoute(params) {
return function (dispatch, getState) {
const state = getState()
const { api } = state.otp.config
// Because findRoute is sometimes called from strange places,
// we must explicitly check for V2 as we can not rely on
// the check having already happened
if (api.v2) actions = v2Actions

return dispatch(actions.findRoute(params))
}
return executeOTPAction('findRoute', params)
}

export function findPatternsForRoute(params) {
return actions.findPatternsForRoute(params)
return executeOTPAction('findPatternsForRoute', params)
}

// Geometry for Pattern lookup query
Expand Down Expand Up @@ -768,7 +774,7 @@ export const receivedVehiclePositionsError = createAction(
)

export function getVehiclePositionsForRoute(routeId) {
return actions.getVehiclePositionsForRoute(routeId)
return executeOTPAction('getVehiclePositionsForRoute', routeId)
}

const throttledUrls = {}
Expand Down
28 changes: 13 additions & 15 deletions lib/actions/apiV1.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ export function vehicleRentalQuery(
return createQueryAction(endpoint, responseAction, errorAction, options)
}

function findNearbyAmenities(params, stopId) {
function findNearbyAmenities({ lat, lon, radius = 300 }, stopId) {
return function (dispatch, getState) {
const { lat, lon, radius } = params
const bounds = L.latLng(lat, lon).toBounds(radius)
const { lat: low, lng: left } = bounds.getSouthWest()
const { lat: up, lng: right } = bounds.getNorthEast()
Expand Down Expand Up @@ -109,8 +108,8 @@ function findNearbyAmenities(params, stopId) {
}
}

export function findStop(params) {
return createQueryAction(
export const findStop = (params) =>
createQueryAction(
`index/stops/${params.stopId}`,
findStopResponse,
findStopError,
Expand All @@ -123,32 +122,33 @@ export function findStop(params) {
serviceId: 'stops'
}
)
}

const fetchStopInfo = (stop) =>
async function (dispatch, getState) {
await dispatch(findStop({ stopId: stop.stopId }))
const state = getState()
const { nearbyRadius: radius } = state.otp.config.stopViewer
const { nearbyRadius } = state.otp?.config?.stopViewer
const fetchedStop = state.otp.transitIndex.stops?.[stop?.stopId]
// TODO: stop not found message
if (!fetchedStop) return

const { lat, lon } = fetchedStop
if (radius > 0) {
if (nearbyRadius > 0) {
dispatch(
findNearbyStops(
{
includeRoutes: true,
includeStopTimes: true,
lat,
lon,
radius
nearbyRadius
},
stop.stopId
)
)
dispatch(findNearbyAmenities({ lat, lon, radius }, stop.stopId))
dispatch(
findNearbyAmenities({ lat, lon, radius: nearbyRadius }, stop.stopId)
)
dispatch(zoomToStop(fetchedStop))
}
}
Expand All @@ -168,8 +168,8 @@ const getVehiclePositionsForRoute = (routeId) =>
}
)

export function findPatternsForRoute(params) {
return createQueryAction(
export const findPatternsForRoute = (params) =>
createQueryAction(
`index/routes/${params.routeId}/patterns?includeGeometry=true`,
findPatternsForRouteResponse,
findPatternsForRouteError,
Expand Down Expand Up @@ -205,10 +205,9 @@ export function findPatternsForRoute(params) {
}
}
)
}

export function findRoute(params) {
return createQueryAction(
export const findRoute = (params) =>
createQueryAction(
`index/routes/${params.routeId}`,
findRouteResponse,
findRouteError,
Expand All @@ -221,7 +220,6 @@ export function findRoute(params) {
}
}
)
}

export default {
fetchStopInfo,
Expand Down
61 changes: 38 additions & 23 deletions lib/actions/apiV2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import clone from 'clone'

import {
createQueryAction,
findGeometryForTrip,
Expand Down Expand Up @@ -73,7 +75,8 @@ const findTrip = (params) =>
tripBikesAllowed: bikesAllowed
stops {
gtfsId: id
id: gtfsId
stopId: gtfsId
code
name
lat
Expand Down Expand Up @@ -105,14 +108,14 @@ const findTrip = (params) =>
}
)

export function vehicleRentalQuery(
export const vehicleRentalQuery = (
params,
responseAction,
errorAction,
options
) {
) =>
// TODO: ErrorsByNetwork is missing
return createGraphQLQueryAction(
createGraphQLQueryAction(
`{
rentalVehicles {
vehicleId
Expand Down Expand Up @@ -153,7 +156,6 @@ export function vehicleRentalQuery(
}
}
)
}

const stopTimeGraphQLQuery = `
stopTimes: stoptimesForPatterns(numberOfDepartures: 3) {
Expand Down Expand Up @@ -255,6 +257,9 @@ const findNearbyAmenities = ({ lat, lon, radius = 300, stopId }) => {
id
lat
lon
...on VehicleRentalStation {
network
}
}
distance
}
Expand All @@ -267,6 +272,9 @@ const findNearbyAmenities = ({ lat, lon, radius = 300, stopId }) => {
id
lat
lon
...on RentalVehicle {
network
}
}
distance
}
Expand Down Expand Up @@ -308,7 +316,9 @@ const findNearbyAmenities = ({ lat, lon, radius = 300, stopId }) => {
lat: edge.node?.place?.lat,
lon: edge.node?.place?.lon,
name: edge.node?.place?.id || '',
networks: ['null']
networks: [edge.node?.place?.network || 'null'],
x: edge.node?.place?.lon,
y: edge.node?.place?.lat
}
})
},
Expand All @@ -321,7 +331,9 @@ const findNearbyAmenities = ({ lat, lon, radius = 300, stopId }) => {
lat: edge.node?.place?.lat,
lon: edge.node?.place?.lon,
name: edge.node?.place?.id || '',
networks: ['null']
networks: ['null'],
x: edge.node?.place?.lon,
y: edge.node?.place?.lat
}
}
),
Expand All @@ -335,7 +347,9 @@ const findNearbyAmenities = ({ lat, lon, radius = 300, stopId }) => {
lat: edge.node?.place?.lat,
lon: edge.node?.place?.lon,
name: edge.node?.place?.id || '',
networks: ['null']
networks: [edge.node?.place?.network || 'null'],
x: edge.node?.place?.lon,
y: edge.node?.place?.lat
}
})
}
Expand All @@ -347,7 +361,10 @@ const findNearbyAmenities = ({ lat, lon, radius = 300, stopId }) => {

const fetchStopInfo = (stop) => {
const { stopId } = stop
if (!stopId) return {}
if (!stopId)
return function (dispatch, getState) {
console.warn("No stopId passed, can't fetch stop!")
}

return createGraphQLQueryAction(
`{
Expand All @@ -366,7 +383,7 @@ const fetchStopInfo = (stop) => {
return findStopError(payload.errors)
}
const { stop } = payload?.data
if (!stop) return findStopError()
if (!stop || !stop.lat || !stop.lon) return findStopError()

// Fetch nearby stops and amenities
// TODO: add radius from config
Expand Down Expand Up @@ -411,14 +428,13 @@ const fetchStopInfo = (stop) => {
)
}

const getVehiclePositionsForRoute = () => {
return function (dispatch, getState) {
const getVehiclePositionsForRoute = () =>
function (dispatch, getState) {
console.warn('OTP2 does not yet support vehicle positions for route!')
}
}

export function findRoute(params) {
return function (dispatch, getState) {
export const findRoute = (params) =>
function (dispatch, getState) {
const { routeId } = params
if (!routeId) return

Expand Down Expand Up @@ -481,8 +497,9 @@ export function findRoute(params) {
const { route } = payload?.data
if (!route) return

const newRoute = clone(route)
const routePatterns = {}
route.patterns.forEach((pattern) => {
newRoute.patterns.forEach((pattern) => {
const patternStops = pattern.stops.map((stop) => {
const color =
stop.routes?.length > 0 && `#${stop.routes[0].color}`
Expand All @@ -496,20 +513,19 @@ export function findRoute(params) {
stops: patternStops
}
})
route.patterns = routePatterns
newRoute.patterns = routePatterns
// TODO: avoid explicit behavior shift like this
route.v2 = true
newRoute.v2 = true

return route
return newRoute
}
}
)
)
}
}

export function findPatternsForRoute(params) {
return function (dispatch, getState) {
export const findPatternsForRoute = (params) =>
function (dispatch, getState) {
const state = getState()
const { routeId } = params
const route = state?.otp?.transitIndex?.routes?.[routeId]
Expand All @@ -521,7 +537,6 @@ export function findPatternsForRoute(params) {
return dispatch(findRoute(params))
}
}
}

export default {
fetchStopInfo,
Expand Down
4 changes: 2 additions & 2 deletions lib/actions/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ export function matchContentToUrl(location) {
case 'route':
if (id) {
// This is a bit of a hack to check if the route details have been grabbed
// agencyName will only be populated if route details are repsent
// bikesAllowed will only be populated if route details are present
// Moving away from manual requests should help resolve this
if (!state.otp.transitIndex?.routes?.[id]?.agencyName) {
if (!state.otp.transitIndex?.routes?.[id]?.bikesAllowed) {
dispatch(findRoute({ routeId: id }))
}
// Check for pattern "submatch"
Expand Down
Loading

0 comments on commit 2869ac2

Please sign in to comment.