]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/mark-discard-dialog.component.ts
LP1959048: manual ng lint fixes
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / mark-discard-dialog.component.ts
1 import {Component, Input, ViewChild} from '@angular/core';
2 import {from, Observable} from 'rxjs';
3 import {tap, concatMap} from 'rxjs/operators';
4 import {NetService} from '@eg/core/net.service';
5 import {EventService} from '@eg/core/event.service';
6 import {ToastService} from '@eg/share/toast/toast.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {DialogComponent} from '@eg/share/dialog/dialog.component';
9 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
10 import {StringComponent} from '@eg/share/string/string.component';
11
12
13 /**
14  * Dialog for marking items discard.
15  */
16
17 @Component({
18   selector: 'eg-mark-discard-dialog',
19   templateUrl: 'mark-discard-dialog.component.html'
20 })
21
22 export class MarkDiscardDialogComponent
23     extends DialogComponent {
24
25     @Input() copyIds: number[];
26
27     numSucceeded: number;
28     numFailed: number;
29
30     @ViewChild('successMsg') private successMsg: StringComponent;
31     @ViewChild('errorMsg') private errorMsg: StringComponent;
32
33     constructor(
34         private modal: NgbModal, // required for passing to parent
35         private toast: ToastService,
36         private net: NetService,
37         private evt: EventService,
38         private auth: AuthService) {
39         super(modal); // required for subclassing
40     }
41
42     open(args: NgbModalOptions): Observable<boolean> {
43         this.numSucceeded = 0;
44         this.numFailed = 0;
45         return super.open(args);
46     }
47
48     markOneItemDiscard(id: number): Observable<any> {
49
50         return this.net.request(
51             'open-ils.circ',
52             'open-ils.circ.mark_item_discard',
53             this.auth.token(), id
54         ).pipe(tap(result => {
55             if (Number(result) === 1) {
56                 this.numSucceeded++;
57                 this.successMsg.current().then(str => this.toast.success(str));
58             } else {
59                 this.numFailed++;
60                 console.error('Mark discard failed ', this.evt.parse(result));
61                 this.errorMsg.current().then(str => this.toast.warning(str));
62             }
63         }));
64     }
65
66     markItemsDiscard(): Promise<any> {
67         this.numSucceeded = 0;
68         this.numFailed = 0;
69
70         return from(this.copyIds)
71         .pipe(concatMap(copyId => this.markOneItemDiscard(copyId)))
72         .toPromise().then(_ => this.close(this.numSucceeded > 0));
73     }
74 }
75
76
77