Skip to content

Commit

Permalink
Merge pull request #1024 from opentripplanner/configurable-walk-options
Browse files Browse the repository at this point in the history
Add configurable strict mode to itinerary filter
  • Loading branch information
amy-corson-ibigroup authored Oct 16, 2023
2 parents 5e1ec55 + 91155fe commit ad14cde
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
3 changes: 3 additions & 0 deletions example-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ itinerary:
# Whether the plan first/previous/next/last buttons should be shown along with
# plan trip itineraries.
showPlanFirstLastButtons: false
# Filters out trips returned by OTP by default, unless specifically requested.
# e.g. filters out walk-only itineraries if user has not explicitly asked for them.
strictItineraryFiltering: false
# Whether to render route names and colors in the blocks inside
# the batch ui rows
renderRouteNamesInBlocks: true
Expand Down
31 changes: 27 additions & 4 deletions lib/actions/apiV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ import { RoutingQueryCallResult } from './api-constants'
import { setItineraryView } from './ui'
import { zoomToPlace } from './map'

const { generateCombinations, generateOtp2Query } = coreUtils.queryGen
const { generateCombinations, generateOtp2Query, SIMPLIFICATIONS } =
coreUtils.queryGen
const { getTripOptionsFromQuery, getUrlParams } = coreUtils.query
const { convertGraphQLResponseToLegacy } = coreUtils.itinerary
const { randId } = coreUtils.storage
Expand Down Expand Up @@ -825,6 +826,8 @@ export function routingQuery(searchId = null, updateSearchInReducer) {
config?.modes?.initialState?.enabledModeButtons ||
{}

const strictModes = config?.itinerary?.strictItineraryFiltering

// Filter mode definitions based on active mode keys
const activeModeButtons = config.modes?.modeButtons.filter((mb) =>
activeModeKeys.includes(mb.key)
Expand Down Expand Up @@ -919,11 +922,31 @@ export function routingQuery(searchId = null, updateSearchInReducer) {
routingError,
{
rewritePayload: (response, dispatch, getState) => {
const withCollapsedShortNames =
response.data?.plan?.itineraries?.map((itin) => ({
const itineraries = response.data?.plan?.itineraries

// Convert user-selected transit modes from mode selector into modes recognized by OTP.
const activeModeStrings = activeModes.map(
(am) => SIMPLIFICATIONS[am.mode]
)

let filteredItineraries = itineraries
// If "strictItineraryFiltering" is enabled, only return itineraries that contain at least one explicitly requested mode...
if (strictModes) {
filteredItineraries = itineraries.filter((itin) =>
itin.legs.some((leg) =>
activeModeStrings.includes(SIMPLIFICATIONS[leg.mode])
)
)
// ... Otherwise return all itineraries.
}

// Filter itineraries to collapse short names and hide unnecessary errors.
const withCollapsedShortNames = filteredItineraries.map(
(itin) => ({
...itin,
legs: itin.legs?.map(convertGraphQLResponseToLegacy)
}))
})
)

/* It is possible for a NO_TRANSIT_CONNECTION error to be
returned even if trips were returned, since it is on a mode-by-mode basis.
Expand Down

0 comments on commit ad14cde

Please sign in to comment.