From 4bc6485c7c79e570416deddd35e0bdaf544e90ff Mon Sep 17 00:00:00 2001 From: Connor Smallman Date: Thu, 13 Sep 2018 08:06:57 +0100 Subject: [PATCH 1/4] fix(location reducer): Call thunk when action kind is push to allow refreshes on same route fix #276 --- src/reducer/createLocationReducer.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/reducer/createLocationReducer.js b/src/reducer/createLocationReducer.js index cb88fee1..b5b53055 100644 --- a/src/reducer/createLocationReducer.js +++ b/src/reducer/createLocationReducer.js @@ -24,7 +24,9 @@ export default (initialState: LocationState, routesMap: RoutesMap) => ( (typeof route === 'string' || route.path) && (action.meta.location.current.pathname !== state.pathname || action.meta.location.current.search !== state.search || - action.meta.location.kind === 'load')) + action.meta.location.kind === 'load' || + action.meta.location.kind === 'push' + )) ) { const query = action.meta.location.current.query const search = action.meta.location.current.search From e8cd3deb6bdac39b036c1839ce6bd3f2049ccb7f Mon Sep 17 00:00:00 2001 From: Sergio Toro Date: Thu, 13 Sep 2018 16:11:00 +0200 Subject: [PATCH 2/4] update kind if same route is requested --- .../createLocationReducer.js.snap | 41 +++++++++++++++++++ __tests__/createLocationReducer.js | 24 +++++++++++ src/reducer/createLocationReducer.js | 17 +++++++- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/__tests__/__snapshots__/createLocationReducer.js.snap b/__tests__/__snapshots__/createLocationReducer.js.snap index e628f1b3..453735e9 100644 --- a/__tests__/__snapshots__/createLocationReducer.js.snap +++ b/__tests__/__snapshots__/createLocationReducer.js.snap @@ -41,6 +41,47 @@ Object { } `; +exports[`createLocationReducer() - reduces action.meta.location.kind being updated 1`] = ` +Object { + "hasSSR": undefined, + "history": Object { + "entries": Array [ + Object { + "hash": "", + "key": "345678", + "pathname": "/first", + "search": "", + "state": undefined, + }, + Object { + "hash": "", + "key": "345678", + "pathname": "/first", + "search": "", + "state": undefined, + }, + ], + "index": 1, + "length": 2, + }, + "kind": "load", + "pathname": "/first", + "payload": Object { + "param": "bar", + }, + "prev": Object { + "pathname": "/first", + "payload": Object {}, + "type": "FIRST", + }, + "routesMap": Object { + "FIRST": "/first", + "SECOND": "/second/:param", + }, + "type": "FIRST", +} +`; + exports[`getInitialState() returns state.history === undefined when using createBrowserHistory 1`] = ` Object { "hasSSR": undefined, diff --git a/__tests__/createLocationReducer.js b/__tests__/createLocationReducer.js index 0d31b41c..cbbe9479 100644 --- a/__tests__/createLocationReducer.js +++ b/__tests__/createLocationReducer.js @@ -17,6 +17,30 @@ it('createLocationReducer() - maintains address bar pathname state and current + expectState(state) }) +it('createLocationReducer() - reduces action.meta.location.kind being updated', () => { + const { initialState, action, routesMap, expectState } = reducerParameters( + 'FIRST', + '/first' + ) + + const reducer = createLocationReducer(initialState, routesMap) + const state = reducer(undefined, action) /*? */ + + const nextAction = { + ...action, + meta: { + location: { + ...action.meta.location, + kind: 'push' + } + } + } + const nextState = reducer(state, nextAction) /*? */ + + expectState(state) + expect(nextState.kind).toEqual('push') +}) + it('locationReducer() reduces action.type === NOT_FOUND', () => { const { initialState, routesMap, action } = reducerParameters( NOT_FOUND, diff --git a/src/reducer/createLocationReducer.js b/src/reducer/createLocationReducer.js index b5b53055..5af6f904 100644 --- a/src/reducer/createLocationReducer.js +++ b/src/reducer/createLocationReducer.js @@ -24,8 +24,7 @@ export default (initialState: LocationState, routesMap: RoutesMap) => ( (typeof route === 'string' || route.path) && (action.meta.location.current.pathname !== state.pathname || action.meta.location.current.search !== state.search || - action.meta.location.kind === 'load' || - action.meta.location.kind === 'push' + action.meta.location.kind === 'load' )) ) { const query = action.meta.location.current.query @@ -43,6 +42,20 @@ export default (initialState: LocationState, routesMap: RoutesMap) => ( routesMap } } + else if ( + route && + !action.error && + (typeof route === 'string' || route.path) && + (action.meta.location.current.pathname === state.pathname || + action.meta.location.current.search === state.search || + action.meta.location.kind !== state.kind + ) + ) { + return { + ...state, + kind: action.meta.location.kind + } + } else if (action.type === ADD_ROUTES) { return { ...state, From 00066ca61d3e5485226c685d26ce149a75fe7fd7 Mon Sep 17 00:00:00 2001 From: Sergio Toro Date: Thu, 13 Sep 2018 16:23:46 +0200 Subject: [PATCH 3/4] revert modifying if closing --- src/reducer/createLocationReducer.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/reducer/createLocationReducer.js b/src/reducer/createLocationReducer.js index 5af6f904..de0a7c8b 100644 --- a/src/reducer/createLocationReducer.js +++ b/src/reducer/createLocationReducer.js @@ -24,8 +24,7 @@ export default (initialState: LocationState, routesMap: RoutesMap) => ( (typeof route === 'string' || route.path) && (action.meta.location.current.pathname !== state.pathname || action.meta.location.current.search !== state.search || - action.meta.location.kind === 'load' - )) + action.meta.location.kind === 'load')) ) { const query = action.meta.location.current.query const search = action.meta.location.current.search From e975180c3ed397d73c28266049662804eb8c44b6 Mon Sep 17 00:00:00 2001 From: Sergio Toro Date: Fri, 14 Sep 2018 10:41:43 +0200 Subject: [PATCH 4/4] condition should be kind being updated --- src/reducer/createLocationReducer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/reducer/createLocationReducer.js b/src/reducer/createLocationReducer.js index de0a7c8b..4c5b1c8d 100644 --- a/src/reducer/createLocationReducer.js +++ b/src/reducer/createLocationReducer.js @@ -45,8 +45,8 @@ export default (initialState: LocationState, routesMap: RoutesMap) => ( route && !action.error && (typeof route === 'string' || route.path) && - (action.meta.location.current.pathname === state.pathname || - action.meta.location.current.search === state.search || + (action.meta.location.current.pathname === state.pathname && + action.meta.location.current.search === state.search && action.meta.location.kind !== state.kind ) ) {