]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/delete-volcopy-dialog.component.ts
LP1615805 No inputs after submit in patron search (AngularJS)
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / delete-volcopy-dialog.component.ts
1 import {Component, OnInit, Input, ViewChild, Renderer2} from '@angular/core';
2 import {Observable, throwError} from 'rxjs';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {NetService} from '@eg/core/net.service';
5 import {EgEvent, EventService} from '@eg/core/event.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {AuthService} from '@eg/core/auth.service';
9 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
10 import {DialogComponent} from '@eg/share/dialog/dialog.component';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
13
14
15 /**
16  * Dialog that confirms, then deletes items and call numbers
17  */
18
19 @Component({
20   selector: 'eg-delete-holding-dialog',
21   templateUrl: 'delete-volcopy-dialog.component.html'
22 })
23
24 export class DeleteHoldingDialogComponent
25     extends DialogComponent implements OnInit {
26
27     // List of "acn" objects which may contain copies.
28     // Objects of either type marked "isdeleted" will be deleted.
29     @Input() callNums: IdlObject[];
30
31     // If true, just ask the server to delete all attached copies
32     // for any deleted call numbers.
33     // Note if this is true and a call number is provided that does not
34     // contain its fleshed copies, the number of copies to delete will not be
35     // reported correctly.
36     @Input() forceDeleteCopies: boolean;
37
38     numCallNums: number;
39     numCopies: number;
40     numSucceeded: number;
41     numFailed: number;
42     deleteEventDesc: string;
43
44     @ViewChild('successMsg', { static: true })
45         private successMsg: StringComponent;
46
47     @ViewChild('errorMsg', { static: true })
48         private errorMsg: StringComponent;
49
50     @ViewChild('confirmOverride', {static: false})
51         private confirmOverride: ConfirmDialogComponent;
52
53     constructor(
54         private modal: NgbModal, // required for passing to parent
55         private toast: ToastService,
56         private net: NetService,
57         private pcrud: PcrudService,
58         private evt: EventService,
59         private renderer: Renderer2,
60         private auth: AuthService) {
61         super(modal); // required for subclassing
62     }
63
64     ngOnInit() {}
65
66     open(args: NgbModalOptions): Observable<boolean> {
67         this.numCallNums = 0;
68         this.numCopies = 0;
69         this.numSucceeded = 0;
70         this.numFailed = 0;
71
72         this.callNums.forEach(callNum => {
73             if (callNum.isdeleted()) {
74                 this.numCallNums++;
75             }
76             if (Array.isArray(callNum.copies())) {
77                 callNum.copies().forEach(c => {
78                     if (c.isdeleted() || this.forceDeleteCopies) {
79                         // Marking copies deleted in forceDeleteCopies mode
80                         // is not required, but we do it here so we can
81                         // report the number of copies to be deleted.
82                         c.isdeleted(true);
83                         this.numCopies++;
84                     }
85                 });
86             }
87         });
88
89         if (this.numCallNums === 0 && this.numCopies === 0) {
90             console.debug('Holdings delete called with no usable data');
91             return throwError(false);
92         }
93
94         return super.open(args);
95     }
96
97     deleteHoldings(override?: boolean) {
98
99         this.deleteEventDesc = '';
100
101         const flags: any = {
102             force_delete_copies: this.forceDeleteCopies
103         };
104
105         let method = 'open-ils.cat.asset.volume.fleshed.batch.update';
106         if (override) {
107             method = `${method}.override`;
108             flags.events = ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'];
109         }
110
111         this.net.request(
112             'open-ils.cat', method,
113             this.auth.token(), this.callNums, 1, flags
114         ).toPromise().then(
115             result => {
116                 const evt = this.evt.parse(result);
117                 if (evt) {
118                     this.handleDeleteEvent(evt, override);
119                 } else {
120                     this.numSucceeded++;
121                     this.close(this.numSucceeded > 0);
122                 }
123             },
124             err => {
125                 console.warn(err);
126                 this.errorMsg.current().then(msg => this.toast.warning(msg));
127                 this.numFailed++;
128             }
129         );
130     }
131
132     handleDeleteEvent(evt: EgEvent, override?: boolean): Promise<any> {
133
134         if (override) { // override failed
135             console.warn(evt);
136             this.numFailed++;
137             return this.errorMsg.current().then(msg => this.toast.warning(msg));
138         }
139
140         this.deleteEventDesc = evt.desc;
141
142         return this.confirmOverride.open().toPromise().then(confirmed => {
143             if (confirmed) {
144                 return this.deleteHoldings(true);
145
146             } else {
147                 // User canceled the delete confirmation dialog
148                 this.numFailed++;
149                 this.errorMsg.current().then(msg => this.toast.warning(msg));
150                 this.close(this.numSucceeded > 0);
151             }
152         });
153     }
154 }
155
156
157