]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/replace-barcode-dialog.component.ts
LP2045292 Color contrast for AngularJS patron bills
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / replace-barcode-dialog.component.ts
1 import {Component, Input, ViewChild, Renderer2} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {switchMap, map, tap} from 'rxjs/operators';
4 import {AuthService} from '@eg/core/auth.service';
5 import {IdlObject} from '@eg/core/idl.service';
6 import {EventService} from '@eg/core/event.service';
7 import {NetService} from '@eg/core/net.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {ToastService} from '@eg/share/toast/toast.service';
10 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
11 import {DialogComponent} from '@eg/share/dialog/dialog.component';
12 import {StringComponent} from '@eg/share/string/string.component';
13
14
15 /**
16  * Dialog for changing an item's barcode
17  */
18
19 @Component({
20     selector: 'eg-replace-barcode-dialog',
21     templateUrl: 'replace-barcode-dialog.component.html'
22 })
23
24 export class ReplaceBarcodeDialogComponent
25     extends DialogComponent {
26
27     @Input() copyIds: number[];
28     ids: number[]; // copy of list so we can pop()
29
30     copy: IdlObject;
31     newBarcode: string;
32     barcodeExists: boolean;
33
34     numSucceeded: number;
35     numFailed: number;
36
37     @ViewChild('successMsg', { static: true })
38     private successMsg: StringComponent;
39
40     @ViewChild('errorMsg', { static: true })
41     private errorMsg: StringComponent;
42
43     constructor(
44         private modal: NgbModal, // required for passing to parent
45         private toast: ToastService,
46         private auth: AuthService,
47         private net: NetService,
48         private evt: EventService,
49         private pcrud: PcrudService,
50         private renderer: Renderer2) {
51         super(modal); // required for subclassing
52     }
53
54     open(args: NgbModalOptions): Observable<boolean> {
55         this.ids = [].concat(this.copyIds);
56         this.numSucceeded = 0;
57         this.numFailed = 0;
58
59         return this.getNextCopy()
60             .pipe(switchMap(() => super.open(args)),
61                 tap(() =>
62                     this.renderer.selectRootElement('#new-barcode-input').focus())
63             );
64     }
65
66     getNextCopy(): Observable<any> {
67
68         if (this.auth.opChangeIsActive()) {
69             // FIXME: kludge for now, opChange has been reverting mid-dialog with batch use when handling permission elevation
70             this.auth.undoOpChange();
71         }
72
73         if (this.ids.length === 0) {
74             this.close(this.numSucceeded > 0);
75         }
76
77         this.newBarcode = '';
78
79         const id = this.ids.pop();
80
81         return this.pcrud.retrieve('acp', id)
82             .pipe(map(c => this.copy = c));
83     }
84
85     replaceOneBarcode() {
86         this.barcodeExists = false;
87
88         // First see if the barcode is in use
89         return this.pcrud.search('acp', {deleted: 'f', barcode: this.newBarcode})
90             .toPromise().then(async (existing) => {
91                 if (existing) {
92                     this.barcodeExists = true;
93                     return;
94                 }
95
96                 this.net.request(
97                     'open-ils.cat',
98                     'open-ils.cat.update_copy_barcode',
99                     this.auth.token(), this.copy.id(), this.newBarcode
100                 ).subscribe(
101                     (res) => {
102                         if (this.evt.parse(res)) {
103                             console.error('parsed error response', res);
104                         } else {
105                             console.log('success', res);
106                             this.numSucceeded++;
107                             this.successMsg.current().then(m => this.toast.success(m));
108                             this.getNextCopy().toPromise();
109                         }
110                     },
111                     (err: unknown) => {
112                         console.error('error', err);
113                         this.numFailed++;
114                         console.error('Replace barcode failed: ', err);
115                         this.errorMsg.current().then(m => this.toast.warning(m));
116                     },
117                     () => {
118                         console.log('finis');
119                     }
120                 );
121             });
122     }
123 }
124
125
126