Skip to content

Commit

Permalink
keep redux page state in sync when going back via browser. Closes #485
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshadfield committed Jan 18, 2018
1 parent e32a1dd commit 8334e41
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/actions/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ export const browserBackForward = () => (dispatch, getState) => {
} else {
dispatch(changePageQuery({query: queryString.parse(window.location.search)}));
}
}
};
9 changes: 7 additions & 2 deletions src/middleware/changeURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import * as types from "../actions/types";
/* What is this middleware?
This middleware acts to keep the app state and the URL query state in sync by intercepting actions
and updating the URL accordingly. Thus, in theory, this middleware can be disabled and the app will still work
as expected
as expected.
The only modification of redux state by this app is (potentially) an action of type types.URL
which is used to "save" the current page so we can diff against a new one!
*/

// eslint-disable-next-line
Expand Down Expand Up @@ -102,7 +105,7 @@ export const changeURLMiddleware = (store) => (next) => (action) => {
if (search) {search = "?" + search;}
if (!pathname.startsWith("/")) {pathname = "/" + pathname;}

if (pathname !== window.location.pathname || window.location.search !== search) {
if (pathname !== window.location.pathname || search !== window.location.search) {
let newURLString = pathname;
if (search) {newURLString += search;}
// if (pathname !== window.location.pathname) {console.log(pathname, window.location.pathname)}
Expand All @@ -114,6 +117,8 @@ export const changeURLMiddleware = (store) => (next) => (action) => {
window.history.replaceState({}, "", newURLString);
}
next({type: types.URL, path: pathname, query: search});
} else if (pathname !== state.datasets.urlPath && action.type === types.PAGE_CHANGE) {
next({type: types.URL, path: pathname, query: search});
}

return result;
Expand Down

0 comments on commit 8334e41

Please sign in to comment.