Skip to content

Commit

Permalink
feat(router-store): add action types
Browse files Browse the repository at this point in the history
Closes #44
  • Loading branch information
vsavkin committed Jun 28, 2017
1 parent 931adb1 commit 140c8e4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
12 changes: 6 additions & 6 deletions modules/router-store/spec/integration.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export {
ROUTER_ERROR,
ROUTER_CANCEL,
ROUTER_NAVIGATION,
RouterNavigationAction,
RouterCancelAction,
RouterErrorAction,
RouterAction,
routerReducer,
RouterErrorPayload,
RouterReducerState,
Expand Down
44 changes: 38 additions & 6 deletions modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {NgModule} from '@angular/core';
import {NavigationCancel, NavigationError, Router, RouterStateSnapshot, RoutesRecognized} from '@angular/router';
import {Store} from '@ngrx/store';
import { Store, Action } from '@ngrx/store';
import {of} from 'rxjs/observable/of';

/**
Expand All @@ -16,6 +16,14 @@ export type RouterNavigationPayload = {
event: RoutesRecognized
};

/**
* An action dispatched when the router navigates.
*/
export interface RouterNavigationAction extends Action {
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 interface RouterCancelAction<T> extends Action {
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 interface RouterErrorAction<T> extends Action {
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 140c8e4

Please sign in to comment.