Skip to content

Commit 9bd4b43

Browse files
authored
Fixed the prev state and params in the redux router state getting set… (#68)
* Fixed the prev state and params in the redux router state getting set to the current state and params by changing the way that the STATE_CHANGE_SUCCESS handler in the middleware accesses the previous state and params.
1 parent 4b5d09a commit 9bd4b43

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

src/__tests__/state-change-success.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import stateChangeSuccess from '../state-change-success';
33

44
describe('stateChangeSuccess', () => {
55
it('should create an action with the provided params', () => {
6-
let action = stateChangeSuccess();
6+
let action = stateChangeSuccess('toState', 'toParams', 'fromState', 'fromParams');
7+
expect(action.payload.toState).to.equal('toState');
8+
expect(action.payload.toParams).to.equal('toParams');
9+
expect(action.payload.fromState).to.equal('fromState');
10+
expect(action.payload.fromParams).to.equal('fromParams');
711
expect(action.type).to.equal('@@reduxUiRouter/onSuccess');
812
});
913
});

src/router-listener.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,28 @@
66
* @return {undefined} undefined
77
*/
88
export default function RouterListener($transitions, ngUiStateChangeActions) {
9-
const prevNext = (t) => [t.to(), t.params('to'), t.from(), t.params('from')];
9+
const prevNext = t => [t.to(), t.params('to'), t.from(), t.params('from')];
1010

11-
$transitions.onStart({}, ($transition$) => ngUiStateChangeActions.onStateChangeStart(...prevNext($transition$)));
12-
$transitions.onError({}, ($transition$) => ngUiStateChangeActions.onStateChangeError(...prevNext($transition$), $transition$.error()));
13-
$transitions.onSuccess({}, () => ngUiStateChangeActions.onStateChangeSuccess());
11+
const prevNextReduxState = t => ([getStateObject(t.to()),
12+
t.params('to'),
13+
getStateObject(t.from()),
14+
t.params('from')]);
15+
16+
$transitions.onStart({}, $transition$ => ngUiStateChangeActions.onStateChangeStart(...prevNext($transition$)));
17+
$transitions.onError({}, $transition$ => ngUiStateChangeActions.onStateChangeError(...prevNext($transition$), $transition$.error()));
18+
$transitions.onSuccess({}, $transition$ => ngUiStateChangeActions.onStateChangeSuccess(...prevNextReduxState($transition$)));
19+
}
20+
21+
function getStateObject(state) {
22+
if (!state) {
23+
return {};
24+
}
25+
const { name, params, url } = state;
26+
return {
27+
name,
28+
params,
29+
url,
30+
};
1431
}
1532

1633
RouterListener.$inject = [

src/router-middleware.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from './action-types';
77

88
export default function routerMiddleware($state) {
9-
return ({ getState }) => next => action => {
9+
return () => next => action => {
1010
const { payload } = action;
1111

1212
switch (action.type) {
@@ -26,10 +26,10 @@ export default function routerMiddleware($state) {
2626
return next({
2727
type: STATE_CHANGE_SUCCESS,
2828
payload: {
29-
currentState: $state.$current.name,
30-
currentParams: $state.params,
31-
prevState: getState().router.currentState,
32-
prevParams: getState().router.currentParams,
29+
currentState: action.payload.toState,
30+
currentParams: action.payload.toParams,
31+
prevState: action.payload.fromState,
32+
prevParams: action.payload.fromParams,
3333
},
3434
});
3535

src/state-change-success.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ import { STATE_CHANGE_SUCCESS } from './action-types';
66
*
77
* http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state
88
*
9+
* @param {Object} toState To state definition
10+
* @param {Object} toParams To params object
11+
* @param {Object} fromState From state definition
12+
* @param {Object} fromParams From params object
913
* @return {Object} Action object
1014
*/
11-
export default function onStateChangeSuccess() {
15+
export default function onStateChangeSuccess(toState, toParams, fromState, fromParams) {
1216
return {
1317
type: STATE_CHANGE_SUCCESS,
18+
payload: {
19+
toState,
20+
toParams,
21+
fromState,
22+
fromParams,
23+
},
1424
};
1525
}

0 commit comments

Comments
 (0)