LP1896512 Angular retrieve record by ID/TCN
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / bib-by-ident.component.ts
1 import {Component, OnInit, ViewChild, Input, AfterViewInit} from '@angular/core';
2 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {NetService} from '@eg/core/net.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6
7 /* Component for retrieving bib records by ID, TCN */
8
9 @Component({
10   templateUrl: 'bib-by-ident.component.html'
11 })
12 export class BibByIdentComponent implements OnInit, AfterViewInit {
13
14     identType: 'id' | 'tcn' = 'id';
15     identValue: string;
16     notFound = false;
17     multiRecordsFound = false;
18
19     constructor(
20         private router: Router,
21         private route: ActivatedRoute,
22         private net: NetService,
23         private pcrud: PcrudService
24     ) {}
25
26     ngOnInit() {
27         this.route.paramMap.subscribe((params: ParamMap) => {
28             this.identType = params.get('identType') as 'id' | 'tcn';
29         });
30     }
31
32     ngAfterViewInit() {
33         const node = document.getElementById('bib-ident-value');
34         setTimeout(() => node.focus());
35     }
36
37     search() {
38         if (!this.identValue) { return; }
39
40         this.notFound = false;
41         this.multiRecordsFound = false;
42
43         let promise;
44         if (this.identType === 'id') {
45             promise = this.getById();
46
47         } else if (this.identType === 'tcn') {
48             promise = this.getByTcn();
49         }
50
51         promise.then(id => {
52             if (id === null) {
53                 this.notFound = true;
54             } else {
55                 this.goToRecord(id);
56             }
57         });
58     }
59
60     getById(): Promise<number> {
61         // Confirm the record exists before redirecting.
62         return this.pcrud.retrieve('bre', this.identValue).toPromise()
63         .then(rec => rec ? rec.id() : null);
64     }
65
66     getByTcn(): Promise<number> {
67         // Start by searching non-deleted records
68
69         return this.net.request(
70             'open-ils.search',
71             'open-ils.search.biblio.tcn', this.identValue).toPromise()
72         .then(resp => {
73
74             if (resp.count > 0) {
75                 return Promise.resolve(resp);
76             }
77
78             // No active records, see if we have any deleted records.
79             return this.net.request(
80                 'open-ils.search',
81                 'open-ils.search.biblio.tcn', this.identValue, true
82             ).toPromise();
83
84         }).then(resp => {
85
86             if (resp.count) {
87                 if (resp.count > 1) {
88                     this.multiRecordsFound = true;
89                     return null;
90                 } else {
91                     return resp.ids[0];
92                 }
93             }
94
95             return null;
96         });
97     }
98
99     goToRecord(id: number) {
100         this.router.navigate([`/staff/catalog/record/${id}`]);
101     }
102 }
103
104