Skip to content

Commit b9e07a1

Browse files
fix(SavedTripScreen): Initialize trip monitored days using itin transit leg svc date.
1 parent 3cbf55c commit b9e07a1

File tree

4 files changed

+91
-7
lines changed

4 files changed

+91
-7
lines changed

__tests__/util/itinerary.js

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
/* globals describe, expect, it */
22

3-
import { itineraryCanBeMonitored } from '../../lib/util/itinerary'
3+
import {
4+
getTransitItineraryDefaultMonitoredDays,
5+
itineraryCanBeMonitored
6+
} from '../../lib/util/itinerary'
7+
import { WEEKDAYS, WEEKEND_DAYS } from '../../lib/util/monitored-trip'
8+
9+
const walkLeg = {
10+
mode: 'WALK'
11+
}
412

513
describe('util > itinerary', () => {
614
describe('itineraryCanBeMonitored', () => {
@@ -20,9 +28,6 @@ describe('util > itinerary', () => {
2028
mode: 'MICROMOBILITY_RENT',
2129
rentedVehicle: true
2230
}
23-
const walkLeg = {
24-
mode: 'WALK'
25-
}
2631
const rideHailLeg = {
2732
mode: 'CAR_HAIL',
2833
hailedCar: true
@@ -80,4 +85,53 @@ describe('util > itinerary', () => {
8085
})
8186
})
8287
})
88+
describe('getTransitItineraryDefaultMonitoredDays', () => {
89+
const transitLegWeekday = {
90+
mode: 'BUS',
91+
serviceDate: '20210609', // Wendesday
92+
transitLeg: true
93+
}
94+
const transitLegSaturday = {
95+
mode: 'BUS',
96+
serviceDate: '20210612', // Saturday
97+
transitLeg: true
98+
}
99+
const transitLegSunday = {
100+
mode: 'BUS',
101+
serviceDate: '20210613', // Sunday
102+
transitLeg: true
103+
}
104+
105+
const testCases = [{
106+
expected: WEEKDAYS,
107+
itinerary: {
108+
legs: [walkLeg, transitLegWeekday]
109+
},
110+
title: 'should be [\'monday\' thru \'friday\'] for an itinerary starting on a weekday.'
111+
}, {
112+
expected: WEEKEND_DAYS,
113+
itinerary: {
114+
legs: [walkLeg, transitLegSaturday]
115+
},
116+
title: 'should be [\'saturday\', \'sunday\'] for an itinerary starting on a Saturday.'
117+
}, {
118+
expected: WEEKEND_DAYS,
119+
itinerary: {
120+
legs: [walkLeg, transitLegSunday]
121+
},
122+
title: 'should be [\'saturday\', \'sunday\'] for an itinerary starting on a Sunday.'
123+
}, {
124+
expected: null,
125+
itinerary: {
126+
legs: [walkLeg]
127+
},
128+
title: 'should be null for an itinerary without transit.'
129+
}]
130+
131+
testCases.forEach(({ expected, itinerary, title }) => {
132+
it(title, () => {
133+
expect(getTransitItineraryDefaultMonitoredDays(itinerary)).toBe(expected)
134+
})
135+
})
136+
})
83137
})

lib/components/user/monitored-trip/saved-trip-screen.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import TripBasicsPane from './trip-basics-pane'
1414
import TripNotificationsPane from './trip-notifications-pane'
1515
import TripSummaryPane from './trip-summary-pane'
1616
import { TRIPS_PATH } from '../../../util/constants'
17-
import { ALL_DAYS, arrayToDayFields, WEEKDAYS } from '../../../util/monitored-trip'
17+
import { getTransitItineraryDefaultMonitoredDays } from '../../../util/itinerary'
18+
import { ALL_DAYS, arrayToDayFields } from '../../../util/monitored-trip'
1819
import { getActiveItineraries, getActiveSearch } from '../../../util/state'
1920
import { RETURN_TO_CURRENT_ROUTE } from '../../../util/ui'
2021
import withLoggedInUserSupport from '../with-logged-in-user-support'
@@ -60,8 +61,9 @@ class SavedTripScreen extends Component {
6061
*/
6162
_createMonitoredTrip = () => {
6263
const { itinerary, loggedInUser, queryParams } = this.props
64+
const monitoredDays = getTransitItineraryDefaultMonitoredDays(itinerary)
6365
return {
64-
...arrayToDayFields(WEEKDAYS),
66+
...arrayToDayFields(monitoredDays),
6567
arrivalVarianceMinutesThreshold: 5,
6668
departureVarianceMinutesThreshold: 5,
6769
excludeFederalHolidays: true,

lib/util/itinerary.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { latLngBounds } from 'leaflet'
22
import moment from 'moment'
33
import coreUtils from '@opentripplanner/core-utils'
4+
import { WEEKDAYS, WEEKEND_DAYS } from './monitored-trip'
45

56
export function getLeafletItineraryBounds (itinerary) {
67
return latLngBounds(coreUtils.itinerary.getItineraryBounds(itinerary))
@@ -47,3 +48,29 @@ export function itineraryCanBeMonitored (itinerary) {
4748
export function getMinutesUntilItineraryStart (itinerary) {
4849
return moment(itinerary.startTime).diff(moment(), 'minutes')
4950
}
51+
52+
/**
53+
* Gets the first transit leg of the given itinerary, or null if none found.
54+
*/
55+
function getFirstTransitLeg (itinerary) {
56+
return itinerary?.legs?.find(leg => leg.transitLeg)
57+
}
58+
59+
/**
60+
* Returns the set of monitored days that will be initially shown to the user
61+
* for the given transit itinerary (itinerary with transit leg).
62+
* @param itinerary The itinerary from which the default monitored days are extracted.
63+
* @returns ['monday' thru 'friday'] if itinerary happens on a weekday(*),
64+
* ['saturday', 'sunday'] if itinerary happens on a saturday/sunday(*).
65+
* (*) The first transit leg will be used to make the determination.
66+
*/
67+
export function getTransitItineraryDefaultMonitoredDays (itinerary) {
68+
const firstTransitLeg = getFirstTransitLeg(itinerary)
69+
// Assume a transit leg exists.
70+
if (!firstTransitLeg) return null
71+
72+
const dayOfWeek = moment(firstTransitLeg.serviceDate, 'YYYYMMDD').day()
73+
return (dayOfWeek === 0 || dayOfWeek === 6)
74+
? WEEKEND_DAYS
75+
: WEEKDAYS
76+
}

lib/util/monitored-trip.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const WEEKDAYS = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']
2-
export const ALL_DAYS = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
2+
export const WEEKEND_DAYS = ['saturday', 'sunday']
3+
export const ALL_DAYS = [...WEEKDAYS, ...WEEKEND_DAYS]
34

45
/**
56
* Extracts the day of week fields of an object (e.g. a monitoredTrip) to an array.

0 commit comments

Comments
 (0)