LP#1801984 Upgrading Angular 6 to Angular 7
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / result / results.component.ts
1 import {Component, OnInit, Input} from '@angular/core';
2 import {Observable} from 'rxjs';
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';
12
13 @Component({
14   selector: 'eg-catalog-results',
15   templateUrl: 'results.component.html'
16 })
17 export class ResultsComponent implements OnInit {
18
19     searchContext: CatalogSearchContext;
20
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} = {};
24
25     constructor(
26         private route: ActivatedRoute,
27         private pcrud: PcrudService,
28         private cat: CatalogService,
29         private bib: BibRecordService,
30         private catUrl: CatalogUrlService,
31         private staffCat: StaffCatalogService
32     ) {}
33
34     ngOnInit() {
35         this.searchContext = this.staffCat.searchContext;
36
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
41         // searches.
42         //
43         // This will also fire on page load.
44         this.route.queryParamMap.subscribe((params: ParamMap) => {
45
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);
53         });
54     }
55
56     searchByUrl(params: ParamMap): void {
57         this.catUrl.applyUrlParams(this.searchContext, params);
58
59         if (this.searchContext.isSearchable()) {
60
61             this.cat.search(this.searchContext)
62             .then(ok => {
63                 this.cat.fetchFacets(this.searchContext);
64                 this.cat.fetchBibSummaries(this.searchContext)
65                 .then(ok2 => this.fleshSearchResults());
66             });
67         }
68     }
69
70     fleshSearchResults(): void {
71         const records = this.searchContext.result.records;
72         if (!records || records.length === 0) { return; }
73
74         // Flesh the creator / editor fields with the user object.
75         this.bib.fleshBibUsers(records.map(r => r.record));
76     }
77
78     searchIsDone(): boolean {
79         return this.searchContext.searchState === CatalogSearchState.COMPLETE;
80     }
81
82 }
83
84