From c3bd151cc3f9d01802db1d6c526af327fbff6ab0 Mon Sep 17 00:00:00 2001 From: lotte Date: Wed, 22 May 2019 16:55:49 +0200 Subject: [PATCH] Fixed issue with retrieving request --- .../filtered-search-page.component.spec.ts | 16 ---------- .../filtered-search-page.component.ts | 30 +++++++++++++++---- .../search-fixed-filter.service.spec.ts | 3 +- .../search-fixed-filter.service.ts | 15 +++++----- .../search-configuration.service.spec.ts | 16 ---------- .../search-configuration.service.ts | 20 ++----------- ...arch-result-list-element.component.spec.ts | 1 - src/app/shared/services/route.service.spec.ts | 1 + src/app/shared/services/route.service.ts | 6 ++-- 9 files changed, 41 insertions(+), 67 deletions(-) diff --git a/src/app/+search-page/filtered-search-page.component.spec.ts b/src/app/+search-page/filtered-search-page.component.spec.ts index 5c49767ed2..59ab9d7b0d 100644 --- a/src/app/+search-page/filtered-search-page.component.spec.ts +++ b/src/app/+search-page/filtered-search-page.component.spec.ts @@ -18,20 +18,4 @@ describe('FilteredSearchPageComponent', () => { searchConfigService = (comp as any).searchConfigService; fixture.detectChanges(); }); - - describe('when fixedFilterQuery is defined', () => { - const fixedFilterQuery = 'fixedFilterQuery'; - - beforeEach(() => { - spyOn(searchConfigService, 'updateFixedFilter').and.callThrough(); - comp.fixedFilterQuery = fixedFilterQuery; - comp.ngOnInit(); - fixture.detectChanges(); - }); - - it('should update the paginated search options', () => { - expect(searchConfigService.updateFixedFilter).toHaveBeenCalledWith(fixedFilterQuery); - }); - }); - }); diff --git a/src/app/+search-page/filtered-search-page.component.ts b/src/app/+search-page/filtered-search-page.component.ts index df37d3f441..5e02b37215 100644 --- a/src/app/+search-page/filtered-search-page.component.ts +++ b/src/app/+search-page/filtered-search-page.component.ts @@ -2,20 +2,24 @@ import { HostWindowService } from '../shared/host-window.service'; import { SearchService } from './search-service/search.service'; import { SearchSidebarService } from './search-sidebar/search-sidebar.service'; import { SearchPageComponent } from './search-page.component'; -import { ChangeDetectionStrategy, Component, Inject, Input } from '@angular/core'; +import { ChangeDetectionStrategy, Component, Inject, Input, OnInit } from '@angular/core'; import { pushInOut } from '../shared/animations/push'; import { RouteService } from '../shared/services/route.service'; import { SearchConfigurationService } from './search-service/search-configuration.service'; import { Observable } from 'rxjs'; import { PaginatedSearchOptions } from './paginated-search-options.model'; import { SEARCH_CONFIG_SERVICE } from '../+my-dspace-page/my-dspace-page.component'; +import { map, switchMap } from 'rxjs/operators'; +import { getSucceededRemoteData } from '../core/shared/operators'; +import { isNotEmpty } from '../shared/empty.util'; /** * This component renders a simple item page. * The route parameter 'id' is used to request the item it represents. * All fields of the item that should be displayed, are defined in its template. */ -@Component({selector: 'ds-filtered-search-page', +@Component({ + selector: 'ds-filtered-search-page', styleUrls: ['./search-page.component.scss'], templateUrl: './search-page.component.html', changeDetection: ChangeDetectionStrategy.OnPush, @@ -28,7 +32,7 @@ import { SEARCH_CONFIG_SERVICE } from '../+my-dspace-page/my-dspace-page.compone ] }) -export class FilteredSearchPageComponent extends SearchPageComponent { +export class FilteredSearchPageComponent extends SearchPageComponent implements OnInit { /** * The actual query for the fixed filter. @@ -44,6 +48,18 @@ export class FilteredSearchPageComponent extends SearchPageComponent { super(service, sidebarService, windowService, searchConfigService, routeService); } + /** + * Listening to changes in the paginated search options + * If something changes, update the search results + * + * Listen to changes in the scope + * If something changes, update the list of scopes for the dropdown + */ + ngOnInit(): void { + super.ngOnInit(); + } + + /** * Get the current paginated search options after updating the fixed filter using the fixedFilterQuery input * This is to make sure the fixed filter is included in the paginated search options, as it is not part of any @@ -51,7 +67,11 @@ export class FilteredSearchPageComponent extends SearchPageComponent { * @returns {Observable} */ protected getSearchOptions(): Observable { - this.searchConfigService.updateFixedFilter(this.fixedFilterQuery); - return this.searchConfigService.paginatedSearchOptions; + return this.searchConfigService.paginatedSearchOptions.pipe( + map((options: PaginatedSearchOptions) => { + const filter = this.fixedFilterQuery || options.fixedFilter; + return Object.assign(options, { fixedFilter: filter }); + }) + ); } } diff --git a/src/app/+search-page/search-filters/search-filter/search-fixed-filter.service.spec.ts b/src/app/+search-page/search-filters/search-filter/search-fixed-filter.service.spec.ts index 3207345564..3f6c2ef133 100644 --- a/src/app/+search-page/search-filters/search-filter/search-fixed-filter.service.spec.ts +++ b/src/app/+search-page/search-filters/search-filter/search-fixed-filter.service.spec.ts @@ -17,7 +17,7 @@ describe('SearchFixedFilterService', () => { configure: () => {}, /* tslint:enable:no-empty */ generateRequestId: () => 'fake-id', - getByUUID: () => observableOf(Object.assign(new RequestEntry(), { + getByHref: () => observableOf(Object.assign(new RequestEntry(), { response: new FilteredDiscoveryQueryResponse(filterQuery, 200, 'OK') })) }) as RequestService; @@ -56,5 +56,4 @@ describe('SearchFixedFilterService', () => { expect(query).toContain(itemUUID); }); }); - }); diff --git a/src/app/+search-page/search-filters/search-filter/search-fixed-filter.service.ts b/src/app/+search-page/search-filters/search-filter/search-fixed-filter.service.ts index 7d59e5a446..0f17b508c9 100644 --- a/src/app/+search-page/search-filters/search-filter/search-fixed-filter.service.ts +++ b/src/app/+search-page/search-filters/search-filter/search-fixed-filter.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; -import { flatMap, map } from 'rxjs/operators'; -import { Observable , of as observableOf } from 'rxjs'; +import { flatMap, map, switchMap, tap } from 'rxjs/operators'; +import { Observable, of as observableOf } from 'rxjs'; import { HALEndpointService } from '../../../core/shared/hal-endpoint.service'; import { GetRequest, RestRequest } from '../../../core/data/request.models'; import { RequestService } from '../../../core/data/request.service'; @@ -33,7 +33,7 @@ export class SearchFixedFilterService { getQueryByFilterName(filterName: string): Observable { if (hasValue(filterName)) { const requestUuid = this.requestService.generateRequestId(); - this.halService.getEndpoint(this.queryByFilterPath).pipe( + const requestObs = this.halService.getEndpoint(this.queryByFilterPath).pipe( map((url: string) => { url += ('/' + filterName); const request = new GetRequest(requestUuid, url); @@ -44,10 +44,12 @@ export class SearchFixedFilterService { }); }), configureRequest(this.requestService) - ).subscribe(); + ); - // get search results from response cache - const filterQuery: Observable = this.requestService.getByUUID(requestUuid).pipe( + const requestEntryObs = requestObs.pipe( + switchMap((request: RestRequest) => this.requestService.getByHref(request.href)), + ); + const filterQuery = requestEntryObs.pipe( getResponseFromEntry(), map((response: FilteredDiscoveryQueryResponse) => response.filterQuery @@ -75,5 +77,4 @@ export class SearchFixedFilterService { getFilterByRelation(relationType: string, itemUUID: string): string { return `f.${relationType}=${itemUUID}`; } - } diff --git a/src/app/+search-page/search-service/search-configuration.service.spec.ts b/src/app/+search-page/search-service/search-configuration.service.spec.ts index 79932805c1..fb95ab8d04 100644 --- a/src/app/+search-page/search-service/search-configuration.service.spec.ts +++ b/src/app/+search-page/search-service/search-configuration.service.spec.ts @@ -171,20 +171,4 @@ describe('SearchConfigurationService', () => { expect((service as any).routeService.getRouteParameterValue).toHaveBeenCalledWith('filter'); }); }); - - describe('when updateFixedFilter is called', () => { - const filter = 'filter'; - - beforeEach(() => { - service.updateFixedFilter(filter); - }); - - it('should update the paginated search options with the correct fixed filter', () => { - expect(service.paginatedSearchOptions.getValue().fixedFilter).toEqual(filter); - }); - - it('should update the search options with the correct fixed filter', () => { - expect(service.searchOptions.getValue().fixedFilter).toEqual(filter); - }); - }); }); diff --git a/src/app/+search-page/search-service/search-configuration.service.ts b/src/app/+search-page/search-service/search-configuration.service.ts index 75caf93d57..41bcea66d6 100644 --- a/src/app/+search-page/search-service/search-configuration.service.ts +++ b/src/app/+search-page/search-service/search-configuration.service.ts @@ -9,7 +9,7 @@ import { of as observableOf, Subscription } from 'rxjs'; -import { filter, flatMap, map, tap } from 'rxjs/operators'; +import { filter, flatMap, map, switchMap, tap } from 'rxjs/operators'; import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model'; import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model'; import { SearchOptions } from '../search-options.model'; @@ -204,7 +204,7 @@ export class SearchConfigurationService implements OnDestroy { */ getCurrentFixedFilter(): Observable { return this.routeService.getRouteParameterValue('filter').pipe( - flatMap((f) => this.fixedFilterService.getQueryByFilterName(f)) + switchMap((f) => this.fixedFilterService.getQueryByFilterName(f)) ); } @@ -229,6 +229,7 @@ export class SearchConfigurationService implements OnDestroy { this.getFiltersPart(), this.getFixedFilterPart() ).subscribe((update) => { + console.log(update); const currentValue: SearchOptions = this.searchOptions.getValue(); const updatedValue: SearchOptions = Object.assign(currentValue, update); this.searchOptions.next(updatedValue); @@ -356,21 +357,6 @@ export class SearchConfigurationService implements OnDestroy { map((fixedFilter) => { return { fixedFilter } }), - tap(t => console.log(t)) ); } - - /** - * Update the fixed filter in paginated and non-paginated search options with a given value - * @param {string} fixedFilter - */ - public updateFixedFilter(fixedFilter: string) { - const currentPaginatedValue: PaginatedSearchOptions = this.paginatedSearchOptions.getValue(); - const updatedPaginatedValue: PaginatedSearchOptions = Object.assign(currentPaginatedValue, { fixedFilter: fixedFilter }); - this.paginatedSearchOptions.next(updatedPaginatedValue); - - const currentValue: SearchOptions = this.searchOptions.getValue(); - const updatedValue: SearchOptions = Object.assign(currentValue, { fixedFilter: fixedFilter }); - this.searchOptions.next(updatedValue); - } } diff --git a/src/app/shared/object-list/search-result-list-element/item-search-result/item-search-result-list-element.component.spec.ts b/src/app/shared/object-list/search-result-list-element/item-search-result/item-search-result-list-element.component.spec.ts index ef4660fdd9..a370d3a632 100644 --- a/src/app/shared/object-list/search-result-list-element/item-search-result/item-search-result-list-element.component.spec.ts +++ b/src/app/shared/object-list/search-result-list-element/item-search-result/item-search-result-list-element.component.spec.ts @@ -75,7 +75,6 @@ describe('ItemSearchResultListElementComponent', () => { it('should show the relationship type badge', () => { const badge = fixture.debugElement.query(By.css('span.badge')); - console.log(itemSearchResultListElementComponent.dso); expect(badge.nativeElement.textContent).toContain(type.toLowerCase()); }); }); diff --git a/src/app/shared/services/route.service.spec.ts b/src/app/shared/services/route.service.spec.ts index c9b3710ee6..c6003521a7 100644 --- a/src/app/shared/services/route.service.spec.ts +++ b/src/app/shared/services/route.service.spec.ts @@ -42,6 +42,7 @@ describe('RouteService', () => { provide: ActivatedRoute, useValue: { queryParams: observableOf(paramObject), + params: observableOf(paramObject), queryParamMap: observableOf(convertToParamMap(paramObject)) }, }, diff --git a/src/app/shared/services/route.service.ts b/src/app/shared/services/route.service.ts index d6f8dabee9..00d3e80c07 100644 --- a/src/app/shared/services/route.service.ts +++ b/src/app/shared/services/route.service.ts @@ -96,7 +96,9 @@ export class RouteService { } getRouteParameterValue(paramName: string): Observable { - return this.store.pipe(select(routeParameterSelector(paramName)), tap((t) => console.log(paramName, t))); + const test = this.store.pipe(select(routeParameterSelector(paramName))); + test.subscribe((t) => {console.log('test', t)}); + return test; } getRouteDataValue(datafield: string): Observable { @@ -139,7 +141,6 @@ export class RouteService { combineLatest(this.router.events, this.getRouteParams(), this.route.queryParams) .pipe(filter(([event, params, queryParams]) => event instanceof NavigationEnd)) .subscribe(([event, params, queryParams]: [NavigationEnd, Params, Params]) => { - console.log(params); this.store.dispatch(new SetParametersAction(params)); this.store.dispatch(new SetQueryParametersAction(queryParams)); this.store.dispatch(new AddUrlToHistoryAction(event.urlAfterRedirects)); @@ -148,7 +149,6 @@ export class RouteService { private getRouteParams(): Observable { let active = this.route; - console.log(active); while (active.firstChild) { active = active.firstChild; }