Skip to content

Commit 78ac56e

Browse files
Andrea BarbassoMattia Vianelli
authored andcommitted
[DSC-2624] prioritize query param for search components
(cherry picked from commit b46fc55c13a7d7bd4cd0bcfcd4f95247a530d033)
1 parent f078634 commit 78ac56e

2 files changed

Lines changed: 37 additions & 24 deletions

File tree

src/app/shared/search/search.component.spec.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -327,18 +327,6 @@ describe('SearchComponent', () => {
327327
expect(routeServiceStub.setParameter).toHaveBeenCalledWith('fixedFilterQuery', 'test-fixed-filter-query');
328328
});
329329

330-
it('should still set "configuration" and "fixedFilterQuery" parameters if they are undefined', () => {
331-
spyOn(routeServiceStub, 'setParameter');
332-
comp.configuration = undefined;
333-
comp.fixedFilterQuery = undefined;
334-
335-
fixture.detectChanges();
336-
337-
expect(routeServiceStub.setParameter).toHaveBeenCalledWith('configuration', undefined);
338-
expect(routeServiceStub.setParameter).toHaveBeenCalledWith('fixedFilterQuery', undefined);
339-
});
340-
341-
342330
it('should init search parameters properly and call retrieveSearchResults', fakeAsync(() => {
343331
spyOn((comp as any), 'retrieveSearchResults').and.callThrough();
344332
fixture.detectChanges();

src/app/shared/search/search.component.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import {
1414
Output,
1515
PLATFORM_ID,
1616
} from '@angular/core';
17+
import { NavigationStart, Router } from '@angular/router';
18+
19+
import { BehaviorSubject, combineLatest, Observable, Subscription, of } from 'rxjs';
20+
import { debounceTime, distinctUntilChanged, filter, map, switchMap, tap } from 'rxjs/operators';
1721
import {
1822
NavigationStart,
1923
Router,
@@ -497,8 +501,12 @@ export class SearchComponent implements OnDestroy, OnInit {
497501

498502
this.searchConfigService.setPaginationId(this.paginationId);
499503

500-
this.routeService.setParameter('configuration', this.configuration);
501-
this.routeService.setParameter('fixedFilterQuery', this.fixedFilterQuery);
504+
if (hasValue(this.configuration)) {
505+
this.routeService.setParameter('configuration', this.configuration);
506+
}
507+
if (hasValue(this.fixedFilterQuery)) {
508+
this.routeService.setParameter('fixedFilterQuery', this.fixedFilterQuery);
509+
}
502510

503511
this.currentScope$ = this.routeService.getQueryParameterValue('scope').pipe(
504512
map((routeValue: string) => hasValue(routeValue) ? routeValue : this.scope ?? ''),
@@ -508,9 +516,27 @@ export class SearchComponent implements OnDestroy, OnInit {
508516
this.searchLink = this.getSearchLink();
509517
this.currentContext$.next(this.context);
510518

519+
// Raw query param observable for configuration
520+
const configurationParam$: Observable<string> =
521+
this.routeService.getQueryParameterValue('configuration').pipe(distinctUntilChanged());
522+
523+
511524
// Determinate PaginatedSearchOptions and listen to any update on it
512-
const configuration$: Observable<string> = this.searchConfigService
525+
const configurationFromService$: Observable<string> = this.searchConfigService
513526
.getCurrentConfiguration(this.configuration).pipe(distinctUntilChanged());
527+
528+
// Effective configuration: param value OR input @Input() configuration OR 'default'
529+
const configuration$: Observable<string> = combineLatest([
530+
configurationParam$,
531+
configurationFromService$,
532+
of(this.configuration)
533+
]).pipe(
534+
map(([paramValue, serviceValue, inputValue]) =>
535+
hasValue(paramValue) ? paramValue : (hasValue(inputValue) ? inputValue : hasValue(serviceValue) ? serviceValue : 'default')
536+
),
537+
distinctUntilChanged()
538+
);
539+
514540
const searchSortOptions$: Observable<SortOptions[]> = combineLatest([configuration$, this.currentScope$]).pipe(
515541
switchMap(([configuration, scope]: [string, string]) => this.searchConfigService.getConfigurationSearchConfig(configuration, scope)),
516542
map((searchConfig: SearchConfig) => this.searchConfigService.getConfigurationSortOptions(searchConfig)),
@@ -531,15 +557,14 @@ export class SearchComponent implements OnDestroy, OnInit {
531557
return searchOptions.pagination.id === this.paginationId;
532558
}),
533559
debounceTime(100),
534-
).subscribe(([configuration, searchSortOptions, searchOptions, sortOption, scope]: [string, SortOptions[], PaginatedSearchOptions, SortOptions, string]) => {
535-
// Build the PaginatedSearchOptions object
536-
const searchOptionsConfiguration = searchOptions.configuration || configuration;
537-
const combinedOptions = Object.assign({}, searchOptions,
538-
{
539-
configuration: searchOptionsConfiguration,
540-
sort: sortOption || searchOptions.sort,
541-
forcedEmbeddedKeys: this.forcedEmbeddedKeys.get(searchOptionsConfiguration) || this.forcedEmbeddedKeys.get('default'),
542-
});
560+
).subscribe(([configuration, searchSortOptions, searchOptions, sortOption, scope]:
561+
[string, SortOptions[], PaginatedSearchOptions, SortOptions, string]) => {
562+
// Always apply the freshly resolved configuration (do NOT keep stale one)
563+
const combinedOptions = Object.assign({}, searchOptions, {
564+
configuration,
565+
sort: sortOption || searchOptions.sort,
566+
forcedEmbeddedKeys: this.forcedEmbeddedKeys.get(configuration) || this.forcedEmbeddedKeys.get('default'),
567+
});
543568
if (combinedOptions.query === '') {
544569
combinedOptions.query = this.query;
545570
}

0 commit comments

Comments
 (0)