]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/replace-barcode-dialog.component.ts
552922e9a085d7e7263bcadb03df5dcded00d1f8
[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 {IdlObject} from '@eg/core/idl.service';
3 import {NetService} from '@eg/core/net.service';
4 import {EventService} from '@eg/core/event.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {ToastService} from '@eg/share/toast/toast.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
9 import {DialogComponent} from '@eg/share/dialog/dialog.component';
10 import {StringComponent} from '@eg/share/string/string.component';
11
12
13 /**
14  * Dialog for marking items missing.
15  */
16
17 @Component({
18   selector: 'eg-replace-barcode-dialog',
19   templateUrl: 'replace-barcode-dialog.component.html'
20 })
21
22 export class ReplaceBarcodeDialogComponent
23     extends DialogComponent implements OnInit {
24
25     @Input() copyIds: number[];
26     ids: number[]; // copy of list so we can pop()
27
28     copy: IdlObject;
29     newBarcode: string;
30     barcodeExists: boolean;
31
32     numSucceeded: number;
33     numFailed: number;
34
35     @ViewChild('successMsg')
36         private successMsg: StringComponent;
37
38     @ViewChild('errorMsg')
39         private errorMsg: StringComponent;
40
41     constructor(
42         private modal: NgbModal, // required for passing to parent
43         private toast: ToastService,
44         private net: NetService,
45         private pcrud: PcrudService,
46         private evt: EventService,
47         private renderer: Renderer2,
48         private auth: AuthService) {
49         super(modal); // required for subclassing
50     }
51
52     ngOnInit() {}
53
54     async open(args: NgbModalOptions): Promise<boolean> {
55         this.ids = [].concat(this.copyIds);
56         this.numSucceeded = 0;
57         this.numFailed = 0;
58
59         await this.getNextCopy();
60         setTimeout(() =>
61             // Give the dialog a chance to render
62             this.renderer.selectRootElement('#new-barcode-input').focus()
63         );
64         return super.open(args);
65     }
66
67     async getNextCopy(): Promise<any> {
68
69         if (this.ids.length === 0) {
70             this.close(this.numSucceeded > 0);
71             return Promise.resolve();
72         }
73
74         this.newBarcode = '';
75
76         const id = this.ids.pop();
77
78         return this.pcrud.retrieve('acp', id)
79         .toPromise().then(c => this.copy = c);
80     }
81
82     async replaceOneBarcode(): Promise<any> {
83         this.barcodeExists = false;
84
85         // First see if the barcode is in use
86         return this.pcrud.search('acp', {deleted: 'f', barcode: this.newBarcode})
87         .toPromise().then(async (existing) => {
88             if (existing) {
89                 this.barcodeExists = true;
90                 return;
91             }
92
93             this.copy.barcode(this.newBarcode);
94             this.pcrud.update(this.copy).toPromise().then(
95                 async (ok) => {
96                     this.numSucceeded++;
97                     this.toast.success(await this.successMsg.current());
98                     this.getNextCopy();
99                 },
100                 async (err) => {
101                     this.numFailed++;
102                     console.error('Replace barcode failed: ', err);
103                     this.toast.warning(await this.errorMsg.current());
104                 }
105             )
106         })
107     }
108 }
109
110
111