Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/__tests__/state-change-success.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import stateChangeSuccess from '../state-change-success';

describe('stateChangeSuccess', () => {
it('should create an action with the provided params', () => {
let action = stateChangeSuccess();
let action = stateChangeSuccess('toState', 'toParams', 'fromState', 'fromParams');
expect(action.payload.toState).to.equal('toState');
expect(action.payload.toParams).to.equal('toParams');
expect(action.payload.fromState).to.equal('fromState');
expect(action.payload.fromParams).to.equal('fromParams');
expect(action.type).to.equal('@@reduxUiRouter/onSuccess');
});
});
25 changes: 21 additions & 4 deletions src/router-listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,28 @@
* @return {undefined} undefined
*/
export default function RouterListener($transitions, ngUiStateChangeActions) {
const prevNext = (t) => [t.to(), t.params('to'), t.from(), t.params('from')];
const prevNext = t => [t.to(), t.params('to'), t.from(), t.params('from')];

$transitions.onStart({}, ($transition$) => ngUiStateChangeActions.onStateChangeStart(...prevNext($transition$)));
$transitions.onError({}, ($transition$) => ngUiStateChangeActions.onStateChangeError(...prevNext($transition$), $transition$.error()));
$transitions.onSuccess({}, () => ngUiStateChangeActions.onStateChangeSuccess());
const prevNextReduxState = t => ([getStateObject(t.to()),
t.params('to'),
getStateObject(t.from()),
t.params('from')]);

$transitions.onStart({}, $transition$ => ngUiStateChangeActions.onStateChangeStart(...prevNext($transition$)));
$transitions.onError({}, $transition$ => ngUiStateChangeActions.onStateChangeError(...prevNext($transition$), $transition$.error()));
$transitions.onSuccess({}, $transition$ => ngUiStateChangeActions.onStateChangeSuccess(...prevNextReduxState($transition$)));
}

function getStateObject(state) {
if (!state) {
return {};
}
const { name, params, url } = state;
return {
name,
params,
url,
};
}

RouterListener.$inject = [
Expand Down
10 changes: 5 additions & 5 deletions src/router-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from './action-types';

export default function routerMiddleware($state) {
return ({ getState }) => next => action => {
return () => next => action => {
const { payload } = action;

switch (action.type) {
Expand All @@ -26,10 +26,10 @@ export default function routerMiddleware($state) {
return next({
type: STATE_CHANGE_SUCCESS,
payload: {
currentState: $state.$current.name,
currentParams: $state.params,
prevState: getState().router.currentState,
prevParams: getState().router.currentParams,
currentState: action.payload.toState,
currentParams: action.payload.toParams,
prevState: action.payload.fromState,
prevParams: action.payload.fromParams,
},
});

Expand Down
12 changes: 11 additions & 1 deletion src/state-change-success.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import { STATE_CHANGE_SUCCESS } from './action-types';
*
* http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state
*
* @param {Object} toState To state definition
* @param {Object} toParams To params object
* @param {Object} fromState From state definition
* @param {Object} fromParams From params object
* @return {Object} Action object
*/
export default function onStateChangeSuccess() {
export default function onStateChangeSuccess(toState, toParams, fromState, fromParams) {
return {
type: STATE_CHANGE_SUCCESS,
payload: {
toState,
toParams,
fromState,
fromParams,
},
};
}