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';
12 /** Hold details read-only view */
15 selector: 'eg-hold-detail',
16 templateUrl: 'detail.component.html'
18 export class HoldDetailComponent implements OnInit {
20 notes: IdlObject[] = [];
21 notifies: IdlObject[] = [];
23 private _holdId: number;
24 @Input() set holdId(id: number) {
25 if (this._holdId !== id) {
33 get holdId(): number {
37 hold: any; // wide hold reference
38 @Input() set wideHold(wh: any) {
47 @Output() onShowList: EventEmitter<any>;
49 @ViewChild('noteDialog') noteDialog: HoldNoteDialogComponent;
50 @ViewChild('notifyDialog') notifyDialog: HoldNotifyDialogComponent;
53 private net: NetService,
54 private pcrud: PcrudService,
55 private org: OrgService,
56 private auth: AuthService,
58 this.onShowList = new EventEmitter<any>();
67 if (!this.holdId && !this.hold) { return; }
69 const promise = this.hold ? Promise.resolve(this.hold) :
72 'open-ils.circ.hold.wide_hash.stream',
73 this.auth.token(), {id: this.holdId}
76 return promise.then(wideHold => {
78 // avoid this.holdId = since it re-fires this fetch.
79 this._holdId = wideHold.id;
81 .then(_ => this.getNotes())
82 .then(_ => this.getNotifies());
85 getNotes(): Promise<any> {
87 return this.pcrud.search('ahrn', {hold: this.holdId})
88 .pipe(tap(note => this.notes.push(note))).toPromise();
91 getNotifies(): Promise<any> {
94 return this.pcrud.search('ahn', {hold: this.holdId}, {
96 flesh_fields: {ahn: ['notify_staff']},
97 order_by: {ahn: 'notify_time DESC'}
98 }).pipe(tap(notify => this.notifies.push(notify))).toPromise();
101 getOrgName(id: number) {
103 return this.org.get(id).shortname();
108 this.onShowList.emit();
111 deleteNote(note: IdlObject) {
112 this.pcrud.remove(note).toPromise()
113 .then(ok => { if (ok) { this.getNotes(); } });
117 this.noteDialog.open().subscribe(note => this.notes.unshift(note));
121 this.notifyDialog.open().subscribe(notify => this.getNotifies()); // fleshing