Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clicking "Back" button on "Edit trip" page after logging in causes loop effect #376

Open
landonreed opened this issue May 27, 2021 · 1 comment
Labels

Comments

@landonreed
Copy link
Member

landonreed commented May 27, 2021

When clicking the "Back" button after having just logged in to my user account, it will send me back to the URL from which I was just redirected.

@miles-grant-ibigroup
Copy link
Collaborator

miles-grant-ibigroup commented Jul 7, 2021

Have run into this problem again when trying to prevent navigation away from a dirty trip edit page (#400). Using onbeforeunload is supposed to prevent navigation with the browser back and reload buttons. The reload button is blocked, but back is not intercepted. This to me suggests that react-router has already intercepted it and isn't working with it properly.

Resolving this issue will likely involve fixing behavior within the higher level component which is doing the routing

window.addEventListener('popstate', handleBackButtonPress)

and including a method to set blocking routing, or removing an item from history.

Would likely involve working on this function

export function handleBackButtonPress (e) {
return function (dispatch, getState) {
const state = getState()
const { activeSearchId } = state.otp
const uiUrlParams = getUiUrlParams(state)
// Get new search ID from URL after back button pressed.
// console.log('back button pressed', e)
const urlParams = coreUtils.query.getUrlParams()
const previousSearchId = urlParams.ui_activeSearch
const previousItinIndex = +urlParams.ui_activeItinerary || 0
const previousSearch = state.otp.searches[previousSearchId]
if (previousSearch) {
// If back button pressed and active search has changed, set search to
// previous search ID.
if (activeSearchId !== previousSearchId) {
dispatch(setActiveSearch(previousSearchId))
} else if (uiUrlParams.ui_activeItinerary !== previousItinIndex) {
// Active itinerary index has changed.
dispatch(setActiveItinerary({ index: previousItinIndex }))
}
} else {
// The back button was pressed, but there was no corresponding search
// found for the previous search ID. Derive search from URL params.
if (!previousSearchId && activeSearchId) {
// There is no search ID. Clear active search and from/to
dispatch(clearActiveSearch())
dispatch(clearLocation({ type: 'from' }))
dispatch(clearLocation({ type: 'to' }))
} else if (previousSearchId) {
console.warn(`No search found in state history for search ID: ${previousSearchId}. Replanning...`)
// Set query to the params found in the URL and perform routing query
// for search ID.
// Also, we don't want to update the URL here because that will funk with
// the browser history.
dispatch(parseUrlQueryString(urlParams))
}
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants