94242: Add optional NavigationExtras to PaginationService.updateRoute

This commit is contained in:
Yury Bondarenko
2022-09-15 11:50:50 +02:00
parent 9fc7b57157
commit 6e5fe96696
2 changed files with 54 additions and 16 deletions

View File

@@ -101,6 +101,17 @@ describe('PaginationService', () => {
expect(router.navigate).toHaveBeenCalledWith([], {queryParams: navigateParams, queryParamsHandling: 'merge'}); expect(router.navigate).toHaveBeenCalledWith([], {queryParams: navigateParams, queryParamsHandling: 'merge'});
}); });
it('should pass on navigationExtras to router.navigate', () => {
service.updateRoute('test', {page: 2}, undefined, undefined, { queryParamsHandling: 'preserve', replaceUrl: true, preserveFragment: true });
const navigateParams = {};
navigateParams[`test.page`] = `2`;
navigateParams[`test.rpp`] = `10`;
navigateParams[`test.sf`] = `score`;
navigateParams[`test.sd`] = `ASC`;
expect(router.navigate).toHaveBeenCalledWith([], {queryParams: navigateParams, queryParamsHandling: 'preserve', replaceUrl: true, preserveFragment: true });
});
}); });
describe('updateRouteWithUrl', () => { describe('updateRouteWithUrl', () => {
it('should update the route with the provided page params and url', () => { it('should update the route with the provided page params and url', () => {
@@ -125,7 +136,17 @@ describe('PaginationService', () => {
expect(router.navigate).toHaveBeenCalledWith(['someUrl'], {queryParams: navigateParams, queryParamsHandling: 'merge'}); expect(router.navigate).toHaveBeenCalledWith(['someUrl'], {queryParams: navigateParams, queryParamsHandling: 'merge'});
}); });
it('should pass on navigationExtras to router.navigate', () => {
service.updateRouteWithUrl('test',['someUrl'], {page: 2}, undefined, undefined, { queryParamsHandling: 'preserve', replaceUrl: true, preserveFragment: true });
const navigateParams = {};
navigateParams[`test.page`] = `2`;
navigateParams[`test.rpp`] = `10`;
navigateParams[`test.sf`] = `score`;
navigateParams[`test.sd`] = `ASC`;
expect(router.navigate).toHaveBeenCalledWith(['someUrl'], {queryParams: navigateParams, queryParamsHandling: 'preserve', replaceUrl: true, preserveFragment: true });
});
}); });
describe('clearPagination', () => { describe('clearPagination', () => {
it('should clear the pagination next time the updateRoute/updateRouteWithUrl method is called', () => { it('should clear the pagination next time the updateRoute/updateRouteWithUrl method is called', () => {

View File

@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Router } from '@angular/router'; import { NavigationExtras, Router } from '@angular/router';
import { RouteService } from '../services/route.service'; import { RouteService } from '../services/route.service';
import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model'; import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model';
import { combineLatest as observableCombineLatest, Observable } from 'rxjs'; import { combineLatest as observableCombineLatest, Observable } from 'rxjs';
@@ -114,15 +114,22 @@ export class PaginationService {
* @param params - The page related params to update in the route * @param params - The page related params to update in the route
* @param extraParams - Addition params unrelated to the pagination that need to be added to the route * @param extraParams - Addition params unrelated to the pagination that need to be added to the route
* @param retainScrollPosition - Scroll to the pagination component after updating the route instead of the top of the page * @param retainScrollPosition - Scroll to the pagination component after updating the route instead of the top of the page
* @param navigationExtras - Extra parameters to pass on to `router.navigate`. Can be used to override values set by this service.
*/ */
updateRoute(paginationId: string, params: { updateRoute(
paginationId: string,
params: {
page?: number page?: number
pageSize?: number pageSize?: number
sortField?: string sortField?: string
sortDirection?: SortDirection sortDirection?: SortDirection
}, extraParams?, retainScrollPosition?: boolean) { },
extraParams?,
retainScrollPosition?: boolean,
navigationExtras?: NavigationExtras,
) {
this.updateRouteWithUrl(paginationId, [], params, extraParams, retainScrollPosition); this.updateRouteWithUrl(paginationId, [], params, extraParams, retainScrollPosition, navigationExtras);
} }
/** /**
@@ -132,13 +139,21 @@ export class PaginationService {
* @param params - The page related params to update in the route * @param params - The page related params to update in the route
* @param extraParams - Addition params unrelated to the pagination that need to be added to the route * @param extraParams - Addition params unrelated to the pagination that need to be added to the route
* @param retainScrollPosition - Scroll to the pagination component after updating the route instead of the top of the page * @param retainScrollPosition - Scroll to the pagination component after updating the route instead of the top of the page
* @param navigationExtras - Extra parameters to pass on to `router.navigate`. Can be used to override values set by this service.
*/ */
updateRouteWithUrl(paginationId: string, url: string[], params: { updateRouteWithUrl(
paginationId: string,
url: string[],
params: {
page?: number page?: number
pageSize?: number pageSize?: number
sortField?: string sortField?: string
sortDirection?: SortDirection sortDirection?: SortDirection
}, extraParams?, retainScrollPosition?: boolean) { },
extraParams?,
retainScrollPosition?: boolean,
navigationExtras?: NavigationExtras,
) {
this.getCurrentRouting(paginationId).subscribe((currentFindListOptions) => { this.getCurrentRouting(paginationId).subscribe((currentFindListOptions) => {
const currentParametersWithIdName = this.getParametersWithIdName(paginationId, currentFindListOptions); const currentParametersWithIdName = this.getParametersWithIdName(paginationId, currentFindListOptions);
const parametersWithIdName = this.getParametersWithIdName(paginationId, params); const parametersWithIdName = this.getParametersWithIdName(paginationId, params);
@@ -149,12 +164,14 @@ export class PaginationService {
this.router.navigate(url, { this.router.navigate(url, {
queryParams: queryParams, queryParams: queryParams,
queryParamsHandling: 'merge', queryParamsHandling: 'merge',
fragment: `p-${paginationId}` fragment: `p-${paginationId}`,
...navigationExtras,
}); });
} else { } else {
this.router.navigate(url, { this.router.navigate(url, {
queryParams: queryParams, queryParams: queryParams,
queryParamsHandling: 'merge' queryParamsHandling: 'merge',
...navigationExtras,
}); });
} }
this.clearParams = {}; this.clearParams = {};