1 import {Component, OnInit, OnDestroy, QueryList, ViewChildren, ViewChild} from '@angular/core';
2 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
3 import {FormGroup, FormControl, Validators} from '@angular/forms';
4 import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
5 import {Observable, from, of, Subscription} from 'rxjs';
6 import { single, switchMap, tap, debounceTime } from 'rxjs/operators';
7 import {PatronService} from '@eg/staff/share/patron/patron.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {IdlObject} from '@eg/core/idl.service';
10 import {ReservationsGridComponent} from './reservations-grid.component';
11 import {ServerStoreService} from '@eg/core/server-store.service';
12 import {ToastService} from '@eg/share/toast/toast.service';
13 import {PatronBarcodeValidator} from '@eg/share/validators/patron_barcode_validator.directive';
17 templateUrl: './return.component.html'
20 export class ReturnComponent implements OnInit, OnDestroy {
22 findPatron: FormGroup;
23 subscriptions: Subscription[] = [];
26 noSelectedRows: (rows: IdlObject[]) => boolean;
27 handleNavChange: ($event: NgbNavChangeEvent) => void;
28 @ViewChild('tabs', { static: true }) tabs: NgbNav;
29 @ViewChildren(ReservationsGridComponent) grids: QueryList<ReservationsGridComponent>;
32 private pcrud: PcrudService,
33 private patron: PatronService,
34 private pbv: PatronBarcodeValidator,
35 private route: ActivatedRoute,
36 private router: Router,
37 private store: ServerStoreService,
38 private toast: ToastService
44 this.route.paramMap.pipe(switchMap((params: ParamMap) => {
45 return this.handleParams$(params);
49 this.store.getItem('eg.booking.return.tab')
52 this.findPatron = new FormGroup({
53 'patronBarcode': new FormControl(null,
54 [Validators.required],
56 'resourceBarcode': new FormControl(null,
57 [Validators.required])
60 const debouncing = 1500;
61 this.subscriptions.push(
62 this.patronBarcode.valueChanges.pipe(
63 debounceTime(debouncing),
65 if ('INVALID' === this.patronBarcode.status) {
66 this.toast.danger('No patron found with this barcode');
69 return this.patron.bcSearch(val).pipe(
71 tap((resp) => { this.router.navigate(['/staff', 'booking', 'return', 'by_patron', resp[0].id]); })
78 this.subscriptions.push(
79 this.resourceBarcode.valueChanges.pipe(
80 debounceTime(debouncing),
82 if ('INVALID' !== this.resourceBarcode.status) {
83 return this.pcrud.search('brsrc', {'barcode': val}, {
84 order_by: {'curr_rsrcs': 'pickup_time DESC'},
87 flesh_fields: {'brsrc': ['curr_rsrcs']},
88 select: {'curr_rsrcs': {'return_time': null, 'pickup_time': {'!=': null}}}
89 }).pipe(tap((resp) => {
90 if (resp.curr_rsrcs()[0].usr()) {
91 this.tabs.select('resource');
92 this.patronId = resp.curr_rsrcs()[0].usr();
102 this.noSelectedRows = (rows: IdlObject[]) => (rows.length === 0);
104 this.handleNavChange = ($event) => {
105 this.store.setItem('eg.booking.return.tab', $event.nextId)
107 this.router.navigate(['/staff', 'booking', 'return']);
108 this.findPatron.patchValue({resourceBarcode: ''});
109 this.patronId = null;
114 handleParams$ = (params: ParamMap): Observable<any> => {
115 this.patronId = +params.get('patron_id');
117 return this.pcrud.search('au', {
122 flesh_fields: {'au': ['card']}
125 this.tabs.select('patron_tab');
126 this.findPatron.patchValue({patronBarcode: resp.card().barcode()});
128 }, (err) => { console.debug(err); }
131 return from(this.store.getItem('eg.booking.return.tab'))
133 if (tab) { this.tabs.select(tab); }
137 refreshGrids = (): void => {
138 this.grids.forEach (grid => grid.reloadGrid());
140 get patronBarcode() {
141 return this.findPatron.get('patronBarcode');
143 get resourceBarcode() {
144 return this.findPatron.get('resourceBarcode');
147 ngOnDestroy(): void {
148 this.subscriptions.forEach((subscription) => {
149 subscription.unsubscribe();