]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holds/detail.component.ts
LP1959048: manual ng lint fixes
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holds / detail.component.ts
1 import {Component, OnInit, Input, Output, ViewChild, EventEmitter} from '@angular/core';
2 import {tap} from 'rxjs/operators';
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 import {OrgService} from '@eg/core/org.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {HoldNoteDialogComponent} from './note-dialog.component';
9 import {HoldNotifyDialogComponent} from './notify-dialog.component';
10
11 /** Hold details read-only view */
12
13 @Component({
14   selector: 'eg-hold-detail',
15   templateUrl: 'detail.component.html'
16 })
17 export class HoldDetailComponent implements OnInit {
18     detailTab = 'notes';
19     notes: IdlObject[] = [];
20     notifies: IdlObject[] = [];
21
22     private _holdId: number;
23     @Input() set holdId(id: number) {
24         if (this._holdId !== id) {
25             this._holdId = id;
26             if (this.initDone) {
27                 this.fetchHold();
28             }
29         }
30     }
31
32     get holdId(): number {
33         return this._holdId;
34     }
35
36     hold: any; // wide hold reference
37     @Input() set wideHold(wh: any) {
38         this.hold = wh;
39     }
40
41     get wideHold(): any {
42         return this.hold;
43     }
44
45     // Display bib record summary along the top of the detail page.
46     @Input() showRecordSummary = false;
47
48     initDone: boolean;
49     @Output() onShowList: EventEmitter<any>;
50
51     @ViewChild('noteDialog') noteDialog: HoldNoteDialogComponent;
52     @ViewChild('notifyDialog') notifyDialog: HoldNotifyDialogComponent;
53
54     constructor(
55         private net: NetService,
56         private pcrud: PcrudService,
57         private org: OrgService,
58         private auth: AuthService,
59     ) {
60         this.onShowList = new EventEmitter<any>();
61     }
62
63     ngOnInit() {
64         this.initDone = true;
65         this.fetchHold();
66     }
67
68     fetchHold() {
69         if (!this.holdId && !this.hold) { return; }
70
71         const promise = this.hold ? Promise.resolve(this.hold) :
72             this.net.request(
73                 'open-ils.circ',
74                 'open-ils.circ.hold.wide_hash.stream',
75                 this.auth.token(), {id: this.holdId}
76             ).toPromise();
77
78         return promise.then(wideHold => {
79             this.hold = wideHold;
80             // avoid this.holdId = since it re-fires this fetch.
81             this._holdId = wideHold.id;
82         })
83         .then(_ => this.getNotes())
84         .then(_ => this.getNotifies());
85     }
86
87     getNotes(): Promise<any> {
88         this.notes = [];
89         return this.pcrud.search('ahrn', {hold: this.holdId})
90         .pipe(tap(note => this.notes.push(note))).toPromise();
91     }
92
93     getNotifies(): Promise<any> {
94         this.notifies = [];
95
96         return this.pcrud.search('ahn', {hold: this.holdId}, {
97             flesh: 1,
98             flesh_fields: {ahn: ['notify_staff']},
99             order_by: {ahn: 'notify_time DESC'}
100         }).pipe(tap(notify => this.notifies.push(notify))).toPromise();
101     }
102
103     getOrgName(id: number) {
104         if (id) {
105             return this.org.get(id).shortname();
106         }
107     }
108
109     showListView() {
110         this.onShowList.emit();
111     }
112
113     deleteNote(note: IdlObject) {
114         this.pcrud.remove(note).toPromise()
115         .then(ok => { if (ok) { this.getNotes(); } });
116     }
117
118     newNote() {
119         this.noteDialog.open().subscribe(note => this.notes.unshift(note));
120     }
121
122     newNotify() {
123         this.notifyDialog.open().subscribe(notify => this.getNotifies()); // fleshing
124     }
125 }
126
127