Skip to content

Commit 6e5fe96

Browse files
committed
94242: Add optional NavigationExtras to PaginationService.updateRoute
1 parent 9fc7b57 commit 6e5fe96

2 files changed

Lines changed: 54 additions & 16 deletions

File tree

src/app/core/pagination/pagination.service.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ describe('PaginationService', () => {
101101

102102
expect(router.navigate).toHaveBeenCalledWith([], {queryParams: navigateParams, queryParamsHandling: 'merge'});
103103
});
104+
it('should pass on navigationExtras to router.navigate', () => {
105+
service.updateRoute('test', {page: 2}, undefined, undefined, { queryParamsHandling: 'preserve', replaceUrl: true, preserveFragment: true });
106+
107+
const navigateParams = {};
108+
navigateParams[`test.page`] = `2`;
109+
navigateParams[`test.rpp`] = `10`;
110+
navigateParams[`test.sf`] = `score`;
111+
navigateParams[`test.sd`] = `ASC`;
112+
113+
expect(router.navigate).toHaveBeenCalledWith([], {queryParams: navigateParams, queryParamsHandling: 'preserve', replaceUrl: true, preserveFragment: true });
114+
});
104115
});
105116
describe('updateRouteWithUrl', () => {
106117
it('should update the route with the provided page params and url', () => {
@@ -125,7 +136,17 @@ describe('PaginationService', () => {
125136

126137
expect(router.navigate).toHaveBeenCalledWith(['someUrl'], {queryParams: navigateParams, queryParamsHandling: 'merge'});
127138
});
139+
it('should pass on navigationExtras to router.navigate', () => {
140+
service.updateRouteWithUrl('test',['someUrl'], {page: 2}, undefined, undefined, { queryParamsHandling: 'preserve', replaceUrl: true, preserveFragment: true });
128141

142+
const navigateParams = {};
143+
navigateParams[`test.page`] = `2`;
144+
navigateParams[`test.rpp`] = `10`;
145+
navigateParams[`test.sf`] = `score`;
146+
navigateParams[`test.sd`] = `ASC`;
147+
148+
expect(router.navigate).toHaveBeenCalledWith(['someUrl'], {queryParams: navigateParams, queryParamsHandling: 'preserve', replaceUrl: true, preserveFragment: true });
149+
});
129150
});
130151
describe('clearPagination', () => {
131152
it('should clear the pagination next time the updateRoute/updateRouteWithUrl method is called', () => {

src/app/core/pagination/pagination.service.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { Router } from '@angular/router';
2+
import { NavigationExtras, Router } from '@angular/router';
33
import { RouteService } from '../services/route.service';
44
import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model';
55
import { combineLatest as observableCombineLatest, Observable } from 'rxjs';
@@ -114,15 +114,22 @@ export class PaginationService {
114114
* @param params - The page related params to update in the route
115115
* @param extraParams - Addition params unrelated to the pagination that need to be added to the route
116116
* @param retainScrollPosition - Scroll to the pagination component after updating the route instead of the top of the page
117+
* @param navigationExtras - Extra parameters to pass on to `router.navigate`. Can be used to override values set by this service.
117118
*/
118-
updateRoute(paginationId: string, params: {
119-
page?: number
120-
pageSize?: number
121-
sortField?: string
122-
sortDirection?: SortDirection
123-
}, extraParams?, retainScrollPosition?: boolean) {
119+
updateRoute(
120+
paginationId: string,
121+
params: {
122+
page?: number
123+
pageSize?: number
124+
sortField?: string
125+
sortDirection?: SortDirection
126+
},
127+
extraParams?,
128+
retainScrollPosition?: boolean,
129+
navigationExtras?: NavigationExtras,
130+
) {
124131

125-
this.updateRouteWithUrl(paginationId, [], params, extraParams, retainScrollPosition);
132+
this.updateRouteWithUrl(paginationId, [], params, extraParams, retainScrollPosition, navigationExtras);
126133
}
127134

128135
/**
@@ -132,13 +139,21 @@ export class PaginationService {
132139
* @param params - The page related params to update in the route
133140
* @param extraParams - Addition params unrelated to the pagination that need to be added to the route
134141
* @param retainScrollPosition - Scroll to the pagination component after updating the route instead of the top of the page
142+
* @param navigationExtras - Extra parameters to pass on to `router.navigate`. Can be used to override values set by this service.
135143
*/
136-
updateRouteWithUrl(paginationId: string, url: string[], params: {
137-
page?: number
138-
pageSize?: number
139-
sortField?: string
140-
sortDirection?: SortDirection
141-
}, extraParams?, retainScrollPosition?: boolean) {
144+
updateRouteWithUrl(
145+
paginationId: string,
146+
url: string[],
147+
params: {
148+
page?: number
149+
pageSize?: number
150+
sortField?: string
151+
sortDirection?: SortDirection
152+
},
153+
extraParams?,
154+
retainScrollPosition?: boolean,
155+
navigationExtras?: NavigationExtras,
156+
) {
142157
this.getCurrentRouting(paginationId).subscribe((currentFindListOptions) => {
143158
const currentParametersWithIdName = this.getParametersWithIdName(paginationId, currentFindListOptions);
144159
const parametersWithIdName = this.getParametersWithIdName(paginationId, params);
@@ -149,12 +164,14 @@ export class PaginationService {
149164
this.router.navigate(url, {
150165
queryParams: queryParams,
151166
queryParamsHandling: 'merge',
152-
fragment: `p-${paginationId}`
167+
fragment: `p-${paginationId}`,
168+
...navigationExtras,
153169
});
154170
} else {
155171
this.router.navigate(url, {
156172
queryParams: queryParams,
157-
queryParamsHandling: 'merge'
173+
queryParamsHandling: 'merge',
174+
...navigationExtras,
158175
});
159176
}
160177
this.clearParams = {};

0 commit comments

Comments
 (0)