]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/mark-missing-dialog.component.ts
LP2045292 Color contrast for AngularJS patron bills
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / mark-missing-dialog.component.ts
1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {Observable, throwError} from 'rxjs';
3 import {NetService} from '@eg/core/net.service';
4 import {EventService} from '@eg/core/event.service';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {DialogComponent} from '@eg/share/dialog/dialog.component';
8 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
9 import {StringComponent} from '@eg/share/string/string.component';
10
11
12 /**
13  * Dialog for marking items missing.
14  */
15
16 @Component({
17   selector: 'eg-mark-missing-dialog',
18   templateUrl: 'mark-missing-dialog.component.html'
19 })
20
21 export class MarkMissingDialogComponent
22     extends DialogComponent implements OnInit {
23
24     @Input() copyIds: number[];
25
26     numSucceeded: number;
27     numFailed: number;
28
29     @ViewChild('successMsg', { static: true })
30         private successMsg: StringComponent;
31
32     @ViewChild('errorMsg', { static: true })
33         private errorMsg: StringComponent;
34
35     constructor(
36         private modal: NgbModal, // required for passing to parent
37         private toast: ToastService,
38         private net: NetService,
39         private evt: EventService,
40         private auth: AuthService) {
41         super(modal); // required for subclassing
42     }
43
44     ngOnInit() {}
45
46     open(args: NgbModalOptions): Observable<boolean> {
47         this.numSucceeded = 0;
48         this.numFailed = 0;
49         return super.open(args);
50     }
51
52     async markOneItemMissing(ids: number[]): Promise<any> {
53         if (ids.length === 0) {
54             return Promise.resolve();
55         }
56
57         const id = ids.pop();
58
59         return this.net.request(
60             'open-ils.circ',
61             'open-ils.circ.mark_item_missing',
62             this.auth.token(), id
63         ).toPromise().then(async(result) => {
64             if (Number(result) === 1) {
65                 this.numSucceeded++;
66                 this.toast.success(await this.successMsg.current());
67             } else {
68                 this.numFailed++;
69                 console.error('Mark missing failed ', this.evt.parse(result));
70                 this.toast.warning(await this.errorMsg.current());
71             }
72             return this.markOneItemMissing(ids);
73         });
74     }
75
76     async markItemsMissing(): Promise<any> {
77         this.numSucceeded = 0;
78         this.numFailed = 0;
79         const ids = [].concat(this.copyIds);
80         await this.markOneItemMissing(ids);
81         this.close(this.numSucceeded > 0);
82     }
83 }
84
85
86