Skip to content

Commit

Permalink
feat(router-store) add selectParamFromRouterTree selector
Browse files Browse the repository at this point in the history
  • Loading branch information
markostanimirovic committed Dec 9, 2020
1 parent 7c05f5c commit 72bca54
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
19 changes: 17 additions & 2 deletions modules/router-store/spec/router_selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ const mockData = {
},
fragment: null,
firstChild: {
params: {},
params: {
p: 'first-occurrence',
},
paramMap: {
params: {},
params: {
p: 'first-occurrence',
},
},
data: {},
url: [
Expand All @@ -42,10 +46,12 @@ const mockData = {
firstChild: {
params: {
id: 'etyDDwAAQBAJ',
p: 'second-occurrence',
},
paramMap: {
params: {
id: 'etyDDwAAQBAJ',
p: 'second-occurrence',
},
},
data: {
Expand Down Expand Up @@ -182,6 +188,15 @@ describe('Router State Selectors', () => {
);
});

it('should create a selector for selecting the first occurrence of a specific route param', () => {
const result = selectors.selectParamFromRouterTree('p')(state);

expect(result).toEqual(state.router.state.root.firstChild.params.p);
expect(result).not.toEqual(
state.router.state.root.firstChild.firstChild.params.p
);
});

it('should create a selector for selecting the route data', () => {
const result = selectors.selectRouteData(state);

Expand Down
3 changes: 3 additions & 0 deletions modules/router-store/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export interface RouterStateSelectors<V> {
selectQueryParam: (param: string) => MemoizedSelector<V, string | undefined>;
selectRouteParams: MemoizedSelector<V, Params>;
selectRouteParam: (param: string) => MemoizedSelector<V, string | undefined>;
selectParamFromRouterTree: (
param: string
) => MemoizedSelector<V, string | undefined>;
selectRouteData: MemoizedSelector<V, Data>;
selectUrl: MemoizedSelector<V, string>;
}
16 changes: 16 additions & 0 deletions modules/router-store/src/router_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ export function getSelectors<V>(
selectCurrentRoute,
(route) => route && route.data
);
const selectParamFromRouterTree = (param: string) =>
createSelector(selectRouterState, (routerState) => {
let paramValue: string | undefined;
let route = routerState?.root;

while (route?.firstChild) {
route = route.firstChild;
if (route?.params?.[param]) {
paramValue = route.params[param];
break;
}
}

return paramValue;
});
const selectUrl = createSelector(
selectRouterState,
(routerState) => routerState && routerState.url
Expand All @@ -57,6 +72,7 @@ export function getSelectors<V>(
selectQueryParam,
selectRouteParams,
selectRouteParam,
selectParamFromRouterTree,
selectRouteData,
selectUrl,
};
Expand Down
17 changes: 9 additions & 8 deletions projects/ngrx.io/content/guide/router-store/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ export const selectRouter = createFeatureSelector<
>('router');

export const {
selectCurrentRoute, // select the current route
selectFragment, // select the current route fragment
selectQueryParams, // select the current route query params
selectQueryParam, // factory function to select a query param
selectRouteParams, // select the current route params
selectRouteParam, // factory function to select a route param
selectRouteData, // select the current route data
selectUrl, // select the current url
selectCurrentRoute, // select the current route
selectFragment, // select the current route fragment
selectQueryParams, // select the current route query params
selectQueryParam, // factory function to select a query param
selectRouteParams, // select the current route params
selectRouteParam, // factory function to select a route param
selectParamFromRouterTree, // factory function to select the first occurrence of a route param
selectRouteData, // select the current route data
selectUrl, // select the current url
} = fromRouter.getSelectors(selectRouter);

export const selectSelectedCarId = selectQueryParam('carId');
Expand Down

0 comments on commit 72bca54

Please sign in to comment.