Skip to content

Commit 6ecb330

Browse files
[DSC-1917] Port of [CST-16472] Reset ParamMap in the absence of any parameters
1 parent 6d8419d commit 6ecb330

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/app/core/services/route.service.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { RouteService } from './route.service';
99
import { RouterMock } from '../../shared/mocks/router.mock';
1010
import { TestScheduler } from 'rxjs/testing';
1111
import { AddUrlToHistoryAction } from '../history/history.actions';
12+
import {NativeWindowRef, NativeWindowService} from './window.service';
1213

1314
describe('RouteService', () => {
1415
let scheduler: TestScheduler;
@@ -48,12 +49,13 @@ describe('RouteService', () => {
4849
},
4950
{ provide: Router, useValue: router },
5051
{ provide: Store, useValue: store },
52+
{ provide: NativeWindowService, useClass: new NativeWindowRef() },
5153
]
5254
});
5355
}));
5456

5557
beforeEach(() => {
56-
service = new RouteService(TestBed.inject(ActivatedRoute), TestBed.inject(Router), TestBed.inject(Store));
58+
service = new RouteService(TestBed.inject(ActivatedRoute), TestBed.inject(Router), TestBed.inject(Store), TestBed.inject(NativeWindowRef));
5759
serviceAsAny = service;
5860
});
5961

src/app/core/services/route.service.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { distinctUntilChanged, filter, map, take } from 'rxjs/operators';
2-
import { Injectable } from '@angular/core';
3-
import { ActivatedRoute, NavigationEnd, Params, Router, RouterStateSnapshot, } from '@angular/router';
2+
import {Inject, Injectable} from '@angular/core';
3+
import {ActivatedRoute, NavigationEnd, ParamMap, Params, Router, RouterStateSnapshot,} from '@angular/router';
44

55
import { combineLatest, Observable } from 'rxjs';
66
import { createSelector, MemoizedSelector, select, Store } from '@ngrx/store';
@@ -12,6 +12,7 @@ import { hasValue } from '../../shared/empty.util';
1212
import { historySelector } from '../history/selectors';
1313
import { AddUrlToHistoryAction } from '../history/history.actions';
1414
import { CoreState } from '../core-state.model';
15+
import {NativeWindowRef, NativeWindowService} from './window.service';
1516

1617
/**
1718
* Selector to select all route parameters from the store
@@ -63,7 +64,7 @@ export function parameterSelector(key: string, paramsSelector: (state: CoreState
6364
providedIn: 'root'
6465
})
6566
export class RouteService {
66-
constructor(private route: ActivatedRoute, private router: Router, private store: Store<CoreState>) {
67+
constructor(private route: ActivatedRoute, private router: Router, private store: Store<CoreState>, @Inject(NativeWindowService) private _window: NativeWindowRef) {
6768
this.saveRouting();
6869
}
6970

@@ -141,9 +142,14 @@ export class RouteService {
141142

142143
public getQueryParamMap(): Observable<any> {
143144
return this.route.queryParamMap.pipe(
144-
map((paramMap) => {
145+
map((paramMap: ParamMap) => {
145146
const snapshot: RouterStateSnapshot = this.router.routerState.snapshot;
146147
// Due to an Angular bug, sometimes change of QueryParam is not detected so double checks with route snapshot
148+
if (!(this._window.nativeWindow.location.href || '').includes('?')){
149+
// Prevent sending outdated query params
150+
const emptyParamMap: ParamMap = { keys: [], has: (name: string) => false, get: (name: string) => null, getAll: (name: string) => [] };
151+
return emptyParamMap;
152+
}
147153
if (!isEqual(paramMap, snapshot.root.queryParamMap)) {
148154
return snapshot.root.queryParamMap;
149155
} else {

0 commit comments

Comments
 (0)