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';
16 templateUrl: './pickup.component.html'
19 export class PickupComponent implements OnInit, OnDestroy {
21 findPatron: FormGroup;
22 subscriptions: Subscription[] = [];
23 onlyShowCaptured = true;
25 @ViewChild('readyGrid', { static: false }) readyGrid: ReservationsGridComponent;
26 @ViewChild('pickedUpGrid', { static: false }) pickedUpGrid: ReservationsGridComponent;
28 noSelectedRows: (rows: IdlObject[]) => boolean;
29 handleShowCapturedChange: () => void;
30 retrievePatron: () => void;
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
45 this.findPatron = new FormGroup({
46 'patronBarcode': new FormControl(null,
47 [Validators.required],
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', {
60 flesh_fields: {'au': ['card']}});
64 this.findPatron.patchValue({patronBarcode: response.card().barcode()}, {emitEvent: false});
65 this.readyGrid.reloadGrid();
66 this.pickedUpGrid.reloadGrid();
70 const debouncing = 1500;
71 this.subscriptions.push(
72 this.patronBarcode.valueChanges.pipe(
73 debounceTime(debouncing),
75 if ('INVALID' === this.patronBarcode.status) {
76 this.toast.danger('No patron found with this barcode');
79 return this.patron.bcSearch(val).pipe(
81 tap((resp) => { this.router.navigate(['/staff', 'booking', 'pickup', 'by_patron', resp[0].id]); })
89 this.store.getItem('eg.booking.pickup.ready.only_show_captured').then(onlyCaptured => {
90 if (onlyCaptured != null) { this.onlyShowCaptured = onlyCaptured; }
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);
100 get patronBarcode() {
101 return this.findPatron.get('patronBarcode');
104 ngOnDestroy(): void {
105 this.subscriptions.forEach((subscription) => {
106 subscription.unsubscribe();