Skip to content

Commit

Permalink
Merge pull request #1190 from opentripplanner/support-route-viewer-so…
Browse files Browse the repository at this point in the history
…rt-in-nearby-view

Support route viewer sort in nearby view
  • Loading branch information
miles-grant-ibigroup authored May 6, 2024
2 parents 5f21292 + 9bf0ed5 commit 147e870
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7122,6 +7122,7 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = `
/>
}
homeTimezone="America/Los_Angeles"
routeSortComparator={[Function]}
stopData={
Object {
"__typename": "Stop",
Expand Down Expand Up @@ -14945,6 +14946,7 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = `
/>
}
homeTimezone="America/Los_Angeles"
routeSortComparator={[Function]}
stopData={
Object {
"__typename": "Stop",
Expand Down Expand Up @@ -23041,6 +23043,7 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = `
/>
}
homeTimezone="America/Los_Angeles"
routeSortComparator={[Function]}
stopData={
Object {
"__typename": "Stop",
Expand Down Expand Up @@ -33944,6 +33947,7 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = `
/>
}
homeTimezone="America/Los_Angeles"
routeSortComparator={[Function]}
stopData={
Object {
"__typename": "Stop",
Expand Down Expand Up @@ -40874,6 +40878,7 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = `
/>
}
homeTimezone="America/Los_Angeles"
routeSortComparator={[Function]}
stopData={
Object {
"__typename": "Stop",
Expand Down Expand Up @@ -47900,6 +47905,7 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = `
/>
}
homeTimezone="America/Los_Angeles"
routeSortComparator={[Function]}
stopData={
Object {
"__typename": "Stop",
Expand Down Expand Up @@ -59395,6 +59401,7 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = `
/>
}
homeTimezone="America/Los_Angeles"
routeSortComparator={[Function]}
stopData={
Object {
"__typename": "Stop",
Expand Down
4 changes: 3 additions & 1 deletion example-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -662,4 +662,6 @@ itinerary:
### Setting to hide stops with no depatures in the nearby view.
# hideEmptyStops: true
### What radius should the nearby query get results within? (in meters)
# radius: 600
# radius: 600
### Setting this to true will use the route viewer sort algorithm on routes
# useRouteViewSort: false
2 changes: 1 addition & 1 deletion i18n/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -521,13 +521,13 @@ components:
signInText: Inicia sesión para guardar el viaje
signInTooltip: Por favor, inicia sesión para guardar el viaje.
SavedTripEditor:
deleteSavedTrip: Borrar viaje guardado
editSavedTrip: Editar el viaje guardado
saveNewTrip: Guardar un nuevo viaje
tripInformation: Información sobre el viaje
tripNotFound: No se encontró el viaje
tripNotFoundDescription: Lo sentimos, no se encontró el viaje solicitado.
tripNotifications: Notificaciones de viaje
deleteSavedTrip: Borrar viaje guardado
SavedTripList:
alertTag: "{alerta, plural, one {Ver una alerta} other {Ver # alertas}}"
fromTo: De {from} al {to}
Expand Down
27 changes: 22 additions & 5 deletions lib/components/viewers/nearby/stop.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Calendar } from '@styled-icons/fa-solid'
import { connect } from 'react-redux'
import { FormattedMessage } from 'react-intl'
import { TransitOperator } from '@opentripplanner/types'
import coreUtils from '@opentripplanner/core-utils'
import React from 'react'

Expand All @@ -23,13 +24,15 @@ type Props = {
fromToSlot: JSX.Element
homeTimezone: string
nearbyViewConfig?: NearbyViewConfig
routeSortComparator: (a: PatternStopTime, b: PatternStopTime) => number
stopData: StopData
}

const Stop = ({
fromToSlot,
homeTimezone,
nearbyViewConfig,
routeSortComparator,
stopData
}: Props): JSX.Element => {
const patternRows = (stopData.stoptimesForPatterns || [])
Expand All @@ -52,10 +55,7 @@ const Stop = ({
}
return acc
}, [])
.sort(
(a: PatternStopTime, b: PatternStopTime) =>
fullTimestamp(a.stoptimes?.[0]) - fullTimestamp(b.stoptimes?.[0])
)
.sort(routeSortComparator)
.map((st: any, index: number) => {
const sortedStopTimes = st.stoptimes.sort(
(a: StopTime, b: StopTime) => fullTimestamp(a) - fullTimestamp(b)
Expand Down Expand Up @@ -103,9 +103,26 @@ const Stop = ({

const mapStateToProps = (state: AppReduxState) => {
const { config } = state.otp
const nearbyViewConfig = config?.nearbyView
const transitOperators = config?.transitOperators || []

// Default sort: departure time
let routeSortComparator = (a: PatternStopTime, b: PatternStopTime) =>
fullTimestamp(a.stoptimes?.[0]) - fullTimestamp(b.stoptimes?.[0])

if (nearbyViewConfig?.useRouteViewSort) {
routeSortComparator = (a: PatternStopTime, b: PatternStopTime) =>
coreUtils.route.makeRouteComparator(transitOperators)(
// @ts-expect-error core-utils types are wrong!
a.pattern.route,
b.pattern.route
)
}

return {
homeTimezone: config.homeTimezone,
nearbyViewConfig: config?.nearbyView
nearbyViewConfig,
routeSortComparator
}
}

Expand Down
6 changes: 5 additions & 1 deletion lib/util/config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ interface ApiKeyConfig {
export type BugsnagConfig = ApiKeyConfig
export type MapillaryConfig = ApiKeyConfig

export type NearbyViewConfig = { hideEmptyStops?: boolean; radius?: number }
export type NearbyViewConfig = {
hideEmptyStops?: boolean
radius?: number
useRouteViewSort?: boolean
}

/** TODO: Language settings */
export type LanguageConfig = Record<string, any>
Expand Down

0 comments on commit 147e870

Please sign in to comment.