54053: Filter-labels uses FilterService instead of SearchService

This commit is contained in:
Kristof De Langhe
2018-07-18 16:20:13 +02:00
parent 98966bd0d0
commit a09c33ea14
4 changed files with 20 additions and 56 deletions

View File

@@ -20,7 +20,8 @@ import { SortDirection, SortOptions } from '../../../core/cache/models/sort-opti
import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model';
import { SearchOptions } from '../../search-options.model';
import { PaginatedSearchOptions } from '../../paginated-search-options.model';
import { ActivatedRoute } from '@angular/router';
import { ActivatedRoute, Params } from '@angular/router';
import { FilterLabel } from '../../search-service/filter-label.model';
const filterStateSelector = (state: SearchFiltersState) => state.searchFilter;
@@ -76,7 +77,7 @@ export class SearchFilterService {
);
}
getCurrentFilters(): Observable<any> {
getCurrentFilters(): Observable<Params> {
return this.routeService.getQueryParamsWithPrefix('f.').map((filterParams) => {
if (isNotEmpty(filterParams)) {
const params = {};
@@ -98,6 +99,20 @@ export class SearchFilterService {
});
}
getCurrentFilterLabels(): Observable<FilterLabel[]> {
return this.getCurrentFilters().pipe(
map((params: Params) => {
const filterLabels: FilterLabel[] = [];
Object.keys(params).forEach((key) => {
params[key].forEach((p: string) => {
filterLabels.push(new FilterLabel(p, key));
});
});
return filterLabels;
})
);
}
getCurrentFrontendFilters(): Observable<any> {
return this.routeService.getQueryParamsWithPrefix('f.');
}

View File

@@ -4,6 +4,7 @@ import { Observable } from 'rxjs/Observable';
import { Params } from '@angular/router';
import { FilterLabel } from '../search-service/filter-label.model';
import { map } from 'rxjs/operators';
import { SearchFilterService } from '../search-filters/search-filter/search-filter.service';
@Component({
selector: 'ds-search-labels',
@@ -13,8 +14,8 @@ import { map } from 'rxjs/operators';
export class SearchLabelsComponent {
appliedFilters: Observable<FilterLabel[]>;
constructor(private searchService: SearchService) {
this.appliedFilters = this.searchService.getFilterLabels();
constructor(private searchService: SearchService, private filterService: SearchFilterService) {
this.appliedFilters = this.filterService.getCurrentFilterLabels();
}
getRemoveParams(filterLabel: FilterLabel): Observable<Params> {

View File

@@ -247,36 +247,5 @@ describe('SearchService', () => {
expect((searchService as any).responseCache.get).toHaveBeenCalledWith(requestUrl);
});
});
describe('when getFilterLabels is called', () => {
let obs: Observable<FilterLabel[]>;
const value = 'Test';
const orgField = 'author';
const field = 'f.' + orgField;
const mockConfig = new RemoteData(false, false, true, null, [
{
name: orgField,
type: null,
hasFacets: false,
pageSize: 5,
isOpenByDefault: false,
paramName: field
} as SearchFilterConfig
]);
const mockParams = [];
beforeEach(() => {
spyOn((searchService as any), 'getConfig').and.returnValue(Observable.of(mockConfig));
mockParams[field] = value;
(searchService as any).route.queryParams = Observable.of(mockParams);
obs = searchService.getFilterLabels();
});
it('should return the correct labels', () => {
obs.subscribe((filters) => {
expect(filters[0].value).toEqual(value);
});
});
});
});
});

View File

@@ -262,27 +262,6 @@ export class SearchService implements OnDestroy {
}
getFilterLabels(): Observable<FilterLabel[]> {
return combineLatest(this.getConfig(), this.route.queryParams).pipe(
map(([rd, params]) => {
const filterLabels: FilterLabel[] = [];
rd.payload.forEach((config: SearchFilterConfig) => {
const param = params[config.paramName];
if (param !== undefined) {
if (param instanceof Array && param.length > 1) {
param.forEach((p: string) => {
filterLabels.push(new FilterLabel(p, config.paramName))
});
} else {
filterLabels.push(new FilterLabel(param, config.paramName));
}
}
});
return filterLabels.filter((n) => n !== undefined && n.value.length > 0);
})
);
}
getViewMode(): Observable<ViewMode> {
return this.route.queryParams.map((params) => {
if (isNotEmpty(params.view) && hasValue(params.view)) {