1
- import { OTP_API_DATE_FORMAT , OTP_API_TIME_FORMAT } from '@opentripplanner/core-utils/lib/time'
2
- import { getTimeZoneOffset } from '@opentripplanner/core-utils/lib/itinerary'
1
+ import { getTimeZoneOffset } from '@opentripplanner/core-utils/lib/itinerary'
2
+ import {
3
+ OTP_API_DATE_FORMAT ,
4
+ OTP_API_TIME_FORMAT
5
+ } from '@opentripplanner/core-utils/lib/time'
3
6
import moment from 'moment'
4
7
5
- import { getFirstStopId } from '../util/itinerary '
6
- import { getActiveItinerary } from '../util/state '
8
+ import { getActiveItinerary } from '../util/state '
9
+ import { getFirstStopId } from '../util/itinerary '
7
10
8
- import { routingQuery } from './api'
9
- import { setQueryParam } from './form'
11
+ import { routingQuery } from './api'
12
+ import { setQueryParam } from './form'
10
13
11
14
const SERVICE_BREAK = '03:00'
12
15
const NINETY_SECONDS = 90000
13
16
14
- function updateParamsAndPlan ( params ) {
17
+ function updateParamsAndPlan ( params ) {
15
18
return function ( dispatch , getState ) {
16
19
dispatch ( setQueryParam ( params ) )
17
20
dispatch ( routingQuery ( ) )
18
21
}
19
22
}
20
23
21
- function offsetTime ( itinerary , unixTime ) {
24
+ function offsetTime ( itinerary , unixTime ) {
22
25
let offset = 0
23
26
if ( itinerary ) offset = getTimeZoneOffset ( itinerary )
24
27
return moment ( unixTime + offset )
@@ -28,30 +31,32 @@ function offsetTime (itinerary, unixTime) {
28
31
* Effectively checks whether #planFirst has already been clicked, i.e., the
29
32
* query is planning a depart at the service break time.
30
33
*/
31
- function isPlanningFirst ( query ) {
32
- const { departArrive, time} = query
34
+ function isPlanningFirst ( query ) {
35
+ const { departArrive, time } = query
33
36
return departArrive === 'DEPART' && time === SERVICE_BREAK
34
37
}
35
38
36
39
/**
37
40
* Plan the first trip of the day, or if the first trip has already been planned,
38
41
* plan the first trip of the previous day.
39
42
*/
40
- export function planFirst ( ) {
43
+ export function planFirst ( ) {
41
44
return function ( dispatch , getState ) {
42
45
const state = getState ( )
43
46
const itinerary = getActiveItinerary ( state )
44
- const { currentQuery} = state . otp
47
+ const { currentQuery } = state . otp
45
48
const date = moment ( currentQuery . date )
46
49
// If already planning for the "first" trip, subtract a day to mirror the
47
50
// behavior of planLast.
48
51
if ( isPlanningFirst ( currentQuery ) ) date . subtract ( 'days' , 1 )
49
52
const params = {
50
53
date : date . format ( OTP_API_DATE_FORMAT ) ,
51
54
departArrive : 'DEPART' ,
52
- startTransitStopId : getFirstStopId ( itinerary ) ,
53
55
time : SERVICE_BREAK
54
56
}
57
+ if ( ! state . otp ?. config ?. api ?. v2 ) {
58
+ params . startTransitStopId = getFirstStopId ( itinerary )
59
+ }
55
60
dispatch ( updateParamsAndPlan ( params ) )
56
61
}
57
62
}
@@ -60,16 +65,18 @@ export function planFirst () {
60
65
* Plan the previous trip, setting the arrive by time to the current itinerary's
61
66
* end time (minus a small amount).
62
67
*/
63
- export function planPrevious ( ) {
68
+ export function planPrevious ( ) {
64
69
return function ( dispatch , getState ) {
65
70
const itinerary = getActiveItinerary ( getState ( ) )
66
71
const newEndTime = offsetTime ( itinerary , itinerary . endTime - NINETY_SECONDS )
67
72
const params = {
68
73
date : newEndTime . format ( OTP_API_DATE_FORMAT ) ,
69
74
departArrive : 'ARRIVE' ,
70
- startTransitStopId : getFirstStopId ( itinerary ) ,
71
75
time : newEndTime . format ( OTP_API_TIME_FORMAT )
72
76
}
77
+ if ( ! getState ( ) . otp ?. config ?. api ?. v2 ) {
78
+ params . startTransitStopId = getFirstStopId ( itinerary )
79
+ }
73
80
dispatch ( updateParamsAndPlan ( params ) )
74
81
}
75
82
}
@@ -78,16 +85,22 @@ export function planPrevious () {
78
85
* Plan the next trip, setting the depart at time to the current itinerary's
79
86
* start time (plus a small amount).
80
87
*/
81
- export function planNext ( ) {
88
+ export function planNext ( ) {
82
89
return function ( dispatch , getState ) {
83
90
const itinerary = getActiveItinerary ( getState ( ) )
84
- const newStartTime = offsetTime ( itinerary , itinerary . startTime + NINETY_SECONDS )
91
+ const newStartTime = offsetTime (
92
+ itinerary ,
93
+ itinerary . startTime + NINETY_SECONDS
94
+ )
85
95
const params = {
86
96
date : newStartTime . format ( OTP_API_DATE_FORMAT ) ,
87
97
departArrive : 'DEPART' ,
88
- startTransitStopId : getFirstStopId ( itinerary ) ,
89
98
time : newStartTime . format ( OTP_API_TIME_FORMAT )
90
99
}
100
+ if ( ! getState ( ) . otp ?. config ?. api ?. v2 ) {
101
+ params . startTransitStopId = getFirstStopId ( itinerary )
102
+ }
103
+
91
104
dispatch ( updateParamsAndPlan ( params ) )
92
105
}
93
106
}
@@ -96,17 +109,22 @@ export function planNext () {
96
109
* Plan the last trip of the day, or if the last trip has already been planned,
97
110
* plan the last trip of the next day.
98
111
*/
99
- export function planLast ( ) {
112
+ export function planLast ( ) {
100
113
return function ( dispatch , getState ) {
101
114
const state = getState ( )
102
115
const itinerary = getActiveItinerary ( state )
103
- const { currentQuery} = state . otp
116
+ const { currentQuery } = state . otp
104
117
const params = {
105
- date : moment ( currentQuery . date ) . add ( 'days' , 1 ) . format ( OTP_API_DATE_FORMAT ) ,
118
+ date : moment ( currentQuery . date )
119
+ . add ( 'days' , 1 )
120
+ . format ( OTP_API_DATE_FORMAT ) ,
106
121
departArrive : 'ARRIVE' ,
107
- startTransitStopId : getFirstStopId ( itinerary ) ,
108
122
time : SERVICE_BREAK
109
123
}
124
+ if ( ! state . otp ?. config ?. api ?. v2 ) {
125
+ params . startTransitStopId = getFirstStopId ( itinerary )
126
+ }
127
+
110
128
dispatch ( updateParamsAndPlan ( params ) )
111
129
}
112
130
}
0 commit comments