Skip to content

Commit

Permalink
fix(routerId): if routerId present, include in copied URL
Browse files Browse the repository at this point in the history
  • Loading branch information
landonreed committed Jun 22, 2020
1 parent e97b58f commit 3d1bbb5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
13 changes: 9 additions & 4 deletions lib/actions/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ export function matchContentToUrl (location) {
// Parse comma separated params (ensuring numbers are parsed correctly).
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.)
// Attempt to parse path if lat/lon not found. (Legacy UI otp.js used
// slashes in the pathname to specify lat, lon, etc.)
[,, lat, lon, zoom, routerId] = idToParams(location.pathname, '/')
}
console.log(lat, lon, zoom, routerId)
// Update map location/zoom and optionally override router ID.
dispatch(setMapCenter({ lat, lon }))
dispatch(setMapZoom({ zoom }))
if (+lat && +lon) dispatch(setMapCenter({ lat, lon }))
if (+zoom) dispatch(setMapZoom({ zoom }))
// If router ID is provided, override the default routerId.
if (routerId) dispatch(setRouterId(routerId))
dispatch(setMainPanelContent(null))
Expand All @@ -89,6 +90,10 @@ export function matchContentToUrl (location) {
}
}

/**
* Split the path id into its parts (according to specified delimiter). Parse
* numbers if detected.
*/
function idToParams (id, delimiter = ',') {
return id.split(delimiter).map(s => isNaN(s) ? s : +s)
}
Expand Down
10 changes: 5 additions & 5 deletions lib/components/app/responsive-webapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,20 @@ class ResponsiveWebapp extends Component {
{ enableHighAccuracy: true }
)
}

// Handle routing to a specific part of the app (e.g. stop viewer) on page
// load. (This happens prior to routing request in case special routerId is
// set from URL.)
this.props.matchContentToUrl(this.props.location)
if (location && location.search) {
// Set search params and plan trip if routing enabled and a query exists
// in the URL.
this.props.parseUrlQueryString()
}
// Handle routing to a specific part of the app (e.g. stop viewer) on page
// load.
this.props.matchContentToUrl(this.props.location)
}

componentWillUnmount () {
// Remove on back button press listener.
window.removeEventListener('popstate')
window.removeEventListener('popstate', this.props.handleBackButtonPress)
}

render () {
Expand Down
18 changes: 16 additions & 2 deletions lib/components/narrative/trip-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,24 @@ class CopyUrlButton extends Component {
this.state = { showCopied: false }
}

_resetState = () => this.setState({ showCopied: false })

_onClick = () => {
copyToClipboard(window.location.href)
// If special routerId has been set in session storage, construct copy URL
// for itinerary with #/start/ prefix to set routerId on page load.
const routerId = window.sessionStorage.getItem('routerId')
let url = window.location.href
if (routerId) {
const parts = url.split('#')
if (parts.length === 2) {
url = `${parts[0]}#/start/x/x/x/${routerId}${parts[1]}`
} else {
console.warn('URL not formatted as expected, copied URL will not contain session routerId.', routerId)
}
}
copyToClipboard(url)
this.setState({ showCopied: true })
setTimeout(() => { this.setState({ showCopied: false }) }, 2000)
window.setTimeout(this._resetState, 2000)
}

render () {
Expand Down

0 comments on commit 3d1bbb5

Please sign in to comment.