From 77051585cc5318135fcfaf4861bf5436623c8422 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Thu, 28 May 2020 14:53:39 -0700 Subject: [PATCH] feat(router-store): add route fragment selector (#2543) --- modules/router-store/spec/router_selectors.spec.ts | 9 +++++++++ modules/router-store/src/models.ts | 1 + modules/router-store/src/router_selectors.ts | 5 +++++ projects/ngrx.io/content/guide/router-store/selectors.md | 1 + 4 files changed, 16 insertions(+) diff --git a/modules/router-store/spec/router_selectors.spec.ts b/modules/router-store/spec/router_selectors.spec.ts index 88c24c47bd..1a80b8ee24 100644 --- a/modules/router-store/spec/router_selectors.spec.ts +++ b/modules/router-store/spec/router_selectors.spec.ts @@ -69,6 +69,7 @@ const mockData = { ref: 'ngrx.io', }, }, + fragment: 'test-fragment', children: [], }, fragment: null, @@ -141,6 +142,14 @@ describe('Router State Selectors', () => { expect(result).toEqual(undefined); }); + it('should create a selector for selecting the fragment', () => { + const result = selectors.selectFragment(state); + + expect(result).toEqual( + state.router.state.root.firstChild.firstChild.fragment + ); + }); + it('should create a selector for selecting the query params', () => { const result = selectors.selectQueryParams(state); diff --git a/modules/router-store/src/models.ts b/modules/router-store/src/models.ts index c147f918ad..2acdcc4018 100644 --- a/modules/router-store/src/models.ts +++ b/modules/router-store/src/models.ts @@ -3,6 +3,7 @@ import { MemoizedSelector } from '@ngrx/store'; export interface RouterStateSelectors { selectCurrentRoute: MemoizedSelector; + selectFragment: MemoizedSelector; selectQueryParams: MemoizedSelector; selectQueryParam: (param: string) => MemoizedSelector; selectRouteParams: MemoizedSelector; diff --git a/modules/router-store/src/router_selectors.ts b/modules/router-store/src/router_selectors.ts index 706dc6a2ec..1d1044bb8b 100644 --- a/modules/router-store/src/router_selectors.ts +++ b/modules/router-store/src/router_selectors.ts @@ -22,6 +22,10 @@ export function getSelectors( } return route; }); + const selectFragment = createSelector( + selectCurrentRoute, + route => route && route.fragment + ); const selectQueryParams = createSelector( selectCurrentRoute, route => route && route.queryParams @@ -45,6 +49,7 @@ export function getSelectors( return { selectCurrentRoute, + selectFragment, selectQueryParams, selectQueryParam, selectRouteParams, diff --git a/projects/ngrx.io/content/guide/router-store/selectors.md b/projects/ngrx.io/content/guide/router-store/selectors.md index 602af17567..88bc1fa467 100644 --- a/projects/ngrx.io/content/guide/router-store/selectors.md +++ b/projects/ngrx.io/content/guide/router-store/selectors.md @@ -26,6 +26,7 @@ export const selectRouter = createFeatureSelector< 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