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';
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 implements OnInit {
24 @Input() copyIds: number[];
25 ids: number[]; // copy of list so we can pop()
29 barcodeExists: boolean;
34 @ViewChild('successMsg')
35 private successMsg: StringComponent;
37 @ViewChild('errorMsg')
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
50 open(args: NgbModalOptions): Observable<boolean> {
51 this.ids = [].concat(this.copyIds);
52 this.numSucceeded = 0;
55 return this.getNextCopy()
56 .pipe(switchMap(() => super.open(args)),
58 this.renderer.selectRootElement('#new-barcode-input').focus())
62 getNextCopy(): Observable<any> {
64 if (this.ids.length === 0) {
65 this.close(this.numSucceeded > 0);
70 const id = this.ids.pop();
72 return this.pcrud.retrieve('acp', id)
73 .pipe(map(c => this.copy = c));
76 async replaceOneBarcode(): Promise<any> {
77 this.barcodeExists = false;
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) => {
83 this.barcodeExists = true;
87 this.copy.barcode(this.newBarcode);
88 this.pcrud.update(this.copy).toPromise().then(
91 this.toast.success(await this.successMsg.current());
92 return this.getNextCopy().toPromise();
96 console.error('Replace barcode failed: ', err);
97 this.toast.warning(await this.errorMsg.current());