]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/booking/pickup.component.ts
LP1859241 Angular holds patron search dialog
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / booking / pickup.component.ts
1 import {Component, OnInit, ViewChild, OnDestroy} from '@angular/core';
2 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
3 import {Subscription, of} from 'rxjs';
4 import {single, filter, switchMap, debounceTime, tap} from 'rxjs/operators';
5 import {PatronService} from '@eg/staff/share/patron/patron.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {IdlObject} from '@eg/core/idl.service';
8 import {ReservationsGridComponent} from './reservations-grid.component';
9 import {ServerStoreService} from '@eg/core/server-store.service';
10 import {ToastService} from '@eg/share/toast/toast.service';
11 import {FormControl, FormGroup, Validators} from '@angular/forms';
12 import {PatronBarcodeValidator} from '@eg/share/validators/patron_barcode_validator.directive';
13
14
15 @Component({
16   templateUrl: './pickup.component.html'
17 })
18
19 export class PickupComponent implements OnInit, OnDestroy {
20     patronId: number;
21     findPatron: FormGroup;
22     subscriptions: Subscription[] = [];
23     onlyShowCaptured = true;
24
25     @ViewChild('readyGrid', { static: false }) readyGrid: ReservationsGridComponent;
26     @ViewChild('pickedUpGrid', { static: false }) pickedUpGrid: ReservationsGridComponent;
27
28     noSelectedRows: (rows: IdlObject[]) => boolean;
29     handleShowCapturedChange: () => void;
30     retrievePatron: () => void;
31
32     constructor(
33         private pcrud: PcrudService,
34         private patron: PatronService,
35         private pbv: PatronBarcodeValidator,
36         private route: ActivatedRoute,
37         private router: Router,
38         private store: ServerStoreService,
39         private toast: ToastService
40     ) {
41     }
42
43
44     ngOnInit() {
45         this.findPatron = new FormGroup({
46             'patronBarcode': new FormControl(null,
47                 [Validators.required],
48                 [this.pbv.validate])
49         });
50
51         this.route.paramMap.pipe(
52             filter((params: ParamMap) => params.has('patron_id')),
53             switchMap((params: ParamMap) => {
54                 this.patronId = +params.get('patron_id');
55                 return this.pcrud.search('au', {
56                     'id': this.patronId,
57                 }, {
58                     limit: 1,
59                     flesh: 1,
60                     flesh_fields: {'au': ['card']}});
61             })
62         ).subscribe(
63             (response) => {
64                 this.findPatron.patchValue({patronBarcode: response.card().barcode()}, {emitEvent: false});
65                 this.readyGrid.reloadGrid();
66                 this.pickedUpGrid.reloadGrid();
67             }
68         );
69
70         const debouncing = 1500;
71         this.subscriptions.push(
72             this.patronBarcode.valueChanges.pipe(
73                 debounceTime(debouncing),
74                 switchMap((val) => {
75                     if ('INVALID' === this.patronBarcode.status) {
76                         this.toast.danger('No patron found with this barcode');
77                         return of();
78                     } else {
79                         return this.patron.bcSearch(val).pipe(
80                             single(),
81                             tap((resp) => { this.router.navigate(['/staff', 'booking', 'pickup', 'by_patron', resp[0].id]); })
82                         );
83                     }
84                 })
85             )
86             .subscribe());
87
88
89         this.store.getItem('eg.booking.pickup.ready.only_show_captured').then(onlyCaptured => {
90             if (onlyCaptured != null) { this.onlyShowCaptured = onlyCaptured; }
91         });
92         this.handleShowCapturedChange = () => {
93             this.onlyShowCaptured = !this.onlyShowCaptured;
94             this.readyGrid.reloadGrid();
95             this.store.setItem('eg.booking.pickup.ready.only_show_captured', this.onlyShowCaptured);
96         };
97
98
99     }
100     get patronBarcode() {
101         return this.findPatron.get('patronBarcode');
102     }
103
104     ngOnDestroy(): void {
105         this.subscriptions.forEach((subscription) => {
106             subscription.unsubscribe();
107         });
108     }
109
110 }