Skip to content

Commit 3a1f359

Browse files
feat(router-store): add selectParamFromRouterState selector (#2771)
Closes #2758
1 parent 6da7158 commit 3a1f359

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ const mockData = {
1818
},
1919
fragment: null,
2020
firstChild: {
21-
params: {},
21+
params: {
22+
p: 'first-occurrence',
23+
},
2224
paramMap: {
23-
params: {},
25+
params: {
26+
p: 'first-occurrence',
27+
},
2428
},
2529
data: {},
2630
url: [
@@ -42,10 +46,12 @@ const mockData = {
4246
firstChild: {
4347
params: {
4448
id: 'etyDDwAAQBAJ',
49+
p: 'second-occurrence',
4550
},
4651
paramMap: {
4752
params: {
4853
id: 'etyDDwAAQBAJ',
54+
p: 'second-occurrence',
4955
},
5056
},
5157
data: {
@@ -182,6 +188,15 @@ describe('Router State Selectors', () => {
182188
);
183189
});
184190

191+
it('should create a selector for selecting the first occurrence of a specific route param', () => {
192+
const result = selectors.selectParamFromRouterState('p')(state);
193+
194+
expect(result).toEqual(state.router.state.root.firstChild.params.p);
195+
expect(result).not.toEqual(
196+
state.router.state.root.firstChild.firstChild.params.p
197+
);
198+
});
199+
185200
it('should create a selector for selecting the route data', () => {
186201
const result = selectors.selectRouteData(state);
187202

modules/router-store/src/models.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export interface RouterStateSelectors<V> {
88
selectQueryParam: (param: string) => MemoizedSelector<V, string | undefined>;
99
selectRouteParams: MemoizedSelector<V, Params>;
1010
selectRouteParam: (param: string) => MemoizedSelector<V, string | undefined>;
11+
selectParamFromRouterState: (
12+
param: string
13+
) => MemoizedSelector<V, string | undefined>;
1114
selectRouteData: MemoizedSelector<V, Data>;
1215
selectUrl: MemoizedSelector<V, string>;
1316
}

modules/router-store/src/router_selectors.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ export function getSelectors<V>(
4141
);
4242
const selectRouteParam = (param: string) =>
4343
createSelector(selectRouteParams, (params) => params && params[param]);
44+
const selectParamFromRouterState = (param: string) =>
45+
createSelector(selectRouterState, (routerState) => {
46+
let paramValue: string | undefined;
47+
let route = routerState?.root;
48+
49+
while (route?.firstChild) {
50+
route = route.firstChild;
51+
if (route?.params?.[param]) {
52+
paramValue = route.params[param];
53+
break;
54+
}
55+
}
56+
57+
return paramValue;
58+
});
4459
const selectRouteData = createSelector(
4560
selectCurrentRoute,
4661
(route) => route && route.data
@@ -57,6 +72,7 @@ export function getSelectors<V>(
5772
selectQueryParam,
5873
selectRouteParams,
5974
selectRouteParam,
75+
selectParamFromRouterState,
6076
selectRouteData,
6177
selectUrl,
6278
};

projects/ngrx.io/content/guide/router-store/selectors.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ export const selectRouter = createFeatureSelector<
2525
>('router');
2626

2727
export const {
28-
selectCurrentRoute, // select the current route
29-
selectFragment, // select the current route fragment
30-
selectQueryParams, // select the current route query params
31-
selectQueryParam, // factory function to select a query param
32-
selectRouteParams, // select the current route params
33-
selectRouteParam, // factory function to select a route param
34-
selectRouteData, // select the current route data
35-
selectUrl, // select the current url
28+
selectCurrentRoute, // select the current route
29+
selectFragment, // select the current route fragment
30+
selectQueryParams, // select the current route query params
31+
selectQueryParam, // factory function to select a query param
32+
selectRouteParams, // select the current route params
33+
selectRouteParam, // factory function to select a route param
34+
selectParamFromRouterState, // factory function to select the first occurrence of a route param
35+
selectRouteData, // select the current route data
36+
selectUrl, // select the current url
3637
} = fromRouter.getSelectors(selectRouter);
3738

3839
export const selectSelectedCarId = selectQueryParam('carId');

0 commit comments

Comments
 (0)