Skip to content

Commit

Permalink
Merge pull request #3781 from anup2307/fix-browserhistory
Browse files Browse the repository at this point in the history
Fixed browser history issue from the historypush and related components
  • Loading branch information
DaleMcGrew authored Nov 27, 2023
2 parents 081e22a + 2fe804e commit aa425c6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
4 changes: 1 addition & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,7 @@ class App extends Component {
}

onVoterStoreChange () {
historyPush({
pathname: this.props.location.pathname,
});
historyPush(this.props.location.pathname);
}

setShowHeader (doShowHeader) {
Expand Down
11 changes: 9 additions & 2 deletions src/js/common/utils/historyPush.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ import webAppConfig from '../../config';
// If history retention is not working, see TabWithPushHistory.jsx for an example of how to do it.
// See v5: https://reactrouter.com/native/api/Hooks/usehistory
// IMPORTANT: The HTML5 window.history, is very different from the react-router V5 history, don't use window.history!
export default function historyPush (route) {
// removePriorPathname determines whether the route should be pushed or replace the previous entry in the history stack.
export default function historyPush (route, removePriorPathname = false) {
if (route !== global.weVoteGlobalHistory.location.pathname) {
if (removePriorPathname) {
global.weVoteGlobalHistory.replace(route);
} else {
global.weVoteGlobalHistory.push(route);
}
}
if (webAppConfig.LOG_ROUTING) {
console.log(`historyPush ******** ${route} *******`);
}
global.weVoteGlobalHistory.push(route);
}
14 changes: 7 additions & 7 deletions src/js/pages/Ballot/Ballot.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ class Ballot extends Component {
BallotActions.voterBallotItemsRetrieve(0, '', ballotLocationShortcutFromUrl);

// Change the URL to match
historyPush(`${ballotBaseUrl}/${ballotLocationShortcutFromUrl}`);
historyPush(`${ballotBaseUrl}/${ballotLocationShortcutFromUrl}`, true);
} else if (ballotReturnedWeVoteIdFromUrl !== '') {
// Change the ballot on load to make sure we are getting what we expect from the url
BallotActions.voterBallotItemsRetrieve(0, ballotReturnedWeVoteIdFromUrl, '');

// Change the URL to match
historyPush(`${ballotBaseUrl}/id/${ballotReturnedWeVoteIdFromUrl}`);
historyPush(`${ballotBaseUrl}/id/${ballotReturnedWeVoteIdFromUrl}`, true);
} else if (googleCivicElectionIdFromUrl !== 0) {
// Change the ballot on load to make sure we are getting what we expect from the url
if (googleCivicElectionId !== googleCivicElectionIdFromUrl) {
Expand All @@ -210,7 +210,7 @@ class Ballot extends Component {
if (!currentPathnameStartsWithNewUrl) {
// As long as the current pathname starts with the new URL, do NOT redirect
// console.log('REDIRECTING TO ballotElectionUrl');
historyPush(ballotElectionUrl);
historyPush(ballotElectionUrl, true);
}
}

Expand All @@ -224,12 +224,12 @@ class Ballot extends Component {
// console.log('ballotElectionUrl2: ', ballotElectionUrl2);
const currentPathnameStartsWithNewUrl2 = currentPathname && currentPathname.startsWith(ballotElectionUrl2);
if (!currentPathnameStartsWithNewUrl2) {
historyPush(ballotElectionUrl2);
historyPush(ballotElectionUrl2, true);
}
}
} else if (BallotStore.ballotProperties && BallotStore.ballotProperties.ballot_found === false) { // No ballot found
// console.log('if (BallotStore.ballotProperties && BallotStore.ballotProperties.ballot_found === false');
historyPush('/settings/location');
historyPush('/settings/location', true);
} else { // } else if (ballotWithItemsFromCompletionFilterType === undefined) {
// console.log('WebApp doesn\'t know the election or have ballot data, so ask the API server to return best guess');
BallotActions.voterBallotItemsRetrieve(0, '', '');
Expand Down Expand Up @@ -596,15 +596,15 @@ class Ballot extends Component {
if (voter && voter.is_signed_in) {
// console.log('onVoterStoreChange, about to historyPush(pathname):', pathname);
// Return to the same page without the 'voter_refresh_timer_on' variable
historyPush(pathname);
historyPush(pathname, true);
} else if (numberOfVoterRetrieveAttempts < 3) {
// console.log('About to startTimerToRetrieveVoter');
this.startTimerToRetrieveVoter();
} else {
// We have exceeded the number of allowed attempts and want to 'turn off' the request to refresh the voter object
// Return to the same page without the 'voter_refresh_timer_on' variable
// console.log('Exiting voterRefreshTimerOn');
historyPush(pathname);
historyPush(pathname, true);
}
} else {
// console.log('Ballot.jsx onVoterStoreChange VoterStore.getVoter: ', VoterStore.getVoter());
Expand Down
6 changes: 3 additions & 3 deletions src/js/pages/Campaigns/CampaignsHome.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class CampaignsHome extends Component {
const newPathname = this.getStateNamePathnameFromStateCode(voterStateCode);
const { location: { pathname } } = window;
if (pathname !== newPathname) {
historyPush(newPathname);
historyPush(newPathname, true);
} else {
this.setState({ stateCode: voterStateCode });
}
Expand Down Expand Up @@ -572,7 +572,7 @@ class CampaignsHome extends Component {
const newPathname = this.getStateNamePathnameFromStateCode(e.target.value);
const { location: { pathname } } = window;
if (pathname !== newPathname) {
historyPush(newPathname);
historyPush(newPathname);
} else {
this.setState({ stateCode: e.target.value });
}
Expand Down Expand Up @@ -624,7 +624,7 @@ class CampaignsHome extends Component {

render () {
renderLog('CampaignsHome'); // Set LOG_RENDER_EVENTS to log all renders
const {
const {
campaignList, campaignListTimeStampOfChange,
campaignsShowing,
candidateListOther, candidateListTimeStampOfChange,
Expand Down
2 changes: 1 addition & 1 deletion src/js/pages/Campaigns/CampaignsHomeLoader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class CampaignsHomeLoader extends Component {
const newPathname = this.getStateNamePathnameFromStateCode(voterStateCode);
const { location: { pathname } } = window;
if (pathname !== newPathname) {
historyPush(newPathname);
historyPush(newPathname, true);
} else {
this.setState({ stateCode: voterStateCode });
}
Expand Down

0 comments on commit aa425c6

Please sign in to comment.