From 448d177b4f9d3299fb586d125fc06a310a86a3ce Mon Sep 17 00:00:00 2001 From: Colin Megill Date: Tue, 10 Oct 2017 18:15:46 -0700 Subject: [PATCH] add redux middleware for paths --- src/middleware/updateURLMiddleware.js | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/middleware/updateURLMiddleware.js diff --git a/src/middleware/updateURLMiddleware.js b/src/middleware/updateURLMiddleware.js new file mode 100644 index 00000000..eefbe488 --- /dev/null +++ b/src/middleware/updateURLMiddleware.js @@ -0,0 +1,47 @@ +const updateURLMiddleware = store => next => action => { + const oldState = store.getState(); + const nextAction = next(action); + + console.log('middleware', store, next, action) + + if (action.type === 'url changed') { + return nextAction; + } + + const state = store.getState(); + + // Internal helper for working with URIs + const oldURI = new URI(window.location.href); + const newURI = new URI(oldURI).setQueryData({}); + + newURI.setPath('/foo/bar'); + + // Set the path based on state + if (!state.isOnLandingPage && state.project.id) { + newURI.setPath(newURI.getPath() + state.project.id + '/'); + newURI.addQueryData('baz', state.mode); + newURI.addQueryData('bat', state.selection.activePageID); + } else { + newURI.setPath(newURI.getPath() + state.landingSection + '/'); + } + + // Avoid URL thrashing by replacing state while loading instead of pushing + const newPath = newURI.toString(); + const oldPath = oldURI.toString(); + if (newPath !== oldPath) { + if ( + (oldState.mode === 'asdf' && + state.mode === 'asdf' && + !oldState.isOnLandingPage) || + oldState.isLoadingProject !== state.isLoadingProject + ) { + window.history.replaceState(null, null, newPath); + } else { + window.history.pushState(null, null, newPath); + } + } + + return nextAction; +}; + +export default updateURLMiddleware;