Skip to content

Commit 283424f

Browse files
timdeschryverbrandonroberts
authored andcommitted
feat(router-store): add routerState to action payload (#1511)
1 parent c8bc008 commit 283424f

File tree

3 files changed

+97
-16
lines changed

3 files changed

+97
-16
lines changed

modules/router-store/spec/integration.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
RouterStateSnapshot,
77
NavigationCancel,
88
NavigationError,
9+
ActivatedRouteSnapshot,
910
} from '@angular/router';
1011
import { Store, ScannedActionsSubject } from '@ngrx/store';
1112
import { filter, first, mapTo, take } from 'rxjs/operators';
@@ -88,6 +89,51 @@ describe('integration spec', () => {
8889
});
8990
});
9091

92+
it('should have the routerState in the payload', (done: any) => {
93+
const actionLog: RouterAction<any>[] = [];
94+
const reducer = (state: string = '', action: RouterAction<any>) => {
95+
switch (action.type) {
96+
case ROUTER_CANCEL:
97+
case ROUTER_ERROR:
98+
case ROUTER_NAVIGATED:
99+
case ROUTER_NAVIGATION:
100+
case ROUTER_REQUEST:
101+
actionLog.push(action);
102+
return state;
103+
default:
104+
return state;
105+
}
106+
};
107+
108+
createTestModule({
109+
reducers: { reducer },
110+
canActivate: (
111+
route: ActivatedRouteSnapshot,
112+
state: RouterStateSnapshot
113+
) => state.url !== 'next',
114+
});
115+
116+
const router: Router = TestBed.get(Router);
117+
const log = logOfRouterAndActionsAndStore();
118+
119+
const hasRouterState = (action: RouterAction<any>) =>
120+
!!action.payload.routerState;
121+
122+
router
123+
.navigateByUrl('/')
124+
.then(() => {
125+
expect(actionLog.filter(hasRouterState).length).toBe(actionLog.length);
126+
})
127+
.then(() => {
128+
actionLog.splice(0);
129+
return router.navigateByUrl('next');
130+
})
131+
.then(() => {
132+
expect(actionLog.filter(hasRouterState).length).toBe(actionLog.length);
133+
done();
134+
});
135+
});
136+
91137
xit('should support preventing navigation', (done: any) => {
92138
const reducer = (state: string = '', action: RouterAction<any>) => {
93139
if (

modules/router-store/src/actions.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,21 @@ export const ROUTER_REQUEST = '@ngrx/router-store/request';
1919
/**
2020
* Payload of ROUTER_REQUEST
2121
*/
22-
export type RouterRequestPayload = {
22+
export type RouterRequestPayload<
23+
T extends BaseRouterStoreState = SerializedRouterStateSnapshot
24+
> = {
25+
routerState: T;
2326
event: NavigationStart;
2427
};
2528

2629
/**
2730
* An action dispatched when a router navigation request is fired.
2831
*/
29-
export type RouterRequestAction = {
32+
export type RouterRequestAction<
33+
T extends BaseRouterStoreState = SerializedRouterStateSnapshot
34+
> = {
3035
type: typeof ROUTER_REQUEST;
31-
payload: RouterRequestPayload;
36+
payload: RouterRequestPayload<T>;
3237
};
3338

3439
/**
@@ -39,7 +44,9 @@ export const ROUTER_NAVIGATION = '@ngrx/router-store/navigation';
3944
/**
4045
* Payload of ROUTER_NAVIGATION.
4146
*/
42-
export type RouterNavigationPayload<T extends BaseRouterStoreState> = {
47+
export type RouterNavigationPayload<
48+
T extends BaseRouterStoreState = SerializedRouterStateSnapshot
49+
> = {
4350
routerState: T;
4451
event: RoutesRecognized;
4552
};
@@ -62,7 +69,10 @@ export const ROUTER_CANCEL = '@ngrx/router-store/cancel';
6269
/**
6370
* Payload of ROUTER_CANCEL.
6471
*/
65-
export type RouterCancelPayload<T, V extends BaseRouterStoreState> = {
72+
export type RouterCancelPayload<
73+
T,
74+
V extends BaseRouterStoreState = SerializedRouterStateSnapshot
75+
> = {
6676
routerState: V;
6777
storeState: T;
6878
event: NavigationCancel;
@@ -87,7 +97,10 @@ export const ROUTER_ERROR = '@ngrx/router-store/error';
8797
/**
8898
* Payload of ROUTER_ERROR.
8999
*/
90-
export type RouterErrorPayload<T, V extends BaseRouterStoreState> = {
100+
export type RouterErrorPayload<
101+
T,
102+
V extends BaseRouterStoreState = SerializedRouterStateSnapshot
103+
> = {
91104
routerState: V;
92105
storeState: T;
93106
event: NavigationError;
@@ -112,16 +125,21 @@ export const ROUTER_NAVIGATED = '@ngrx/router-store/navigated';
112125
/**
113126
* Payload of ROUTER_NAVIGATED.
114127
*/
115-
export type RouterNavigatedPayload = {
128+
export type RouterNavigatedPayload<
129+
T extends BaseRouterStoreState = SerializedRouterStateSnapshot
130+
> = {
131+
routerState: T;
116132
event: NavigationEnd;
117133
};
118134

119135
/**
120136
* An action dispatched after navigation has ended and new route is active.
121137
*/
122-
export type RouterNavigatedAction = {
138+
export type RouterNavigatedAction<
139+
T extends BaseRouterStoreState = SerializedRouterStateSnapshot
140+
> = {
123141
type: typeof ROUTER_NAVIGATED;
124-
payload: RouterNavigatedPayload;
142+
payload: RouterNavigatedPayload<T>;
125143
};
126144

127145
/**
@@ -131,8 +149,8 @@ export type RouterAction<
131149
T,
132150
V extends BaseRouterStoreState = SerializedRouterStateSnapshot
133151
> =
134-
| RouterRequestAction
152+
| RouterRequestAction<V>
135153
| RouterNavigationAction<V>
136154
| RouterCancelAction<T, V>
137155
| RouterErrorAction<T, V>
138-
| RouterNavigatedAction;
156+
| RouterNavigatedAction<V>;

modules/router-store/src/router_store_module.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
RoutesRecognized,
1414
NavigationStart,
1515
Event,
16+
RouterEvent,
1617
} from '@angular/router';
1718
import { select, Selector, Store } from '@ngrx/store';
1819
import { withLatestFrom } from 'rxjs/operators';
@@ -51,6 +52,12 @@ export interface StoreRouterConfig<
5152
navigationActionTiming?: NavigationActionTiming;
5253
}
5354

55+
interface StoreRouterActionPayload {
56+
event: RouterEvent;
57+
routerState?: SerializedRouterStateSnapshot;
58+
storeState?: any;
59+
}
60+
5461
export enum NavigationActionTiming {
5562
PreActivation = 1,
5663
PostActivation = 2,
@@ -281,28 +288,38 @@ export class StoreRouterConnectingModule {
281288

282289
private dispatchRouterCancel(event: NavigationCancel): void {
283290
this.dispatchRouterAction(ROUTER_CANCEL, {
284-
routerState: this.routerState!,
285291
storeState: this.storeState,
286292
event,
287293
});
288294
}
289295

290296
private dispatchRouterError(event: NavigationError): void {
291297
this.dispatchRouterAction(ROUTER_ERROR, {
292-
routerState: this.routerState!,
293298
storeState: this.storeState,
294299
event: new NavigationError(event.id, event.url, `${event}`),
295300
});
296301
}
297302

298303
private dispatchRouterNavigated(event: NavigationEnd): void {
299-
this.dispatchRouterAction(ROUTER_NAVIGATED, { event });
304+
const routerState = this.serializer.serialize(
305+
this.router.routerState.snapshot
306+
);
307+
this.dispatchRouterAction(ROUTER_NAVIGATED, { event, routerState });
300308
}
301309

302-
private dispatchRouterAction(type: string, payload: any): void {
310+
private dispatchRouterAction(
311+
type: string,
312+
payload: StoreRouterActionPayload
313+
): void {
303314
this.trigger = RouterTrigger.ROUTER;
304315
try {
305-
this.store.dispatch({ type, payload });
316+
this.store.dispatch({
317+
type,
318+
payload: {
319+
routerState: this.routerState,
320+
...payload,
321+
},
322+
});
306323
} finally {
307324
this.trigger = RouterTrigger.NONE;
308325
}

0 commit comments

Comments
 (0)