LP1849212: don't send empty courseId arrays to pcrud
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / result / record.component.ts
1 import {Component, OnInit, OnDestroy, Input} from '@angular/core';
2 import {Subscription} from 'rxjs';
3 import {Router} from '@angular/router';
4 import {OrgService} from '@eg/core/org.service';
5 import {IdlObject} from '@eg/core/idl.service';
6 import {CatalogService} from '@eg/share/catalog/catalog.service';
7 import {BibRecordSummary} from '@eg/share/catalog/bib-record.service';
8 import {CatalogSearchContext} from '@eg/share/catalog/search-context';
9 import {CatalogUrlService} from '@eg/share/catalog/catalog-url.service';
10 import {StaffCatalogService} from '../catalog.service';
11 import {BasketService} from '@eg/share/catalog/basket.service';
12 import {CourseService} from '@eg/staff/share/course.service';
13
14 @Component({
15   selector: 'eg-catalog-result-record',
16   templateUrl: 'record.component.html',
17   styleUrls: ['record.component.css']
18 })
19 export class ResultRecordComponent implements OnInit, OnDestroy {
20
21     @Input() index: number;  // 0-index display row
22     @Input() summary: BibRecordSummary;
23
24     // Optional call number (acn) object to highlight
25     // Assumed prefix/suffix are fleshed
26     // Used by call number browse.
27     @Input() callNumber: IdlObject;
28
29     searchContext: CatalogSearchContext;
30     isRecordSelected: boolean;
31     basketSub: Subscription;
32     hasCourse = false;
33     courses: any[] = [];
34
35     constructor(
36         private router: Router,
37         private org: OrgService,
38         private cat: CatalogService,
39         private catUrl: CatalogUrlService,
40         private staffCat: StaffCatalogService,
41         private basket: BasketService,
42         private course: CourseService
43     ) {}
44
45     ngOnInit() {
46         this.searchContext = this.staffCat.searchContext;
47         this.loadCourseInformation(this.summary.id);
48         this.isRecordSelected = this.basket.hasRecordId(this.summary.id);
49
50         // Watch for basket changes caused by other components
51         this.basketSub = this.basket.onChange.subscribe(() => {
52             this.isRecordSelected = this.basket.hasRecordId(this.summary.id);
53         });
54     }
55
56     ngOnDestroy() {
57         this.basketSub.unsubscribe();
58     }
59
60     loadCourseInformation(recordId) {
61         this.course.isOptedIn().then(res => {
62             if (res) {
63                 this.course.fetchCoursesForRecord(recordId).then(course_list => {
64                     if (course_list) {
65                         Object.keys(course_list).forEach(key => {
66                             this.courses.push(course_list[key]);
67                         });
68                         this.hasCourse = true;
69                     }
70                 });
71             }
72         });
73     }
74
75     orgName(orgId: number): string {
76         return this.org.get(orgId).shortname();
77     }
78
79     iconFormatLabel(code: string): string {
80         return this.cat.iconFormatLabel(code);
81     }
82
83     placeHold(): void {
84         let holdType = 'T';
85         let holdTarget = this.summary.id;
86
87         const ts = this.searchContext.termSearch;
88         if (ts.isMetarecordSearch()) {
89             holdType = 'M';
90             holdTarget = this.summary.metabibId;
91         }
92
93         this.router.navigate([`/staff/catalog/hold/${holdType}`],
94             {queryParams: {target: holdTarget}});
95     }
96
97     addToList(): void {
98         alert('Adding to list for bib ' + this.summary.id);
99     }
100
101     // Params to genreate a new author search based on a reset
102     // clone of the current page params.
103     getAuthorSearchParams(summary: BibRecordSummary): any {
104         return this.staffCat.getAuthorSearchParams(summary);
105     }
106
107     // Returns the URL parameters for the current page plus the
108     // "fromMetarecord" param used for linking title links to
109     // MR constituent result records list.
110     appendFromMrParam(summary: BibRecordSummary): any {
111         const tmpContext = this.staffCat.cloneContext(this.searchContext);
112         tmpContext.termSearch.fromMetarecord = summary.metabibId;
113         return this.catUrl.toUrlParams(tmpContext);
114     }
115
116     // Returns true if the selected record summary is a metarecord summary
117     // and it links to more than one constituent bib record.
118     hasMrConstituentRecords(summary: BibRecordSummary): boolean {
119         return (
120             summary.metabibId && summary.metabibRecords.length > 1
121         );
122     }
123
124     currentParams(): any {
125         return this.catUrl.toUrlParams(this.searchContext);
126     }
127
128     toggleBasketEntry() {
129         if (this.isRecordSelected) {
130             return this.basket.addRecordIds([this.summary.id]);
131         } else {
132             return this.basket.removeRecordIds([this.summary.id]);
133         }
134     }
135 }
136
137