]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/replace-barcode-dialog.component.ts
LP1823041: Converting new dialogs to observables
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / replace-barcode-dialog.component.ts
1 import {Component, OnInit, Input, ViewChild, Renderer2} from '@angular/core';
2 import {Observable, throwError} from 'rxjs';
3 import {flatMap, map, tap} from 'rxjs/operators';
4 import {IdlObject} from '@eg/core/idl.service';
5 import {NetService} from '@eg/core/net.service';
6 import {EventService} from '@eg/core/event.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {ToastService} from '@eg/share/toast/toast.service';
9 import {AuthService} from '@eg/core/auth.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 marking items missing.
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 implements OnInit {
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')
38         private successMsg: StringComponent;
39
40     @ViewChild('errorMsg')
41         private errorMsg: StringComponent;
42
43     constructor(
44         private modal: NgbModal, // required for passing to parent
45         private toast: ToastService,
46         private net: NetService,
47         private pcrud: PcrudService,
48         private evt: EventService,
49         private renderer: Renderer2,
50         private auth: AuthService) {
51         super(modal); // required for subclassing
52     }
53
54     ngOnInit() {}
55
56     open(args: NgbModalOptions): Observable<boolean> {
57         this.ids = [].concat(this.copyIds);
58         this.numSucceeded = 0;
59         this.numFailed = 0;
60
61         return this.getNextCopy()
62         .pipe(flatMap(() => {
63             return super.open(args)
64             .pipe(tap(() => {this.renderer.selectRootElement('#new-barcode-input').focus(); }));
65         }));
66     }
67
68     getNextCopy(): Observable<any> {
69
70         if (this.ids.length === 0) {
71             this.close(this.numSucceeded > 0);
72             return throwError(false);
73         }
74
75         this.newBarcode = '';
76
77         const id = this.ids.pop();
78
79         return this.pcrud.retrieve('acp', id)
80         .pipe(map(c => this.copy = c));
81     }
82
83     async replaceOneBarcode(): Promise<any> {
84         this.barcodeExists = false;
85
86         // First see if the barcode is in use
87         return this.pcrud.search('acp', {deleted: 'f', barcode: this.newBarcode})
88         .toPromise().then(async (existing) => {
89             if (existing) {
90                 this.barcodeExists = true;
91                 return;
92             }
93
94             this.copy.barcode(this.newBarcode);
95             this.pcrud.update(this.copy).toPromise().then(
96                 async (ok) => {
97                     this.numSucceeded++;
98                     this.toast.success(await this.successMsg.current());
99                     this.getNextCopy();
100                 },
101                 async (err) => {
102                     this.numFailed++;
103                     console.error('Replace barcode failed: ', err);
104                     this.toast.warning(await this.errorMsg.current());
105                 }
106             );
107         });
108     }
109 }
110
111
112