From ce46dffc1855fae2bb535bd5aa7d1807ac9684d0 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Tue, 17 May 2022 19:38:18 +0000 Subject: [PATCH] LP#1942220: (follow-up) catch unsaved LI copy changes The interface now discourages letting leaving the LI copy page if there are unsaved changes. Signed-off-by: Galen Charlton --- .../staff/acq/lineitem/batch-copies.component.html | 1 + .../staff/acq/lineitem/batch-copies.component.ts | 4 ++++ .../app/staff/acq/lineitem/copies.component.html | 12 +++++++++++- .../src/app/staff/acq/lineitem/copies.component.ts | 17 +++++++++++++++++ .../app/staff/acq/lineitem/copy-attrs.component.ts | 13 +++++++++++++ .../src/eg2/src/app/staff/acq/po/routing.module.ts | 22 +++++++++++++++++++--- 6 files changed, 65 insertions(+), 4 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/staff/acq/lineitem/batch-copies.component.html b/Open-ILS/src/eg2/src/app/staff/acq/lineitem/batch-copies.component.html index 7359c246b4..a654c87bc8 100644 --- a/Open-ILS/src/eg2/src/app/staff/acq/lineitem/batch-copies.component.html +++ b/Open-ILS/src/eg2/src/app/staff/acq/lineitem/batch-copies.component.html @@ -51,6 +51,7 @@ (deleteRequested)="deleteCopy($event)" (cancelRequested)="cancelCopy($event)" [showReceiver]="!hasEditableCopies()" + (becameDirty)="becameDirty.emit(true)" [rowIndex]="idx + 1" [lineitem]="lineitem" [copy]="copy"> diff --git a/Open-ILS/src/eg2/src/app/staff/acq/lineitem/batch-copies.component.ts b/Open-ILS/src/eg2/src/app/staff/acq/lineitem/batch-copies.component.ts index 370ed34da0..f312b43eca 100644 --- a/Open-ILS/src/eg2/src/app/staff/acq/lineitem/batch-copies.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/acq/lineitem/batch-copies.component.ts @@ -30,6 +30,8 @@ export class LineitemBatchCopiesComponent implements OnInit { @Input() lineitem: IdlObject; @Input() batchAdd = false; + @Output() becameDirty = new EventEmitter(); + @ViewChild('confirmAlertsDialog') confirmAlertsDialog: LineitemAlertDialogComponent; @ViewChild('cancelDialog') cancelDialog: CancelDialogComponent; @@ -64,6 +66,7 @@ export class LineitemBatchCopiesComponent implements OnInit { this.lineitem.lineitem_details().forEach(copy => { copy[field](val); copy.ischanged(true); // isnew() takes precedence + this.becameDirty.emit(true); }); }); } @@ -77,6 +80,7 @@ export class LineitemBatchCopiesComponent implements OnInit { } else { // Requires a Save Changes action. copy.isdeleted(true); + this.becameDirty.emit(true); } } diff --git a/Open-ILS/src/eg2/src/app/staff/acq/lineitem/copies.component.html b/Open-ILS/src/eg2/src/app/staff/acq/lineitem/copies.component.html index 380336bb4a..d9301f108b 100644 --- a/Open-ILS/src/eg2/src/app/staff/acq/lineitem/copies.component.html +++ b/Open-ILS/src/eg2/src/app/staff/acq/lineitem/copies.component.html @@ -1,4 +1,11 @@

Items for Line Item {{lineitem.id()}} ({{getTitle(lineitem)}})

