1 import {Component, OnInit, Input} from '@angular/core';
2 import {Observable} from 'rxjs/Observable';
3 import {map, switchMap, distinctUntilChanged} from 'rxjs/operators';
4 import {ActivatedRoute, ParamMap} from '@angular/router';
5 import {CatalogService} from '@eg/share/catalog/catalog.service';
6 import {BibRecordService} from '@eg/share/catalog/bib-record.service';
7 import {CatalogUrlService} from '@eg/share/catalog/catalog-url.service';
8 import {CatalogSearchContext, CatalogSearchState} from '@eg/share/catalog/search-context';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {StaffCatalogService} from '../catalog.service';
11 import {IdlObject} from '@eg/core/idl.service';
14 selector: 'eg-catalog-results',
15 templateUrl: 'results.component.html'
17 export class ResultsComponent implements OnInit {
19 searchContext: CatalogSearchContext;
21 // Cache record creator/editor since this will likely be a
22 // reasonably small set of data w/ lots of repitition.
23 userCache: {[id: number]: IdlObject} = {};
26 private route: ActivatedRoute,
27 private pcrud: PcrudService,
28 private cat: CatalogService,
29 private bib: BibRecordService,
30 private catUrl: CatalogUrlService,
31 private staffCat: StaffCatalogService
35 this.searchContext = this.staffCat.searchContext;
37 // Our search context is initialized on page load. Once
38 // ResultsComponent is active, it will not be reinitialized,
39 // even if the route parameters changes (unless we change the
40 // route reuse policy). Watch for changes here to pick up new
43 // This will also fire on page load.
44 this.route.queryParamMap.subscribe((params: ParamMap) => {
46 // TODO: Angular docs suggest using switchMap(), but
47 // it's not firing for some reason. Also, could avoid
48 // firing unnecessary searches when a param unrelated to
49 // searching is changed by .map()'ing out only the desired
50 // params and running through .distinctUntilChanged(), but
51 // .map() is not firing either. I'm missing something.
52 this.searchByUrl(params);
56 searchByUrl(params: ParamMap): void {
57 this.catUrl.applyUrlParams(this.searchContext, params);
59 if (this.searchContext.isSearchable()) {
61 this.cat.search(this.searchContext)
63 this.cat.fetchFacets(this.searchContext);
64 this.cat.fetchBibSummaries(this.searchContext)
65 .then(ok2 => this.fleshSearchResults());
70 fleshSearchResults(): void {
71 const records = this.searchContext.result.records;
72 if (!records || records.length === 0) { return; }
74 // Flesh the creator / editor fields with the user object.
75 this.bib.fleshBibUsers(records.map(r => r.record));
78 searchIsDone(): boolean {
79 return this.searchContext.searchState === CatalogSearchState.COMPLETE;