Skip to content

Commit f53201f

Browse files
committed
fix(time): make default query react properly to homeTimezone config
1 parent efddef4 commit f53201f

File tree

9 files changed

+56
-87
lines changed

9 files changed

+56
-87
lines changed

__tests__/reducers/__snapshots__/create-otp-reducer.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Object {
66
"config": Object {
77
"autoPlan": false,
88
"debouncePlanTimeMs": 0,
9+
"homeTimezone": "America/Los_Angeles",
910
"language": Object {},
1011
"operators": Array [],
1112
"realtimeEffectsDisplayThreshold": 120,

__tests__/util/__snapshots__/state.js.snap

Lines changed: 0 additions & 21 deletions
This file was deleted.

__tests__/util/state.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
/* globals describe, expect, it */
22

3-
import {getDefaultQuery, queryIsValid} from '../../lib/util/state'
3+
import {queryIsValid} from '../../lib/util/state'
44

55
describe('util > state', () => {
6-
it('getDefaultQuery should parse window hash if available', () => {
7-
window.location.hash = '#plan?arriveBy=false&date=2017-02-03&fromPlace=12,34&toPlace=34,12&time=12:34'
8-
expect(getDefaultQuery()).toMatchSnapshot()
9-
})
10-
116
describe('queryIsValid', () => {
127
const fakeFromLocation = {
138
lat: 12,

lib/actions/form.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export function resetForm () {
3535
} else {
3636
// Get user overrides and apply to default query
3737
const userOverrides = getItem('defaultQuery', {})
38-
const defaultQuery = Object.assign(getDefaultQuery(), userOverrides)
38+
const defaultQuery = Object.assign(
39+
getDefaultQuery(otpState.config),
40+
userOverrides
41+
)
3942
// Filter out non-options (i.e., date, places).
4043
const options = getTripOptionsFromQuery(defaultQuery)
4144
// Default mode is currently WALK,TRANSIT. We need to update this value
@@ -67,7 +70,15 @@ export function parseUrlQueryString (params = getUrlParams()) {
6770
})
6871
const searchId = params.ui_activeSearch || randId()
6972
// Convert strings to numbers/objects and dispatch
70-
dispatch(setQueryParam(planParamsToQuery(planParams), searchId))
73+
dispatch(
74+
setQueryParam(
75+
planParamsToQuery(
76+
planParams,
77+
getState().otp.config
78+
),
79+
searchId
80+
)
81+
)
7182
}
7283
}
7384

lib/reducers/create-otp-reducer.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import clone from 'clone'
22
import update from 'immutability-helper'
33
import isEqual from 'lodash.isequal'
4+
import moment from 'moment-timezone'
45
import objectPath from 'object-path'
56

67
import { matchLatLon } from '../util/map'
@@ -58,10 +59,11 @@ function validateInitalState (initialState) {
5859
* Create the initial state of otp-react-redux using user-provided config, any
5960
* items in localStorage and a few defaults.
6061
*/
61-
export function getInitialState (config, initialQuery) {
62+
export function getInitialState (userDefinedConfig, initialQuery) {
6263
const defaultConfig = {
6364
autoPlan: false,
6465
debouncePlanTimeMs: 0,
66+
homeTimezone: moment.tz.guess(),
6567
language: {},
6668
operators: [],
6769
realtimeEffectsDisplayThreshold: 120,
@@ -74,14 +76,15 @@ export function getInitialState (config, initialQuery) {
7476
timeRange: 345600 // four days in seconds
7577
}
7678
}
79+
const config = Object.assign(defaultConfig, userDefinedConfig)
7780

7881
// Load user settings from local storage.
7982
// TODO: Make this work with settings fetched from alternative storage system
8083
// (e.g., OTP backend middleware containing user profile system).
8184
// User overrides determine user's default mode/query parameters.
8285
const userOverrides = getItem('defaultQuery', {})
8386
// Combine user overrides with default query to get default search settings.
84-
const defaults = Object.assign(getDefaultQuery(), userOverrides)
87+
const defaults = Object.assign(getDefaultQuery(config), userOverrides)
8588
// Whether to auto-refresh stop arrival times in the Stop Viewer.
8689
const autoRefreshStopTimes = getItem('autoRefreshStopTimes', true)
8790
// User's home and work locations
@@ -123,7 +126,7 @@ export function getInitialState (config, initialQuery) {
123126
}
124127

125128
return {
126-
config: Object.assign(defaultConfig, config),
129+
config,
127130
currentQuery,
128131
location: {
129132
currentPosition: {

lib/util/query-params.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import { getCurrentDate, getCurrentTime } from './time'
1919
* (Applicability is assumed if this function is not provided.)
2020
*
2121
* default: the default value for this parameter. The default can be also be a
22-
* function that gets executed when accessing the default value. This can be
23-
* particluarly helpful if a brand new time-dependent value is desired to be
22+
* function that gets executed when accessing the default value. When the value
23+
* is a funciton, it will take an argument of the current config of the otp-rr
24+
* store. This is needed when a brand new time-dependent value is desired to be
2425
* calculated. It's also helpful for producing tests that have consistent data
2526
* output.
2627
*

lib/util/query.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ export function getTripOptionsFromQuery (query, keepPlace = false) {
9898
}
9999

100100
/**
101-
* Gets the default query param by executing the default value function if the
102-
* default is a function.
101+
* Gets the default query param by executing the default value function with the
102+
* provided otp config if the default value is a function.
103103
*/
104-
function getDefaultQueryParamValue (param) {
105-
return typeof param.default === 'function' ? param.default() : param.default
104+
function getDefaultQueryParamValue (param, config) {
105+
return typeof param.default === 'function' ? param.default(config) : param.default
106106
}
107107

108108
/**
@@ -127,23 +127,35 @@ export function isNotDefaultQuery (query, config) {
127127
// Check that the applicability test (if provided) is satisfied
128128
if (typeof paramInfo.applicable === 'function' &&
129129
!paramInfo.applicable(query, config)) return
130-
if (query[param] !== getDefaultQueryParamValue(paramInfo)) {
130+
if (query[param] !== getDefaultQueryParamValue(paramInfo, config)) {
131131
queryIsDifferent = true
132132
}
133133
})
134134
}
135135
return queryIsDifferent
136136
}
137137

138-
export function getDefaultQuery () {
138+
/**
139+
* Get the default query to OTP based on the given config.
140+
*
141+
* @param config the config in the otp-rr store.
142+
*/
143+
export function getDefaultQuery (config) {
139144
const defaultQuery = { routingType: 'ITINERARY' }
140145
queryParams.filter(qp => 'default' in qp).forEach(qp => {
141-
defaultQuery[qp.name] = getDefaultQueryParamValue(qp)
146+
defaultQuery[qp.name] = getDefaultQueryParamValue(qp, config)
142147
})
143148
return defaultQuery
144149
}
145150

146-
export function planParamsToQuery (params) {
151+
/**
152+
* Create a otp query based on a the url params.
153+
*
154+
* @param {Object} params An object representing the parsed querystring of url
155+
* params.
156+
* @param config the config in the otp-rr store.
157+
*/
158+
export function planParamsToQuery (params, config) {
147159
const query = {}
148160
for (var key in params) {
149161
switch (key) {
@@ -161,10 +173,10 @@ export function planParamsToQuery (params) {
161173
: 'NOW'
162174
break
163175
case 'date':
164-
query.date = params.date || getCurrentDate()
176+
query.date = params.date || getCurrentDate(config)
165177
break
166178
case 'time':
167-
query.time = params.time || getCurrentTime()
179+
query.time = params.time || getCurrentTime(config)
168180
break
169181
default:
170182
if (!isNaN(params[key])) query[key] = parseFloat(params[key])

lib/util/state.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -70,43 +70,6 @@ export function queryIsValid (otpState) {
7070
// TODO: add date/time validation
7171
}
7272

73-
/**
74-
* This parses the current URL params and returns a simple query object
75-
* containing only the primary query params.
76-
*
77-
* TODO: Determine if this needs to/can be replaced with
78-
* 'util/query.js#getDefaultQuery'
79-
*/
80-
export function getDefaultQuery () {
81-
let params = {}
82-
if (typeof (window) !== 'undefined') {
83-
params = getUrlParams()
84-
}
85-
const from = (params.fromPlace && params.fromPlace.split(',')) || []
86-
const to = (params.toPlace && params.toPlace.split(',')) || []
87-
return {
88-
routingType: 'ITINERARY',
89-
from: from.length ? {
90-
name: coordsToString(from) || null,
91-
lat: from[0] || null,
92-
lon: from[1] || null
93-
} : null,
94-
to: to.length ? {
95-
name: coordsToString(to) || null,
96-
lat: to[0] || null,
97-
lon: to[1] || null
98-
} : null,
99-
departArrive: params.arriveBy === 'true'
100-
? 'ARRIVE'
101-
: params.arriveBy === 'false'
102-
? 'DEPART'
103-
: 'NOW',
104-
date: params.date || getCurrentDate(),
105-
time: params.time || getCurrentTime(),
106-
mode: params.mode
107-
}
108-
}
109-
11073
export function getRealtimeEffects (otpState) {
11174
const search = getActiveSearch(otpState)
11275

lib/util/time.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,21 @@ export function formatStopTime (seconds, timezone = null, timeFormat = 'h:mm a')
6464
}
6565

6666
/**
67-
* Formats current time for use in OTP query
67+
* Formats current time for use in OTP query based on the given config.
68+
*
69+
* @param config the config in the otp-rr store.
6870
* @returns {string} formatted text representation
6971
*/
70-
export function getCurrentTime () {
71-
return moment().format(TIME_FORMAT_24_HR)
72+
export function getCurrentTime (config) {
73+
return moment().tz(config.homeTimezone).format(getTimeFormat(config))
7274
}
7375

7476
/**
75-
* Formats current date for use in OTP query
77+
* Formats current date for use in OTP query based on the given config.
78+
*
79+
* @param config the config in the otp-rr store.
7680
* @returns {string} formatted text representation
7781
*/
78-
export function getCurrentDate () {
79-
return moment().format('YYYY-MM-DD')
82+
export function getCurrentDate (config) {
83+
return moment().tz(config.homeTimezone).format(getDateFormat(config))
8084
}

0 commit comments

Comments
 (0)