LP1904036 Refresh patron data when returning to patron
authorBill Erickson <berickxx@gmail.com>
Tue, 9 Nov 2021 18:49:07 +0000 (13:49 -0500)
committerGalen Charlton <gmc@equinoxOLI.org>
Fri, 28 Oct 2022 00:13:39 +0000 (20:13 -0400)
Since patron data is cached at the service level within Angular
navigation.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Jane Sandberg <js7389@princeton.edu>
Signed-off-by: Galen Charlton <gmc@equinoxOLI.org>
Open-ILS/src/eg2/src/app/staff/circ/patron/patron.component.ts

index 38af2b8..0684348 100644 (file)
@@ -1,8 +1,8 @@
 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';
@@ -36,6 +36,7 @@ export class PatronComponent implements OnInit, AfterViewInit {
     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
@@ -119,6 +120,24 @@ export class PatronComponent implements OnInit, AfterViewInit {
 
     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);
         });