+ + + +
@@ -59,7 +66,10 @@
- + diff --git a/Open-ILS/src/eg2/src/app/staff/acq/lineitem/copies.component.ts b/Open-ILS/src/eg2/src/app/staff/acq/lineitem/copies.component.ts index 3ac060a148..d6444833a5 100644 --- a/Open-ILS/src/eg2/src/app/staff/acq/lineitem/copies.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/acq/lineitem/copies.component.ts @@ -1,6 +1,7 @@ import {Component, OnInit, AfterViewInit, Input, Output, EventEmitter, ViewChild} from '@angular/core'; import {Router, ActivatedRoute, ParamMap} from '@angular/router'; +import {Observable, of} from 'rxjs'; import {tap} from 'rxjs/operators'; import {Pager} from '@eg/share/util/pager'; import {IdlService, IdlObject} from '@eg/core/idl.service'; @@ -11,6 +12,7 @@ import {AuthService} from '@eg/core/auth.service'; import {LineitemService, FleshCacheParams} from './lineitem.service'; import {ComboboxEntry} from '@eg/share/combobox/combobox.component'; import {ItemLocationService} from '@eg/share/item-location-select/item-location-select.service'; +import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component'; const FORMULA_FIELDS = [ 'owning_lib', @@ -49,6 +51,7 @@ export class LineitemCopiesComponent implements OnInit, AfterViewInit { batchOwningLib: IdlObject; batchFund: ComboboxEntry; batchCopyLocId: number; + dirty = false; saving = false; progressMax = 0; progressValue = 0; @@ -59,6 +62,8 @@ export class LineitemCopiesComponent implements OnInit, AfterViewInit { // Can any changes be applied? liLocked = false; + @ViewChild('leaveConfirm', { static: true }) leaveConfirm: ConfirmDialogComponent; + constructor( private route: ActivatedRoute, private idl: IdlService, @@ -135,6 +140,7 @@ export class LineitemCopiesComponent implements OnInit, AfterViewInit { copy.isnew(true); copy.lineitem(this.lineitem.id()); copies.push(copy); + this.dirty = true; } if (copies.length > this.copyCount) { @@ -195,6 +201,7 @@ export class LineitemCopiesComponent implements OnInit, AfterViewInit { if (this.mode === 'multiAdd') { app.isnew(true); this.lineitem.distribution_formulas().push(app); + this.dirty = true; } else { this.pcrud.create(app).toPromise().then(a => { a.creator(this.auth.user()); @@ -262,6 +269,7 @@ export class LineitemCopiesComponent implements OnInit, AfterViewInit { } else { copy[field](val); + this.dirty = true; } }); @@ -282,6 +290,7 @@ export class LineitemCopiesComponent implements OnInit, AfterViewInit { () => this.load({toCache: true}).then(_ => { this.liService.activateStateChange.emit(this.lineitem.id()); this.saving = false; + this.dirty = false; }) ); } @@ -298,6 +307,14 @@ export class LineitemCopiesComponent implements OnInit, AfterViewInit { getTitle(li: IdlObject): string { return this.liService.getFirstAttributeValue(li, 'title'); } + + canDeactivate(): Observable { + if (this.dirty) { + return this.leaveConfirm.open(); + } else { + return of(true); + } + } } diff --git a/Open-ILS/src/eg2/src/app/staff/acq/lineitem/copy-attrs.component.ts b/Open-ILS/src/eg2/src/app/staff/acq/lineitem/copy-attrs.component.ts index b85c40ef09..b7359d2a4f 100644 --- a/Open-ILS/src/eg2/src/app/staff/acq/lineitem/copy-attrs.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/acq/lineitem/copy-attrs.component.ts @@ -20,6 +20,8 @@ export class LineitemCopyAttrsComponent implements OnInit { @Input() rowIndex: number; @Input() batchAdd = false; + @Output() becameDirty = new EventEmitter(); + fundEntries: ComboboxEntry[]; circModEntries: ComboboxEntry[]; @@ -100,6 +102,17 @@ export class LineitemCopyAttrsComponent implements OnInit { const announce: any = {}; this.copy.ischanged(true); + if (!this.batchMode) { + if (field !== 'owning_lib') { + this.becameDirty.emit(true); + } else { + // FIXME eg-org-select current send needless change + // events, so we need to check + if (entry && this.copy[field]() !== entry.id()) { + this.becameDirty.emit(true); + } + } + } switch (field) { diff --git a/Open-ILS/src/eg2/src/app/staff/acq/po/routing.module.ts b/Open-ILS/src/eg2/src/app/staff/acq/po/routing.module.ts index d294a53686..8f3508d786 100644 --- a/Open-ILS/src/eg2/src/app/staff/acq/po/routing.module.ts +++ b/Open-ILS/src/eg2/src/app/staff/acq/po/routing.module.ts @@ -1,5 +1,8 @@ -import {NgModule} from '@angular/core'; +import {NgModule, Injectable} from '@angular/core'; import {RouterModule, Routes} from '@angular/router'; +import {Router, Resolve, RouterStateSnapshot, + ActivatedRouteSnapshot, CanDeactivate} from '@angular/router'; +import {Observable} from 'rxjs'; import {PoComponent} from './po.component'; import {PrintComponent} from './print.component'; import {PoSummaryComponent} from './summary.component'; @@ -14,6 +17,18 @@ import {PoHistoryComponent} from './history.component'; import {PoEdiMessagesComponent} from './edi.component'; import {PoCreateComponent} from './create.component'; +// following example of https://www.concretepage.com/angular-2/angular-candeactivate-guard-example +export interface PoChildDeactivationGuarded { + canDeactivate(): Observable | Promise | boolean; +} + +@Injectable() +export class CanLeavePoChildGuard implements CanDeactivate { + canDeactivate(component: PoChildDeactivationGuarded): Observable | Promise | boolean { + return component.canDeactivate ? component.canDeactivate() : true; + } +} + const routes: Routes = [{ path: 'create', component: PoCreateComponent @@ -43,7 +58,8 @@ const routes: Routes = [{ component: LineitemHistoryComponent }, { path: 'lineitem/:lineitemId/items', - component: LineitemCopiesComponent + component: LineitemCopiesComponent, + canDeactivate: [CanLeavePoChildGuard] }, { path: 'lineitem/:lineitemId/worksheet', component: LineitemWorksheetComponent @@ -62,7 +78,7 @@ const routes: Routes = [{ @NgModule({ imports: [RouterModule.forChild(routes)], exports: [RouterModule], - providers: [] + providers: [CanLeavePoChildGuard] }) export class PoRoutingModule {} -- 2.11.0