e50f5c19e008aeced7650640f76973ff74d0f158
[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} from 'rxjs';
3 import {switchMap, map, tap} from 'rxjs/operators';
4 import {IdlObject} from '@eg/core/idl.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {ToastService} from '@eg/share/toast/toast.service';
7 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
8 import {DialogComponent} from '@eg/share/dialog/dialog.component';
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-replace-barcode-dialog',
18   templateUrl: 'replace-barcode-dialog.component.html'
19 })
20
21 export class ReplaceBarcodeDialogComponent
22     extends DialogComponent implements OnInit {
23
24     @Input() copyIds: number[];
25     ids: number[]; // copy of list so we can pop()
26
27     copy: IdlObject;
28     newBarcode: string;
29     barcodeExists: boolean;
30
31     numSucceeded: number;
32     numFailed: number;
33
34     @ViewChild('successMsg')
35         private successMsg: StringComponent;
36
37     @ViewChild('errorMsg')
38         private errorMsg: StringComponent;
39
40     constructor(
41         private modal: NgbModal, // required for passing to parent
42         private toast: ToastService,
43         private pcrud: PcrudService,
44         private renderer: Renderer2) {
45         super(modal); // required for subclassing
46     }
47
48     ngOnInit() {}
49
50     open(args: NgbModalOptions): Observable<boolean> {
51         this.ids = [].concat(this.copyIds);
52         this.numSucceeded = 0;
53         this.numFailed = 0;
54
55         return this.getNextCopy()
56         .pipe(switchMap(() => super.open(args)),
57             tap(() =>
58                 this.renderer.selectRootElement('#new-barcode-input').focus())
59         );
60     }
61
62     getNextCopy(): Observable<any> {
63
64         if (this.ids.length === 0) {
65             this.close(this.numSucceeded > 0);
66         }
67
68         this.newBarcode = '';
69
70         const id = this.ids.pop();
71
72         return this.pcrud.retrieve('acp', id)
73         .pipe(map(c => this.copy = c));
74     }
75
76     async replaceOneBarcode(): Promise<any> {
77         this.barcodeExists = false;
78
79         // First see if the barcode is in use
80         return this.pcrud.search('acp', {deleted: 'f', barcode: this.newBarcode})
81         .toPromise().then(async (existing) => {
82             if (existing) {
83                 this.barcodeExists = true;
84                 return;
85             }
86
87             this.copy.barcode(this.newBarcode);
88             this.pcrud.update(this.copy).toPromise().then(
89                 async (ok) => {
90                     this.numSucceeded++;
91                     this.toast.success(await this.successMsg.current());
92                     return this.getNextCopy().toPromise();
93                 },
94                 async (err) => {
95                     this.numFailed++;
96                     console.error('Replace barcode failed: ', err);
97                     this.toast.warning(await this.errorMsg.current());
98                 }
99             );
100         });
101     }
102 }
103
104
105