Skip to content

Commit 92eb991

Browse files
committed
fix: fix recent search onSelect
1 parent af29475 commit 92eb991

File tree

7 files changed

+50
-41
lines changed

7 files changed

+50
-41
lines changed

lib/actions/api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ export function routingQuery (searchId = null) {
123123
// performing a search based on query params taken from the URL after a back
124124
// button press, we don't need to update the OTP URL.
125125
// TODO: Should we be replacing the URL params here?
126-
if (isNewSearch) {
126+
const params = getUrlParams()
127+
if (isNewSearch || params.ui_activeSearch !== searchId) {
127128
dispatch(updateOtpUrlParams(otpState, searchId))
128129
}
129130
// also fetch a non-realtime route
@@ -746,7 +747,6 @@ export function setUrlSearch (params, replaceCurrent = false) {
746747
*/
747748
export function updateOtpUrlParams (otpState, searchId) {
748749
const otpParams = getRoutingParams(otpState, true)
749-
console.log('updating OTP URL params', otpState)
750750
return function (dispatch, getState) {
751751
const params = {}
752752
// Get all OTP-specific params, which will be retained unchanged in the URL

lib/actions/form.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function resetForm () {
3434
dispatch(settingQueryParam(otpState.user.defaults))
3535
} else {
3636
// Get user overrides and apply to default query
37-
const userOverrides = getItem('defaultQuery')
37+
const userOverrides = getItem('defaultQuery', {})
3838
const defaultQuery = Object.assign(getDefaultQuery(), userOverrides)
3939
// Filter out non-options (i.e., date, places).
4040
const options = getTripOptionsFromQuery(defaultQuery)
@@ -48,7 +48,8 @@ export function resetForm () {
4848

4949
/**
5050
* Action to update any specified query parameter. Replaces previous series of
51-
* parameter-specific actions.
51+
* parameter-specific actions. If a search ID is provided, a routing query (OTP
52+
* search) will be kicked off immediately.
5253
*/
5354
export function setQueryParam (payload, searchId) {
5455
return function (dispatch, getState) {

lib/components/form/user-settings.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,30 +78,27 @@ class UserSettings extends Component {
7878
: <small>No favorite stops </small>
7979
}
8080
</ul>
81-
{trackRecent &&
81+
{trackRecent && recentPlaces.length > 0 &&
8282
<div className='recent-places-container'>
8383
<hr />
8484
<div className='section-header'>Recent places</div>
8585
<ul style={{ padding: 0 }}>
86-
{recentPlaces.length > 0
87-
? recentPlaces.map(location => {
88-
return <Place key={location.id} location={location} {...this.props} />
89-
})
90-
: <small>No recent places </small>
91-
}
86+
{recentPlaces.map(location => {
87+
return <Place key={location.id} location={location} {...this.props} />
88+
})}
9289
</ul>
9390
</div>
9491
}
95-
{trackRecent &&
92+
{trackRecent && recentSearches.length > 0 &&
9693
<div className='recent-searches-container'>
9794
<hr />
9895
<div className='section-header'>Recent searches</div>
9996
<ul style={{ padding: 0 }}>
100-
{recentSearches.sort((a, b) => b.timestamp - a.timestamp).length > 0
101-
? recentSearches.map(search => {
97+
{recentSearches
98+
.sort((a, b) => b.timestamp - a.timestamp)
99+
.map(search => {
102100
return <RecentSearch key={search.id} search={search} {...this.props} />
103101
})
104-
: <small>No recent searches</small>
105102
}
106103
</ul>
107104
</div>
@@ -215,7 +212,8 @@ class Place extends Component {
215212
class RecentSearch extends Component {
216213
_onSelect = () => {
217214
const { search, setQueryParam } = this.props
218-
setQueryParam(search.query)
215+
// Update query params and initiate search.
216+
setQueryParam(search.query, search.id)
219217
}
220218

221219
_onForget = () => this.props.forgetSearch(this.props.search.id)

lib/components/viewers/stop-viewer.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class StopViewer extends Component {
5050
}
5151

5252
_onToggleAutoRefresh = () => {
53-
const { autoRefresh, toggleAutoRefresh } = this.props
54-
if (autoRefresh) {
53+
const { autoRefreshStopTimes, toggleAutoRefresh } = this.props
54+
if (autoRefreshStopTimes) {
5555
toggleAutoRefresh(false)
5656
} else {
5757
// Turn on auto-refresh and refresh immediately to give user feedback.
@@ -66,7 +66,7 @@ class StopViewer extends Component {
6666
// Load the viewed stop in the store when the Stop Viewer first mounts
6767
this.props.findStop({ stopId: this.props.viewedStop.stopId })
6868
// Turn on stop times refresh if enabled.
69-
if (this.props.autoRefresh) this._startAutoRefresh()
69+
if (this.props.autoRefreshStopTimes) this._startAutoRefresh()
7070
}
7171

7272
componentWillUnmount () {
@@ -102,8 +102,8 @@ class StopViewer extends Component {
102102
) {
103103
this.props.findStop({ stopId: nextProps.viewedStop.stopId })
104104
}
105-
if (this.props.autoRefresh && !nextProps.autoRefresh) this._stopAutoRefresh()
106-
else if (!this.props.autoRefresh && nextProps.autoRefresh) this._startAutoRefresh()
105+
if (this.props.autoRefreshStopTimes && !nextProps.autoRefreshStopTimes) this._stopAutoRefresh()
106+
else if (!this.props.autoRefreshStopTimes && nextProps.autoRefreshStopTimes) this._startAutoRefresh()
107107
}
108108

109109
render () {
@@ -227,7 +227,7 @@ class StopViewer extends Component {
227227
<input
228228
name='autoUpdate'
229229
type='checkbox'
230-
checked={this.props.autoRefresh}
230+
checked={this.props.autoRefreshStopTimes}
231231
onChange={this._onToggleAutoRefresh} />{' '}
232232
Auto-refresh arrivals?
233233
</label>
@@ -450,7 +450,7 @@ function getStatusLabel (delay) {
450450

451451
const mapStateToProps = (state, ownProps) => {
452452
return {
453-
autoRefresh: state.otp.user.autoRefresh,
453+
autoRefreshStopTimes: state.otp.user.autoRefreshStopTimes,
454454
favoriteStops: state.otp.user.favoriteStops,
455455
homeTimezone: state.otp.config.homeTimezone,
456456
viewedStop: state.otp.ui.viewedStop,

lib/reducers/create-otp-reducer.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,27 @@ const defaultConfig = {
2222
routingTypes: []
2323
}
2424

25-
// Load user override settings from local storage.
26-
// TODO: Make this work with settings fetched from user profile API service.
27-
const userOverrides = getItem('defaultQuery')
25+
// Load user settings from local storage.
26+
// TODO: Make this work with settings fetched from alternative storage system
27+
// (e.g., OTP backend middleware containing user profile system).
28+
// User overrides determine user's default mode/query parameters.
29+
const userOverrides = getItem('defaultQuery', {})
30+
// Combine user overrides with default query to get default search settings.
2831
const defaults = Object.assign(getDefaultQuery(), userOverrides)
29-
const autoRefresh = getItem('autoRefresh', true)
30-
const home = getItem('home', true)
31-
const work = getItem('work', true)
32-
const trackRecent = getItem('trackRecent', true) || false
33-
const recentPlaces = getItem('recent', true) || []
34-
const favoriteStops = getItem('favoriteStops', true) || []
35-
const recentSearches = getItem('recentSearches', true) || []
32+
// Whether to auto-refresh stop arrival times in the Stop Viewer.
33+
const autoRefreshStopTimes = getItem('autoRefreshStopTimes', true)
34+
// User's home and work locations
35+
const home = getItem('home')
36+
const work = getItem('work')
37+
// Whether recent searches and places should be tracked in local storage.
38+
const trackRecent = getItem('trackRecent', false)
39+
// Recent places used in trip plan searches.
40+
const recentPlaces = getItem('recent', [])
41+
// List of user's favorite stops.
42+
const favoriteStops = getItem('favoriteStops', [])
43+
// Recent trip plan searches (excluding time/date parameters to avoid complexity).
44+
const recentSearches = getItem('recentSearches', [])
45+
// Filter valid locations found into locations list.
3646
const locations = [home, work].filter(p => p)
3747
const MAX_RECENT_STORAGE = 5
3848
// TODO: parse and merge URL query params w/ default query
@@ -76,7 +86,7 @@ function createOtpReducer (config, initialQuery) {
7686
nearbyStops: []
7787
},
7888
user: {
79-
autoRefresh,
89+
autoRefreshStopTimes,
8090
// Do not store from/to or date/time in defaults
8191
defaults: getTripOptionsFromQuery(defaults),
8292
favoriteStops,
@@ -605,8 +615,8 @@ function createOtpReducer (config, initialQuery) {
605615
}
606616
})
607617
case 'TOGGLE_AUTO_REFRESH':
608-
storeItem('autoRefresh', action.payload)
609-
return update(state, { user: { autoRefresh: { $set: action.payload } } })
618+
storeItem('autoRefreshStopTimes', action.payload)
619+
return update(state, { user: { autoRefreshStopTimes: { $set: action.payload } } })
610620

611621
case 'FIND_ROUTES_RESPONSE':
612622
// If routes is undefined, initialize it w/ the full payload

lib/util/query-params.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const formatPlace = (location, alternateName) => {
4646
}
4747

4848
// Load stored default query settings from local storage
49-
let storedSettings = getItem('defaultQuery')
49+
let storedSettings = getItem('defaultQuery', {})
5050

5151
const queryParams = [
5252
{ /* from - the trip origin. stored internally as a location (lat/lon/name) object */

lib/util/storage.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ export function storeItem (key, object) {
1010

1111
/**
1212
* Retrieve a javascript object at the specified key. If not found, defaults to
13-
* empty object or, if the nullIfNotFound value is set to true, null.
13+
* null or, the optionally provided notFoundValue.
1414
*/
15-
export function getItem (key, nullIfNotFound = false) {
15+
export function getItem (key, notFoundValue = null) {
1616
let itemAsString
1717
try {
1818
itemAsString = window.localStorage.getItem(`${STORAGE_PREFIX}.${key}`)
1919
const json = JSON.parse(itemAsString)
2020
if (json) return json
21-
else return nullIfNotFound ? null : {}
21+
else return notFoundValue
2222
} catch (e) {
2323
// Catch any errors associated with parsing bad JSON.
2424
console.warn(e, itemAsString)
25-
return nullIfNotFound ? null : {}
25+
return notFoundValue
2626
}
2727
}
2828

0 commit comments

Comments
 (0)