Skip to content

Commit 0dc25d4

Browse files
committed
feat(actions): allow use of custom query builder function
also, don’t require fetch unless we need to in order to make project react-native friendly
1 parent f5eb582 commit 0dc25d4

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

lib/actions/api.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
/* globals fetch */
2+
13
import deepEqual from 'deep-equal'
24
import { createAction } from 'redux-actions'
3-
import fetch from 'isomorphic-fetch'
45
import qs from 'qs'
56

7+
if (typeof (fetch) === 'undefined') {
8+
require('isomorphic-fetch')
9+
}
10+
611
import { hasValidLocation } from '../util/state'
712

813
export const receivedPlanResponse = createAction('PLAN_RESPONSE')
914
export const requestPlanResponse = createAction('PLAN_REQUEST')
1015

11-
export function planTrip () {
16+
export function planTrip (customOtpQueryBuilder) {
1217
return function (dispatch, getState) {
1318
const otpState = getState().otp
1419
const latest = otpState.searches.length && otpState.searches[otpState.searches.length - 1]
@@ -19,7 +24,8 @@ export function planTrip () {
1924
}
2025
if (!hasValidLocation(otpState, 'from') || !hasValidLocation(otpState, 'to')) return // TODO: replace with isQueryValid?
2126
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)
2329
// setURLSearch(url)
2430
fetch(url)
2531
.then(response => {
@@ -41,24 +47,13 @@ function constructPlanQuery (api, query) {
4147
const planEndpoint = `${api.host}:${api.port}${api.path}/plan`
4248
const { mode, time, date } = query
4349
const params = {
50+
arriveBy: query.departArrive === 'ARRIVE',
51+
date,
4452
fromPlace: `${query.from.lat},${query.from.lon}`,
4553
showIntermediateStops: true,
4654
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
6257
}
6358
const stringParams = qs.stringify(params)
6459
// TODO: set url hash based on params

0 commit comments

Comments
 (0)