From 81bc0d9f481799a999a6aa5a71bb76cca2894ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Fern=C3=A1ndez?= Date: Fri, 2 Dec 2022 04:38:09 +0100 Subject: [PATCH] feat(router-store): add new selectRouteDataParam selector (#3673) (#3686) Closes #3673 --- .../spec/router_selectors.spec.ts | 8 ++++++++ modules/router-store/src/models.ts | 3 +++ modules/router-store/src/router_selectors.ts | 3 +++ .../src/app/router.selectors.ts | 19 ++++++++++--------- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/modules/router-store/spec/router_selectors.spec.ts b/modules/router-store/spec/router_selectors.spec.ts index 580e87d912..56729e8035 100644 --- a/modules/router-store/spec/router_selectors.spec.ts +++ b/modules/router-store/spec/router_selectors.spec.ts @@ -227,6 +227,14 @@ describe('Router State Selectors', () => { ); }); + it('should create a selector for selecting a specific route data param', () => { + const result = selectors.selectRouteDataParam('testData')(state); + + expect(result).toEqual( + state.router.state.root.firstChild.firstChild.data.testData + ); + }); + it('should create a selector for selecting the url', () => { const result = selectors.selectUrl(state); diff --git a/modules/router-store/src/models.ts b/modules/router-store/src/models.ts index cd1531cbcb..fd0f03c240 100644 --- a/modules/router-store/src/models.ts +++ b/modules/router-store/src/models.ts @@ -9,6 +9,9 @@ export interface RouterStateSelectors { selectRouteParams: MemoizedSelector; selectRouteParam: (param: string) => MemoizedSelector; selectRouteData: MemoizedSelector; + selectRouteDataParam: ( + param: string + ) => MemoizedSelector; selectUrl: MemoizedSelector; selectTitle: MemoizedSelector; } diff --git a/modules/router-store/src/router_selectors.ts b/modules/router-store/src/router_selectors.ts index 33d139e8c4..abc40835fb 100644 --- a/modules/router-store/src/router_selectors.ts +++ b/modules/router-store/src/router_selectors.ts @@ -54,6 +54,8 @@ export function getSelectors>( selectCurrentRoute, (route) => route && route.data ); + const selectRouteDataParam = (param: string) => + createSelector(selectRouteData, (data) => data && data[param]); const selectUrl = createSelector( selectRouterState, (routerState) => routerState && routerState.url @@ -75,6 +77,7 @@ export function getSelectors>( selectRouteParams, selectRouteParam, selectRouteData, + selectRouteDataParam, selectUrl, selectTitle, }; diff --git a/projects/ngrx.io/content/examples/router-store-selectors/src/app/router.selectors.ts b/projects/ngrx.io/content/examples/router-store-selectors/src/app/router.selectors.ts index a6163afbac..f626fdb13e 100644 --- a/projects/ngrx.io/content/examples/router-store-selectors/src/app/router.selectors.ts +++ b/projects/ngrx.io/content/examples/router-store-selectors/src/app/router.selectors.ts @@ -6,14 +6,15 @@ import { getSelectors, RouterReducerState } from '@ngrx/router-store'; // export const selectRouter = createFeatureSelector('yourFeatureName'); 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 - selectTitle, // Select the title if available + 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 + selectRouteDataParam, // factory function to select a route data param + selectUrl, // select the current url + selectTitle, // select the title if available } = getSelectors(); // #enddocregion routerSelectors