import {Component, ViewChild, OnInit, AfterViewInit, HostListener} from '@angular/core';
-import {Router, ActivatedRoute, ParamMap} from '@angular/router';
+import {Router, ActivatedRoute, ParamMap, RoutesRecognized} from '@angular/router';
import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
import {Observable, throwError, empty} from 'rxjs';
-import {concatMap, tap} from 'rxjs/operators';
+import {filter, pairwise, concatMap, tap} from 'rxjs/operators';
import {NetService} from '@eg/core/net.service';
import {AuthService} from '@eg/core/auth.service';
import {PcrudService} from '@eg/core/pcrud.service';
showSummary = true;
loading = true;
showRecentPatrons = false;
+ refreshing = false;
/* eg-patron-edit is unable to find #editorToolbar directly
* within the template. Adding a ref here allows it to
watchForTabChange() {
+ // When routing to a patron UI from a non-patron UI, refresh the
+ // patron's data so we pick up an external changes (e.g. holds
+ // placement). This is needed because the last active patron,
+ // if one exists, will still be cached in the patron service,
+ // which survives route changes. Route events occur in series
+ // and often repeat, though, so avoid refreshing the patron if
+ // we are already mid-refresh.
+ this.router.events
+ .pipe(filter((evt: any) => evt instanceof RoutesRecognized), pairwise())
+ .subscribe((events: RoutesRecognized[]) => {
+ const prevUrl = events[0].urlAfterRedirects;
+
+ if (!prevUrl.startsWith('/staff/circ/patron') && !this.refreshing) {
+ this.refreshing = true;
+ this.context.refreshPatron().then(_ => this.refreshing = false);
+ }
+ });
+
this.route.data.subscribe(data => {
this.showRecentPatrons = (data && data.showRecentPatrons);
});