Skip to content

Commit f17589f

Browse files
Jefioziebrandonroberts
authored andcommittedDec 14, 2019
feat(router-store): add action creator for root router actions (#2272)
Closes #2206
1 parent 4368cc7 commit f17589f

File tree

3 files changed

+61
-25
lines changed

3 files changed

+61
-25
lines changed
 

‎modules/router-store/src/actions.ts

+25
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88

99
import { BaseRouterStoreState } from './serializers/base';
1010
import { SerializedRouterStateSnapshot } from './serializers/default_serializer';
11+
import { createAction, props } from '@ngrx/store';
1112

1213
/**
1314
* An action dispatched when a router navigation request is fired.
@@ -34,6 +35,10 @@ export type RouterRequestAction<
3435
payload: RouterRequestPayload<T>;
3536
};
3637

38+
export const routerRequestAction = createAction(
39+
ROUTER_REQUEST,
40+
props<{ payload: RouterRequestPayload<SerializedRouterStateSnapshot> }>()
41+
);
3742
/**
3843
* An action dispatched when the router navigates.
3944
*/
@@ -59,6 +64,11 @@ export type RouterNavigationAction<
5964
payload: RouterNavigationPayload<T>;
6065
};
6166

67+
export const routerNavigationAction = createAction(
68+
ROUTER_NAVIGATION,
69+
props<{ payload: RouterNavigationPayload<SerializedRouterStateSnapshot> }>()
70+
);
71+
6272
/**
6373
* An action dispatched when the router cancels navigation.
6474
*/
@@ -87,6 +97,11 @@ export type RouterCancelAction<
8797
payload: RouterCancelPayload<T, V>;
8898
};
8999

100+
export const routerCancelAction = createAction(
101+
ROUTER_CANCEL,
102+
props<{ payload: RouterCancelPayload<SerializedRouterStateSnapshot> }>()
103+
);
104+
90105
/**
91106
* An action dispatched when the router errors.
92107
*/
@@ -115,6 +130,11 @@ export type RouterErrorAction<
115130
payload: RouterErrorPayload<T, V>;
116131
};
117132

133+
export const routerErrorAction = createAction(
134+
ROUTER_ERROR,
135+
props<{ payload: RouterErrorPayload<SerializedRouterStateSnapshot> }>()
136+
);
137+
118138
/**
119139
* An action dispatched after navigation has ended and new route is active.
120140
*/
@@ -140,6 +160,11 @@ export type RouterNavigatedAction<
140160
payload: RouterNavigatedPayload<T>;
141161
};
142162

163+
export const routerNavigatedAction = createAction(
164+
ROUTER_NAVIGATED,
165+
props<{ payload: RouterNavigatedPayload<SerializedRouterStateSnapshot> }>()
166+
);
167+
143168
/**
144169
* A union type of router actions.
145170
*/

‎modules/router-store/src/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export {
1515
RouterNavigationPayload,
1616
RouterRequestAction,
1717
RouterRequestPayload,
18+
routerCancelAction,
19+
routerErrorAction,
20+
routerNavigatedAction,
21+
routerNavigationAction,
22+
routerRequestAction,
1823
} from './actions';
1924
export { routerReducer, RouterReducerState } from './reducer';
2025
export {
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,56 @@
11
# Router Actions
22

3-
Router Store provides five navigation actions which are dispatched in a specific order. The `routerReducer` provided by Router Store updates its state with the latest router state given by the actions.
4-
5-
## Order of actions
6-
7-
Success case:
8-
9-
- `ROUTER_REQUEST`
10-
- `ROUTER_NAVIGATION`
11-
- `ROUTER_NAVIGATED`
12-
13-
Error / Cancel case (with early Navigation Action Timing):
14-
15-
- `ROUTER_REQUEST`
16-
- `ROUTER_NAVIGATION`
17-
- `ROUTER_CANCEL` / `ROUTER_ERROR`
18-
19-
Error / Cancel case (with late Navigation Action Timing):
20-
21-
- `ROUTER_REQUEST`
22-
- `ROUTER_CANCEL` / `ROUTER_ERROR`
3+
Router Store provides five navigation actions which are dispatched in a specific order. The `routerReducer` provided by Router Store updates its state with the latest router state given by the actions. By default we recommend to use the creator functions we provide.
234

245
## Actions
256

26-
### ROUTER_REQUEST
7+
### routerRequestAction
278

289
At the start of each navigation, the router will dispatch a `ROUTER_REQUEST` action.
2910

30-
### ROUTER_NAVIGATION
11+
### routerNavigationAction
3112

3213
During navigation, before any guards or resolvers run, the router will dispatch a `ROUTER_NAVIGATION` action.
3314

3415
If you want the `ROUTER_NAVIGATION` to be dispatched after guards or resolvers run, change the Navigation Action Timing.
3516

36-
### ROUTER_NAVIGATED
17+
### routerNavigatedAction
3718

3819
After a successful navigation, the router will dispatch a `ROUTER_NAVIGATED` action.
3920

40-
### ROUTER_CANCEL
21+
### routerCancelAction
4122

4223
When the navigation is cancelled, for example due to a guard saying that the user cannot access the requested page, the router will dispatch a `ROUTER_CANCEL` action.
4324

4425
The action contains the store state before the navigation. You can use it to restore the consistency of the store.
4526

46-
### ROUTER_ERROR
27+
### routerErrorAction
4728

4829
When there is an error during navigation, the router will dispatch a `ROUTER_ERROR` action.
4930

5031
The action contains the store state before the navigation. You can use it to restore the consistency of the store.
32+
33+
<div class="alert is-important">
34+
35+
**Note:** You can also still use the action type, which was the previously defined way before action creators were introduced in NgRx. If you are looking for examples of the action types, visit the documentation for [versions 7.x and prior](https://v7.ngrx.io/guide/router-store/actions).
36+
37+
</div>
38+
39+
## Order of actions
40+
41+
Success case:
42+
43+
- `ROUTER_REQUEST`
44+
- `ROUTER_NAVIGATION`
45+
- `ROUTER_NAVIGATED`
46+
47+
Error / Cancel case (with early Navigation Action Timing):
48+
49+
- `ROUTER_REQUEST`
50+
- `ROUTER_NAVIGATION`
51+
- `ROUTER_CANCEL` / `ROUTER_ERROR`
52+
53+
Error / Cancel case (with late Navigation Action Timing):
54+
55+
- `ROUTER_REQUEST`
56+
- `ROUTER_CANCEL` / `ROUTER_ERROR`

0 commit comments

Comments
 (0)
Please sign in to comment.