Skip to content

Commit b27e9a6

Browse files
authored
Merge pull request #162 from opentripplanner/store-router-id
Persist routerId across page reloads
2 parents 0457a25 + ea7e634 commit b27e9a6

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

Diff for: lib/actions/ui.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ export function matchContentToUrl (location) {
7171
// Parse comma separated params (ensuring numbers are parsed correctly).
7272
let [lat, lon, zoom, routerId] = id ? idToParams(id) : []
7373
if (!lat || !lon) {
74-
// Attempt to parse path. (Legacy UI otp.js used slashes in the
75-
// pathname to specify lat, lon, etc.)
74+
// Attempt to parse path if lat/lon not found. (Legacy UI otp.js used
75+
// slashes in the pathname to specify lat, lon, etc.)
7676
[,, lat, lon, zoom, routerId] = idToParams(location.pathname, '/')
7777
}
78+
console.log(lat, lon, zoom, routerId)
7879
// Update map location/zoom and optionally override router ID.
79-
dispatch(setMapCenter({ lat, lon }))
80-
dispatch(setMapZoom({ zoom }))
80+
if (+lat && +lon) dispatch(setMapCenter({ lat, lon }))
81+
if (+zoom) dispatch(setMapZoom({ zoom }))
82+
// If router ID is provided, override the default routerId.
8183
if (routerId) dispatch(setRouterId(routerId))
8284
dispatch(setMainPanelContent(null))
8385
break
@@ -89,6 +91,10 @@ export function matchContentToUrl (location) {
8991
}
9092
}
9193

94+
/**
95+
* Split the path id into its parts (according to specified delimiter). Parse
96+
* numbers if detected.
97+
*/
9298
function idToParams (id, delimiter = ',') {
9399
return id.split(delimiter).map(s => isNaN(s) ? s : +s)
94100
}

Diff for: lib/components/app/responsive-webapp.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ class ResponsiveWebapp extends Component {
109109
{ enableHighAccuracy: true }
110110
)
111111
}
112-
112+
// Handle routing to a specific part of the app (e.g. stop viewer) on page
113+
// load. (This happens prior to routing request in case special routerId is
114+
// set from URL.)
115+
this.props.matchContentToUrl(this.props.location)
113116
if (location && location.search) {
114117
// Set search params and plan trip if routing enabled and a query exists
115118
// in the URL.
116119
this.props.parseUrlQueryString()
117120
}
118-
// Handle routing to a specific part of the app (e.g. stop viewer) on page
119-
// load.
120-
this.props.matchContentToUrl(this.props.location)
121121
}
122122

123123
componentWillUnmount () {

Diff for: lib/components/narrative/trip-tools.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,24 @@ class CopyUrlButton extends Component {
7777
this.state = { showCopied: false }
7878
}
7979

80+
_resetState = () => this.setState({ showCopied: false })
81+
8082
_onClick = () => {
81-
copyToClipboard(window.location.href)
83+
// If special routerId has been set in session storage, construct copy URL
84+
// for itinerary with #/start/ prefix to set routerId on page load.
85+
const routerId = window.sessionStorage.getItem('routerId')
86+
let url = window.location.href
87+
if (routerId) {
88+
const parts = url.split('#')
89+
if (parts.length === 2) {
90+
url = `${parts[0]}#/start/x/x/x/${routerId}${parts[1]}`
91+
} else {
92+
console.warn('URL not formatted as expected, copied URL will not contain session routerId.', routerId)
93+
}
94+
}
95+
copyToClipboard(url)
8296
this.setState({ showCopied: true })
83-
setTimeout(() => { this.setState({ showCopied: false }) }, 2000)
97+
window.setTimeout(this._resetState, 2000)
8498
}
8599

86100
render () {

Diff for: lib/reducers/create-otp-reducer.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ export function getInitialState (userDefinedConfig, initialQuery) {
121121
if (config.locations) {
122122
locations.push(...config.locations.map(l => ({ ...l, type: 'suggested' })))
123123
}
124+
// Check for alternative routerId in session storage. This is generally used
125+
// for testing single GTFS feed OTP graphs that are deployed to feed-specific
126+
// routers (e.g., https://otp.server.com/otp/routers/non_default_router).
127+
// This routerId session value is initially set by visiting otp-rr
128+
// with the path /start/:latLonZoomRouter, which dispatches the SET_ROUTER_ID
129+
// action and stores the value in sessionStorage.
130+
// Note: this mechanism assumes that the OTP API path is otp/routers/default.
131+
const routerId = window.sessionStorage.getItem('routerId')
132+
// If routerId is found, update the config.api.path (keep the original config
133+
// value at originalPath in case it needs to be reverted.)
134+
if (routerId) {
135+
config.api.originalPath = userDefinedConfig.api.path
136+
config.api.path = `/otp/routers/${routerId}`
137+
}
124138
let queryModes = currentQuery.mode.split(',')
125139

126140
// If 'TRANSIT' is included in the mode list, replace it with individual modes
@@ -542,11 +556,23 @@ function createOtpReducer (config, initialQuery) {
542556
}
543557
})
544558
case 'SET_ROUTER_ID':
545-
const routerId = action.payload || 'default'
559+
const routerId = action.payload
560+
// Store original path value in originalPath variable.
561+
const originalPath = config.api.originalPath || config.api.path || '/otp/routers/default'
562+
const path = routerId
563+
? `/otp/routers/${routerId}`
564+
// If routerId is null, revert to the original config's API path (or
565+
// the standard path if that is not found).
566+
: originalPath
567+
// Store routerId in session storage (persists through page reloads but
568+
// not when a new tab/window is opened).
569+
if (routerId) window.sessionStorage.setItem('routerId', routerId)
570+
else window.sessionStorage.removeItem('routerId')
546571
return update(state, {
547572
config: {
548573
api: {
549-
path: { $set: `/otp/routers/${routerId}` }
574+
path: { $set: path },
575+
originalPath: { $set: originalPath }
550576
}
551577
}
552578
})

0 commit comments

Comments
 (0)