1
+ /* globals fetch */
2
+
1
3
import deepEqual from 'deep-equal'
2
4
import { createAction } from 'redux-actions'
3
- import fetch from 'isomorphic-fetch'
4
5
import qs from 'qs'
5
6
7
+ if ( typeof ( fetch ) === 'undefined' ) {
8
+ require ( 'isomorphic-fetch' )
9
+ }
10
+
6
11
import { hasValidLocation } from '../util/state'
7
12
8
13
export const receivedPlanResponse = createAction ( 'PLAN_RESPONSE' )
9
14
export const requestPlanResponse = createAction ( 'PLAN_REQUEST' )
10
15
11
- export function planTrip ( ) {
16
+ export function planTrip ( customOtpQueryBuilder ) {
12
17
return function ( dispatch , getState ) {
13
18
const otpState = getState ( ) . otp
14
19
const latest = otpState . searches . length && otpState . searches [ otpState . searches . length - 1 ]
@@ -19,7 +24,8 @@ export function planTrip () {
19
24
}
20
25
if ( ! hasValidLocation ( otpState , 'from' ) || ! hasValidLocation ( otpState , 'to' ) ) return // TODO: replace with isQueryValid?
21
26
dispatch ( requestPlanResponse ( ) )
22
- const url = constructPlanQuery ( getState ( ) . otp . config . api , getState ( ) . otp . currentQuery )
27
+ const queryBuilderFn = customOtpQueryBuilder || constructPlanQuery
28
+ const url = queryBuilderFn ( otpState . config . api , otpState . currentQuery )
23
29
// setURLSearch(url)
24
30
fetch ( url )
25
31
. then ( response => {
@@ -41,24 +47,13 @@ function constructPlanQuery (api, query) {
41
47
const planEndpoint = `${ api . host } :${ api . port } ${ api . path } /plan`
42
48
const { mode, time, date } = query
43
49
const params = {
50
+ arriveBy : query . departArrive === 'ARRIVE' ,
51
+ date,
44
52
fromPlace : `${ query . from . lat } ,${ query . from . lon } ` ,
45
53
showIntermediateStops : true ,
46
54
toPlace : `${ query . to . lat } ,${ query . to . lon } ` ,
47
- mode
48
- }
49
- switch ( query . departArrive ) {
50
- case 'ARRIVE' :
51
- params . arriveBy = true
52
- params . date = date
53
- params . time = time
54
- break
55
- case 'DEPART' :
56
- params . arriveBy = false
57
- params . date = date
58
- params . time = time
59
- break
60
- default :
61
- break
55
+ mode,
56
+ time
62
57
}
63
58
const stringParams = qs . stringify ( params )
64
59
// TODO: set url hash based on params
0 commit comments