Skip to content

Commit 9085525

Browse files
committed
fix(react-router): handle any route w webapp + support otp.js legacy route
1 parent d886c1e commit 9085525

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/actions/ui.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ export function matchContentToUrl (location) {
4040
// This is a bit of a hack to make up for the fact that react-router does
4141
// not always provide the match params as expected.
4242
// https://github.com/ReactTraining/react-router/issues/5870#issuecomment-394194338
43-
const root = location.pathname.split('/')[1]
43+
let root = location.pathname.split('/')[1]
4444
const match = matchPath(location.pathname, {
4545
path: `/${root}/:id`,
4646
exact: true,
4747
strict: false
4848
})
4949
const id = match && match.params && match.params.id
50+
console.log(location, id)
5051
switch (root) {
5152
case 'route':
5253
if (id) {
@@ -65,9 +66,16 @@ export function matchContentToUrl (location) {
6566
dispatch(setMainPanelContent(MainPanelContent.STOP_VIEWER))
6667
}
6768
break
69+
case 'start':
6870
case '@':
6971
// Parse comma separated params (ensuring numbers are parsed correctly).
70-
const [lat, lon, zoom, routerId] = id.split(',').map(s => isNaN(s) ? s : +s)
72+
let [lat, lon, zoom, routerId] = id ? idToParams(id) : []
73+
if (!lat || !lon) {
74+
// Attempt to parse path. (Legacy UI otp.js used slashes in the
75+
// pathname to specify lat, lon, etc.)
76+
[,, lat, lon, zoom, routerId] = idToParams(location.pathname, '/')
77+
}
78+
console.log('Setting start position/zoom/router', lat, lon, zoom, routerId)
7179
// Update map location/zoom and optionally override router ID.
7280
dispatch(setMapCenter({ lat, lon }))
7381
dispatch(setMapZoom({ zoom }))
@@ -82,6 +90,10 @@ export function matchContentToUrl (location) {
8290
}
8391
}
8492

93+
function idToParams (id, delimiter = ',') {
94+
return id.split(delimiter).map(s => isNaN(s) ? s : +s)
95+
}
96+
8597
/**
8698
* Event listener for responsive webapp that handles a back button press and
8799
* sets the active search and itinerary according to the URL query params.

lib/components/app/responsive-webapp.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class RouterWrapper extends Component {
181181
// to a quirk with react-router.
182182
// https://github.com/ReactTraining/react-router/issues/5870#issuecomment-394194338
183183
'/@/:latLonZoomRouter',
184+
'/start/:latLonZoomRouter',
184185
// Route viewer (and route ID).
185186
'/route',
186187
'/route/:id',
@@ -194,6 +195,10 @@ class RouterWrapper extends Component {
194195
path='/print'
195196
component={PrintLayout}
196197
/>
198+
{/* For any other route, simply return the web app. */}
199+
<Route
200+
render={() => <WebappWithRouter {...this.props} />}
201+
/>
197202
</Switch>
198203
</div>
199204
</ConnectedRouter>

0 commit comments

Comments
 (0)