Skip to content

Commit

Permalink
feat(router-store): add selectQueryParam and selectRouteParam (#2014)
Browse files Browse the repository at this point in the history
Select a specific query param or route param

Usage:

export const selectId = selectRouteParam('id');
export const selectRef = selectQueryParam('ref');
  • Loading branch information
timdeschryver authored and brandonroberts committed Jul 16, 2019
1 parent c00a9c2 commit 57fd3d7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
30 changes: 28 additions & 2 deletions modules/router-store/spec/router_selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ const mockData = {
routeConfig: {
path: ':id',
},
queryParams: {},
queryParams: {
ref: 'ngrx.io',
},
queryParamMap: {
params: {},
params: {
ref: 'ngrx.io',
},
},
children: [],
},
Expand Down Expand Up @@ -115,11 +119,13 @@ describe('Router State Selectors', () => {

selectors = getSelectors((state: State) => state.router);
});

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

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

it('should return undefined from selectCurrentRoute if routerState does not exist', () => {
interface State {
router: any;
Expand All @@ -134,27 +140,47 @@ describe('Router State Selectors', () => {

expect(result).toEqual(undefined);
});

it('should create a selector for selecting the query params', () => {
const result = selectors.selectQueryParams(state);

expect(result).toEqual(
state.router.state.root.firstChild.firstChild.queryParams
);
});

it('should create a selector for selecting a specific query param', () => {
const result = selectors.selectQueryParam('ref')(state);

expect(result).toEqual(
state.router.state.root.firstChild.firstChild.queryParams.ref
);
});

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

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

it('should create a selector for selecting a specific route param', () => {
const result = selectors.selectRouteParam('id')(state);

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

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

expect(result).toEqual(
state.router.state.root.firstChild.firstChild.data
);
});

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

Expand Down
2 changes: 2 additions & 0 deletions modules/router-store/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Data, Params } from '@angular/router';
export interface RouterStateSelectors<V> {
selectCurrentRoute: (state: V) => any;
selectQueryParams: (state: V) => Params;
selectQueryParam: (param: string) => (state: V) => string | undefined;
selectRouteParams: (state: V) => Params;
selectRouteParam: (param: string) => (state: V) => string | undefined;
selectRouteData: (state: V) => Data;
selectUrl: (state: V) => string;
}
6 changes: 6 additions & 0 deletions modules/router-store/src/router_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ export function getSelectors<V>(
selectCurrentRoute,
route => route && route.queryParams
);
const selectQueryParam = (param: string) =>
createSelector(selectQueryParams, params => params && params[param]);
const selectRouteParams = createSelector(
selectCurrentRoute,
route => route && route.params
);
const selectRouteParam = (param: string) =>
createSelector(selectRouteParams, params => params && params[param]);
const selectRouteData = createSelector(
selectCurrentRoute,
route => route && route.data
Expand All @@ -42,7 +46,9 @@ export function getSelectors<V>(
return {
selectCurrentRoute,
selectQueryParams,
selectQueryParam,
selectRouteParams,
selectRouteParam,
selectRouteData,
selectUrl,
};
Expand Down
8 changes: 6 additions & 2 deletions projects/ngrx.io/content/guide/router-store/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Usage:
`reducers/index.ts`

```ts
import { getSelectors, RouterReducerState } from '@ngrx/router-store';
import * as fromRouter from '@ngrx/router-store';

export interface State {
router: fromRouter.RouterReducerState<any>;
Expand All @@ -25,9 +25,13 @@ export const selectRouter = createFeatureSelector<

const {
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
} = getSelectors(selectRouter);
} = fromRouter.getSelectors(selectRouter);

export const selectRouteId = selectRouteParam('id');
export const selectStatus = selectQueryParam('status');
```

0 comments on commit 57fd3d7

Please sign in to comment.