1 import {Component, 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';
13 * Dialog for changing an item's barcode
17 selector: 'eg-replace-barcode-dialog',
18 templateUrl: 'replace-barcode-dialog.component.html'
21 export class ReplaceBarcodeDialogComponent
22 extends DialogComponent {
24 @Input() copyIds: number[];
25 ids: number[]; // copy of list so we can pop()
29 barcodeExists: boolean;
34 @ViewChild('successMsg', { static: true })
35 private successMsg: StringComponent;
37 @ViewChild('errorMsg', { static: true })
38 private errorMsg: StringComponent;
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
48 open(args: NgbModalOptions): Observable<boolean> {
49 this.ids = [].concat(this.copyIds);
50 this.numSucceeded = 0;
53 return this.getNextCopy()
54 .pipe(switchMap(() => super.open(args)),
56 this.renderer.selectRootElement('#new-barcode-input').focus())
60 getNextCopy(): Observable<any> {
62 if (this.ids.length === 0) {
63 this.close(this.numSucceeded > 0);
68 const id = this.ids.pop();
70 return this.pcrud.retrieve('acp', id)
71 .pipe(map(c => this.copy = c));
74 async replaceOneBarcode(): Promise<any> {
75 this.barcodeExists = false;
77 // First see if the barcode is in use
78 return this.pcrud.search('acp', {deleted: 'f', barcode: this.newBarcode})
79 .toPromise().then(async (existing) => {
81 this.barcodeExists = true;
85 this.copy.barcode(this.newBarcode);
86 this.pcrud.update(this.copy).toPromise().then(
89 this.toast.success(await this.successMsg.current());
90 return this.getNextCopy().toPromise();
94 console.error('Replace barcode failed: ', err);
95 this.toast.warning(await this.errorMsg.current());