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