From da9e2dbb476195e0824f98e2b59076556cc36f17 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Thu, 8 Apr 2021 16:30:11 -0400 Subject: [PATCH] LP1904036 saved changes warning Signed-off-by: Bill Erickson Signed-off-by: Jane Sandberg Signed-off-by: Galen Charlton --- .../src/app/staff/circ/patron/edit.component.ts | 4 +- .../app/staff/circ/patron/patron.component.html | 7 +++- .../src/app/staff/circ/patron/patron.component.ts | 43 ++++++++++++++++++++-- .../src/app/staff/circ/patron/routing.module.ts | 4 +- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.ts b/Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.ts index f76e2fe6ab..f224d121de 100644 --- a/Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.ts @@ -1333,9 +1333,9 @@ export class EditComponent implements OnInit, AfterViewInit { save(clone?: boolean): Promise { - // TODO clear unload prompt - + this.changesPending = false; this.loading = true; + return this.saveUser() .then(_ => this.saveUserSettings()) .then(_ => this.updateHoldPrefs()) diff --git a/Open-ILS/src/eg2/src/app/staff/circ/patron/patron.component.html b/Open-ILS/src/eg2/src/app/staff/circ/patron/patron.component.html index 388c36ad4f..dba472e041 100644 --- a/Open-ILS/src/eg2/src/app/staff/circ/patron/patron.component.html +++ b/Open-ILS/src/eg2/src/app/staff/circ/patron/patron.component.html @@ -4,6 +4,11 @@ + + + Edit - + diff --git a/Open-ILS/src/eg2/src/app/staff/circ/patron/patron.component.ts b/Open-ILS/src/eg2/src/app/staff/circ/patron/patron.component.ts index ac1b0849f9..d87bac094e 100644 --- a/Open-ILS/src/eg2/src/app/staff/circ/patron/patron.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/circ/patron/patron.component.ts @@ -1,4 +1,4 @@ -import {Component, ViewChild, OnInit, AfterViewInit} from '@angular/core'; +import {Component, ViewChild, OnInit, AfterViewInit, HostListener} from '@angular/core'; import {Router, ActivatedRoute, ParamMap} from '@angular/router'; import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap'; import {NetService} from '@eg/core/net.service'; @@ -9,6 +9,8 @@ import {PatronContextService, BillGridEntry} from './patron.service'; import {PatronSearch, PatronSearchComponent } from '@eg/staff/share/patron/search.component'; import {EditToolbarComponent} from './edit-toolbar.component'; +import {EditComponent} from './edit.component'; +import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component'; const MAIN_TABS = ['checkout', 'items_out', 'holds', 'bills', 'messages', 'edit', 'search']; @@ -29,7 +31,12 @@ export class PatronComponent implements OnInit, AfterViewInit { /* eg-patron-edit is unable to find #editorToolbar directly * within the template. Adding a ref here allows it to * successfully transfer to the editor */ - @ViewChild('editorToolbar') editorToolbar: EditToolbarComponent; + @ViewChild('editorToolbar') private editorToolbar: EditToolbarComponent; + + @ViewChild('patronEditor') private patronEditor: EditComponent; + + @ViewChild('pendingChangesDialog') + private pendingChangesDialog: ConfirmDialogComponent; constructor( private router: Router, @@ -46,6 +53,29 @@ export class PatronComponent implements OnInit, AfterViewInit { this.load(); } + @HostListener('window:beforeunload', ['$event']) + canDeactivate($event?: Event): Promise { + + if (this.patronEditor && this.patronEditor.changesPending) { + + // Each warning dialog clears the current "changes are pending" + // flag so the user is not presented with the dialog again + // unless new changes are made. + this.patronEditor.changesPending = false; + + if ($event) { // window.onbeforeunload + $event.preventDefault(); + $event.returnValue = true; + + } else { // tab OR route change. + return this.pendingChangesDialog.open().toPromise(); + } + + } else { + return Promise.resolve(true); + } + } + load() { this.loading = true; this.fetchSettings() @@ -102,8 +132,13 @@ export class PatronComponent implements OnInit, AfterViewInit { // tab will change with route navigation. evt.preventDefault(); - this.patronTab = evt.nextId; - this.routeToTab(); + // Protect against tab changes with dirty data. + this.canDeactivate().then(ok => { + if (ok) { + this.patronTab = evt.nextId; + this.routeToTab(); + } + }); } // The bills tab has various sub-interfaces. If the user is already diff --git a/Open-ILS/src/eg2/src/app/staff/circ/patron/routing.module.ts b/Open-ILS/src/eg2/src/app/staff/circ/patron/routing.module.ts index 87fecbe331..728b86be23 100644 --- a/Open-ILS/src/eg2/src/app/staff/circ/patron/routing.module.ts +++ b/Open-ILS/src/eg2/src/app/staff/circ/patron/routing.module.ts @@ -5,6 +5,7 @@ import {BcSearchComponent} from './bcsearch.component'; import {PatronResolver} from './resolver.service'; import {TestPatronPasswordComponent} from './test-password.component'; import {RegisterPatronComponent} from './register.component'; +import {CanDeactivateGuard} from '@eg/share/util/can-deactivate.guard'; const routes: Routes = [{ path: '', @@ -49,7 +50,8 @@ const routes: Routes = [{ }, { path: ':id/:tab', component: PatronComponent, - resolve: {resolver : PatronResolver} + resolve: {resolver : PatronResolver}, + canDeactivate: [CanDeactivateGuard] }]; @NgModule({ -- 2.11.0