Skip to content

Commit f2b0a69

Browse files
Merge pull request #1259 from opentripplanner/date-restricted-requests
only show routes from current service week
2 parents 68d9c86 + 0478ca2 commit f2b0a69

File tree

10 files changed

+2635
-4922
lines changed

10 files changed

+2635
-4922
lines changed

__tests__/components/viewers/__snapshots__/nearby-view.js.snap

Lines changed: 543 additions & 0 deletions
Large diffs are not rendered by default.

example-config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,8 @@ disableSingleItineraryDays: false
677677
# maxRealtimeVehicleAge: 60
678678
# # Interval for refreshing vehicle positions
679679
# vehiclePositionRefreshSeconds: 30 # defaults to 30 seconds.
680+
# # Enable this to restrict listing to routes active within the last to next Sunday
681+
# onlyShowCurrentServiceWeek: true
680682

681683
# API key to make Mapillary API calls. These are used to show street imagery.
682684
# Mapillary calls these "Client Tokens". They can be created at https://www.mapillary.com/dashboard/developers

lib/actions/api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ export function vehicleRentalQuery(
370370
export const fetchNearbyResponse = createAction('FETCH_NEARBY_RESPONSE')
371371
export const fetchNearbyError = createAction('FETCH_NEARBY_ERROR')
372372

373-
export function fetchNearby(coords, map) {
374-
return executeOTPAction('fetchNearby', coords, map)
373+
export function fetchNearby(coords, map, currentServiceWeek) {
374+
return executeOTPAction('fetchNearby', coords, map, currentServiceWeek)
375375
}
376376

377377
// Single trip lookup query

lib/actions/apiV2.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
isValidSubsequence,
2424
queryIsValid
2525
} from '../util/state'
26+
import { getCurrentServiceWeek } from '../util/current-service-week'
2627
import {
2728
getRouteColorBasedOnSettings,
2829
getRouteIdForPattern,
@@ -380,14 +381,15 @@ export const fetchNearbyFromStopId = (stopId) => {
380381
)
381382
}
382383

383-
export const fetchNearby = (position, radius) => {
384+
export const fetchNearby = (position, radius, currentServiceWeek) => {
384385
const { lat, lon } = position
385386

386387
return createGraphQLQueryAction(
387388
`query Nearby(
388389
$lat: Float!
389390
$lon: Float!
390391
$radius: Int
392+
$currentServiceWeek: LocalDateRangeInput
391393
) {
392394
nearest(lat:$lat, lon:$lon, maxDistance: $radius, first: 100, filterByPlaceTypes: [STOP, VEHICLE_RENT, BIKE_PARK, CAR_PARK]) {
393395
edges {
@@ -437,11 +439,15 @@ export const fetchNearby = (position, radius) => {
437439
lon
438440
code
439441
gtfsId
442+
stopRoutes: routes (serviceDates: $currentServiceWeek) {
443+
gtfsId
444+
}
440445
stoptimesForPatterns {
441446
pattern {
442447
headsign
443448
desc: name
444449
route {
450+
gtfsId
445451
agency {
446452
name
447453
gtfsId
@@ -475,7 +481,7 @@ export const fetchNearby = (position, radius) => {
475481
}
476482
}
477483
}`,
478-
{ lat, lon, radius },
484+
{ currentServiceWeek, lat, lon, radius },
479485
fetchNearbyResponse,
480486
fetchNearbyError,
481487
{
@@ -858,10 +864,18 @@ export const findRoute = (params) =>
858864

859865
export function findRoutes() {
860866
return function (dispatch, getState) {
867+
// Only calculate current service week if the setting for it is enabled
868+
const currentServiceWeek =
869+
getState().otp?.config?.routeViewer?.onlyShowCurrentServiceWeek === true
870+
? getCurrentServiceWeek()
871+
: undefined
872+
861873
dispatch(
862874
createGraphQLQueryAction(
863-
`{
864-
routes {
875+
`query Routes(
876+
$currentServiceWeek: LocalDateRangeInput
877+
) {
878+
routes (serviceDates: $currentServiceWeek) {
865879
id: gtfsId
866880
agency {
867881
id: gtfsId
@@ -876,7 +890,7 @@ export function findRoutes() {
876890
}
877891
}
878892
`,
879-
{},
893+
{ currentServiceWeek },
880894
findRoutesResponse,
881895
findRoutesError,
882896
{

lib/components/viewers/nearby/nearby-view.tsx

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as apiActions from '../../../actions/api'
88
import * as mapActions from '../../../actions/map'
99
import * as uiActions from '../../../actions/ui'
1010
import { AppReduxState } from '../../../util/state-types'
11+
import { getCurrentServiceWeek } from '../../../util/current-service-week'
1112
import { SetLocationHandler, ZoomToPlaceHandler } from '../../util/types'
1213
import Loading from '../../narrative/loading'
1314
import MobileContainer from '../../mobile/container'
@@ -31,13 +32,19 @@ const AUTO_REFRESH_INTERVAL = 15000
3132
// TODO: use lonlat package
3233
type LatLonObj = { lat: number; lon: number }
3334
type CurrentPosition = { coords?: { latitude: number; longitude: number } }
35+
type ServiceWeek = { end: string; start: string }
3436

3537
type Props = {
3638
currentPosition?: CurrentPosition
39+
currentServiceWeek?: ServiceWeek
3740
defaultLatLon: LatLonObj | null
3841
displayedCoords?: LatLonObj
3942
entityId?: string
40-
fetchNearby: (latLon: LatLonObj, radius?: number) => void
43+
fetchNearby: (
44+
latLon: LatLonObj,
45+
radius?: number,
46+
currentServiceWeek?: ServiceWeek
47+
) => void
4148
hideBackButton?: boolean
4249
location: string
4350
mobile?: boolean
@@ -102,6 +109,7 @@ function getNearbyCoordsFromUrlOrLocationOrMapCenter(
102109

103110
function NearbyView({
104111
currentPosition,
112+
currentServiceWeek,
105113
defaultLatLon,
106114
displayedCoords,
107115
entityId,
@@ -175,10 +183,10 @@ function NearbyView({
175183
firstItemRef.current?.scrollIntoView({ behavior: 'smooth' })
176184
}
177185
if (finalNearbyCoords) {
178-
fetchNearby(finalNearbyCoords, radius)
186+
fetchNearby(finalNearbyCoords, radius, currentServiceWeek)
179187
setLoading(true)
180188
const interval = setInterval(() => {
181-
fetchNearby(finalNearbyCoords, radius)
189+
fetchNearby(finalNearbyCoords, radius, currentServiceWeek)
182190
setLoading(true)
183191
}, AUTO_REFRESH_INTERVAL)
184192
return function cleanup() {
@@ -204,6 +212,16 @@ function NearbyView({
204212
finalNearbyCoords?.lat !== displayedCoords?.lat ||
205213
finalNearbyCoords?.lon !== displayedCoords?.lon
206214

215+
// Build list of nearby routes for filtering within the stop card
216+
const nearbyRoutes = Array.from(
217+
new Set(
218+
nearby
219+
?.map((n: any) =>
220+
n.place?.stopRoutes?.map((sr: { gtfsId?: string }) => sr?.gtfsId)
221+
)
222+
.flat(Infinity)
223+
)
224+
)
207225
const nearbyItemList =
208226
nearby?.map &&
209227
nearby?.map((n: any) => (
@@ -223,7 +241,7 @@ function NearbyView({
223241
/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */
224242
tabIndex={0}
225243
>
226-
{getNearbyItem({ ...n.place, distance: n.distance })}
244+
{getNearbyItem({ ...n.place, distance: n.distance, nearbyRoutes })}
227245
</div>
228246
</li>
229247
))
@@ -290,15 +308,22 @@ function NearbyView({
290308

291309
const mapStateToProps = (state: AppReduxState) => {
292310
const { config, location, transitIndex, ui } = state.otp
293-
const { map } = state.otp.config
311+
const { map, routeViewer } = config
294312
const { nearbyViewCoords } = ui
295313
const { nearby } = transitIndex
296314
const { entityId } = state.router.location.query
297315
const { currentPosition } = location
298316
const defaultLatLon =
299317
map?.initLat && map?.initLon ? { lat: map.initLat, lon: map.initLon } : null
318+
319+
const currentServiceWeek =
320+
routeViewer?.onlyShowCurrentServiceWeek === true
321+
? getCurrentServiceWeek()
322+
: undefined
323+
300324
return {
301325
currentPosition,
326+
currentServiceWeek,
302327
defaultLatLon,
303328
displayedCoords: nearby?.coords,
304329
entityId: entityId && decodeURIComponent(entityId),

lib/components/viewers/nearby/stop.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Calendar } from '@styled-icons/fa-solid'
22
import { connect } from 'react-redux'
33
import { FormattedMessage } from 'react-intl'
4-
import { TransitOperator } from '@opentripplanner/types'
54
import coreUtils from '@opentripplanner/core-utils'
65
import React from 'react'
76

@@ -25,7 +24,7 @@ type Props = {
2524
homeTimezone: string
2625
nearbyViewConfig?: NearbyViewConfig
2726
routeSortComparator: (a: PatternStopTime, b: PatternStopTime) => number
28-
stopData: StopData
27+
stopData: StopData & { nearbyRoutes?: string[] }
2928
}
3029

3130
const Stop = ({
@@ -70,6 +69,14 @@ const Stop = ({
7069
const sortedStopTimes = st.stoptimes.sort(
7170
(a: StopTime, b: StopTime) => fullTimestamp(a) - fullTimestamp(b)
7271
)
72+
if (
73+
// NearbyRoutes if present is populated with a list of routes that appear
74+
// in the current service period.
75+
stopData.nearbyRoutes &&
76+
!stopData.nearbyRoutes.includes(st?.pattern?.route?.gtfsId)
77+
) {
78+
return <></>
79+
}
7380
return (
7481
<PatternRow
7582
alwaysShowLongName={nearbyViewConfig?.alwaysShowLongName}

lib/util/config-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ export interface RouteViewerConfig {
360360
hideRouteShapesWithinFlexZones?: boolean
361361
/** Remove vehicles from the map if they haven't sent an update in a number of seconds */
362362
maxRealtimeVehicleAge?: number
363+
/** Use OTP date limiting to only show current service week in list */
364+
onlyShowCurrentServiceWeek?: boolean
363365
/** Disable vehicle highlight if necessary (e.g. custom or inverted icons) */
364366
vehicleIconHighlight?: boolean
365367
/** Customize vehicle icon padding (the default iconPadding is 2px in otp-ui) */

lib/util/current-service-week.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { addDays, nextSunday, previousSunday } from 'date-fns'
2+
/** Gets the current service week */
3+
export const getCurrentServiceWeek = (): {
4+
end: string
5+
start: string
6+
} => {
7+
const start = previousSunday(new Date()).toISOString().split('T')[0]
8+
const end = addDays(nextSunday(new Date()), 1).toISOString().split('T')[0]
9+
return { end, start }
10+
}

0 commit comments

Comments
 (0)