Skip to content

Commit

Permalink
feat(router-store): Added action types (#47)
Browse files Browse the repository at this point in the history
Closes #44
  • Loading branch information
vsavkin authored and brandonroberts committed Jun 28, 2017
1 parent 931adb1 commit 1f67cb3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
12 changes: 6 additions & 6 deletions modules/router-store/spec/integration.spec.ts
Expand Up @@ -3,14 +3,14 @@ import {TestBed} from '@angular/core/testing';
import {NavigationEnd, Router} from '@angular/router';
import {RouterTestingModule} from '@angular/router/testing';
import {Store, StoreModule} from '@ngrx/store';
import {ROUTER_CANCEL, ROUTER_ERROR, ROUTER_NAVIGATION, routerReducer, StoreRouterConnectingModule} from '../src/index';
import { ROUTER_CANCEL, ROUTER_ERROR, ROUTER_NAVIGATION, routerReducer, StoreRouterConnectingModule, RouterNavigationAction, RouterCancelAction, RouterAction } from '../src/index';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/first';
import 'rxjs/add/operator/toPromise';

describe('integration spec', () => {
it('should work', (done) => {
const reducer = (state: string = '', action: any) => {
const reducer = (state: string = '', action: RouterAction<any>) => {
if (action.type === ROUTER_NAVIGATION) {
return action.payload.routerState.url.toString();
} else {
Expand Down Expand Up @@ -49,7 +49,7 @@ describe('integration spec', () => {
});

it('should support preventing navigation', (done) => {
const reducer = (state: string = '', action: any) => {
const reducer = (state: string = '', action: RouterAction<any>) => {
if (action.type === ROUTER_NAVIGATION && action.payload.routerState.url.toString() === '/next') {
throw new Error('You shall not pass!');
} else {
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('integration spec', () => {
});

it('should support rolling back if navigation gets canceled', (done) => {
const reducer = (state: string = '', action: any): any => {
const reducer = (state: string = '', action: RouterAction<any>): any => {
if (action.type === ROUTER_NAVIGATION) {
return { url: action.payload.routerState.url.toString(), lastAction: ROUTER_NAVIGATION };

Expand Down Expand Up @@ -117,7 +117,7 @@ describe('integration spec', () => {
});

it('should support rolling back if navigation errors', (done) => {
const reducer = (state: string = '', action: any): any => {
const reducer = (state: string = '', action: RouterAction<any>): any => {
if (action.type === ROUTER_NAVIGATION) {
return { url: action.payload.routerState.url.toString(), lastAction: ROUTER_NAVIGATION };

Expand Down Expand Up @@ -155,7 +155,7 @@ describe('integration spec', () => {
});

it('should call navigateByUrl when resetting state of the routerReducer', (done) => {
const reducer = (state: any, action: any) => {
const reducer = (state: any, action: RouterAction<any>) => {
const r = routerReducer(state, action);
return r && r.state ? ({ url: r.state.url, navigationId: r.navigationId }) : null;
};
Expand Down
4 changes: 4 additions & 0 deletions modules/router-store/src/index.ts
Expand Up @@ -2,6 +2,10 @@ export {
ROUTER_ERROR,
ROUTER_CANCEL,
ROUTER_NAVIGATION,
RouterNavigationAction,
RouterCancelAction,
RouterErrorAction,
RouterAction,
routerReducer,
RouterErrorPayload,
RouterReducerState,
Expand Down
42 changes: 37 additions & 5 deletions modules/router-store/src/router_store_module.ts
Expand Up @@ -16,6 +16,14 @@ export type RouterNavigationPayload = {
event: RoutesRecognized
};

/**
* An action dispatched when the router navigates.
*/
export type RouterNavigationAction = {
type: typeof ROUTER_NAVIGATION,
payload: RouterNavigationPayload
};

/**
* An action dispatched when the router cancels navigation.
*/
Expand All @@ -30,6 +38,14 @@ export type RouterCancelPayload<T> = {
event: NavigationCancel
};

/**
* An action dispatched when the router cancel navigation.
*/
export type RouterCancelAction<T> = {
type: typeof ROUTER_CANCEL,
payload: RouterCancelPayload<T>
};

/**
* An action dispatched when the router errors.
*/
Expand All @@ -44,13 +60,29 @@ export type RouterErrorPayload<T> = {
event: NavigationError
};

/**
* An action dispatched when the router errors.
*/
export type RouterErrorAction<T> = {
type: typeof ROUTER_ERROR,
payload: RouterErrorPayload<T>
};

/**
* An union type of router actions.
*/
export type RouterAction<T> = RouterNavigationAction | RouterCancelAction<T> | RouterErrorAction<T>;

export type RouterReducerState = { state: RouterStateSnapshot, navigationId: number };

export function routerReducer(state: RouterReducerState, action: any): RouterReducerState {
if (action.type === 'ROUTER_NAVIGATION' || action.type === 'ROUTER_ERROR' || action.type === 'ROUTER_CANCEL') {
return ({ state: action.payload.routerState, navigationId: action.payload.event.id });
} else {
return state;
export function routerReducer(state: RouterReducerState, action: RouterAction<any>): RouterReducerState {
switch (action.type) {
case ROUTER_NAVIGATION:
case ROUTER_ERROR:
case ROUTER_CANCEL:
return ({ state: action.payload.routerState, navigationId: action.payload.event.id });
default:
return state;
}
}

Expand Down

0 comments on commit 1f67cb3

Please sign in to comment.