Skip to content

Commit

Permalink
fix(react-router): handle any route w webapp + support otp.js legacy …
Browse files Browse the repository at this point in the history
…route
  • Loading branch information
landonreed committed Jul 18, 2019
1 parent d886c1e commit 9085525
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/actions/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ export function matchContentToUrl (location) {
// This is a bit of a hack to make up for the fact that react-router does
// not always provide the match params as expected.
// https://github.com/ReactTraining/react-router/issues/5870#issuecomment-394194338
const root = location.pathname.split('/')[1]
let root = location.pathname.split('/')[1]
const match = matchPath(location.pathname, {
path: `/${root}/:id`,
exact: true,
strict: false
})
const id = match && match.params && match.params.id
console.log(location, id)
switch (root) {
case 'route':
if (id) {
Expand All @@ -65,9 +66,16 @@ export function matchContentToUrl (location) {
dispatch(setMainPanelContent(MainPanelContent.STOP_VIEWER))
}
break
case 'start':
case '@':
// Parse comma separated params (ensuring numbers are parsed correctly).
const [lat, lon, zoom, routerId] = id.split(',').map(s => isNaN(s) ? s : +s)
let [lat, lon, zoom, routerId] = id ? idToParams(id) : []
if (!lat || !lon) {
// Attempt to parse path. (Legacy UI otp.js used slashes in the
// pathname to specify lat, lon, etc.)
[,, lat, lon, zoom, routerId] = idToParams(location.pathname, '/')
}
console.log('Setting start position/zoom/router', lat, lon, zoom, routerId)
// Update map location/zoom and optionally override router ID.
dispatch(setMapCenter({ lat, lon }))
dispatch(setMapZoom({ zoom }))
Expand All @@ -82,6 +90,10 @@ export function matchContentToUrl (location) {
}
}

function idToParams (id, delimiter = ',') {
return id.split(delimiter).map(s => isNaN(s) ? s : +s)
}

/**
* Event listener for responsive webapp that handles a back button press and
* sets the active search and itinerary according to the URL query params.
Expand Down
5 changes: 5 additions & 0 deletions lib/components/app/responsive-webapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class RouterWrapper extends Component {
// to a quirk with react-router.
// https://github.com/ReactTraining/react-router/issues/5870#issuecomment-394194338
'/@/:latLonZoomRouter',
'/start/:latLonZoomRouter',
// Route viewer (and route ID).
'/route',
'/route/:id',
Expand All @@ -194,6 +195,10 @@ class RouterWrapper extends Component {
path='/print'
component={PrintLayout}
/>
{/* For any other route, simply return the web app. */}
<Route
render={() => <WebappWithRouter {...this.props} />}
/>
</Switch>
</div>
</ConnectedRouter>
Expand Down

0 comments on commit 9085525

Please sign in to comment.