From a0bd1b11444e289d296229bc640ed14d67628569 Mon Sep 17 00:00:00 2001 From: amirch1 Date: Mon, 17 Jul 2017 10:06:44 +0300 Subject: [PATCH] fix: verify leave entry details when data is not saved --- .../content-entries-app.module.ts | 4 +- .../content-entries-app.routes.ts | 3 +- .../entry/entry-can-deactivate.service.ts | 24 +++++++++++ .../entry/entry-store.service.ts | 43 +++++++------------ .../entry/entry.component.ts | 8 ++-- 5 files changed, 49 insertions(+), 33 deletions(-) create mode 100644 src/applications/content-entries-app/entry/entry-can-deactivate.service.ts diff --git a/src/applications/content-entries-app/content-entries-app.module.ts b/src/applications/content-entries-app/content-entries-app.module.ts index 8a23d3041d..d637beda38 100644 --- a/src/applications/content-entries-app/content-entries-app.module.ts +++ b/src/applications/content-entries-app/content-entries-app.module.ts @@ -30,6 +30,7 @@ import { EntriesRefineFiltersProvider } from './entries/entries-refine-filters/e import { CategoriesPrimeService } from './shared/categories-prime.service'; import { BulkSchedulingService, BulkAccessControlService, BulkAddTagsService, BulkRemoveTagsService, BulkAddCategoriesService, BulkChangeOwnerService, BulkRemoveCategoriesService, BulkDeleteService, BulkDownloadService } from './entries/bulk-actions/services'; import { SharedComponentsList } from './shared/shared-components-list'; +import { EntryCanDeactivate } from './entry/entry-can-deactivate.service'; @NgModule({ imports: [ @@ -90,7 +91,8 @@ import { SharedComponentsList } from './shared/shared-components-list'; BulkChangeOwnerService, BulkRemoveCategoriesService, BulkDeleteService, - BulkDownloadService + BulkDownloadService, + EntryCanDeactivate ], }) export class ContentEntriesAppModule { diff --git a/src/applications/content-entries-app/content-entries-app.routes.ts b/src/applications/content-entries-app/content-entries-app.routes.ts index 51c4ecb89e..3242fa3004 100644 --- a/src/applications/content-entries-app/content-entries-app.routes.ts +++ b/src/applications/content-entries-app/content-entries-app.routes.ts @@ -14,13 +14,14 @@ import { EntryFlavours } from './entry/entry-flavours/entry-flavours.component'; import { EntryScheduling } from './entry/entry-scheduling/entry-scheduling.component'; import { EntryAccessControl } from './entry/entry-access-control/entry-access-control.component'; import { EntryThumbnails } from './entry/entry-thumbnails/entry-thumbnails.component'; +import { EntryCanDeactivate } from './entry/entry-can-deactivate.service'; export const routing: Route[] = [ {path: '', component: ContentEntriesComponent, children:[ {path: '', redirectTo: 'list', pathMatch: 'full'}, {path: 'list', component: EntriesListComponent}, - {path: 'entry/:id', component: EntryComponent, + {path: 'entry/:id',canDeactivate: [EntryCanDeactivate], component: EntryComponent, data : { entryRoute : true }, diff --git a/src/applications/content-entries-app/entry/entry-can-deactivate.service.ts b/src/applications/content-entries-app/entry/entry-can-deactivate.service.ts new file mode 100644 index 0000000000..51d88a166b --- /dev/null +++ b/src/applications/content-entries-app/entry/entry-can-deactivate.service.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@angular/core' +import { ActivatedRouteSnapshot, RouterStateSnapshot, CanDeactivate } from '@angular/router'; +import { EntryComponent } from './entry.component'; +import { Observable } from 'rxjs/Observable'; + +@Injectable() +export class EntryCanDeactivate implements CanDeactivate { + constructor() {} + canDeactivate(component: EntryComponent, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot, nextState?: RouterStateSnapshot):Observable { + return Observable.create((observer : any) => + { + component.canLeave().subscribe( + result => { + observer.next(result.allowed); + observer.complete(); + }, + error => { + observer.next(false); + observer.complete(); + } + ); + }); + } +} diff --git a/src/applications/content-entries-app/entry/entry-store.service.ts b/src/applications/content-entries-app/entry/entry-store.service.ts index bc5766a904..f53fafbc70 100644 --- a/src/applications/content-entries-app/entry/entry-store.service.ts +++ b/src/applications/content-entries-app/entry/entry-store.service.ts @@ -1,6 +1,6 @@ import { Injectable, OnDestroy, Host } from '@angular/core'; import { ActivatedRoute, Router, NavigationEnd, NavigationStart } from '@angular/router'; -import { Subject } from 'rxjs/Subject'; +import { AppLocalization } from '@kaltura-ng/kaltura-common'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { ISubscription } from 'rxjs/Subscription'; import { Observable } from 'rxjs/Observable'; @@ -29,8 +29,7 @@ export enum ActionTypes EntryPrepareSavingFailed, EntrySavingFailed, EntryDataIsInvalid, - ActiveSectionBusy, - NavigateOut + ActiveSectionBusy } declare type StatusArgs = @@ -74,7 +73,8 @@ export class EntryStore implements OnDestroy { private _browserService : BrowserService, private _entriesStore : EntriesStore, @Host() private _sectionsManager : EntryFormManager, - private _entryRoute: ActivatedRoute) { + private _entryRoute: ActivatedRoute, + private _appLocalization: AppLocalization) { this._sectionsManager.entryStore = this; @@ -123,6 +123,11 @@ export class EntryStore implements OnDestroy { this._entry.complete(); this._browserService.disablePageExitVerification(); + + if (this._saveEntryInvoked) + { + this._entriesStore.reload(true); + } } private _mapSections() : void{ @@ -350,7 +355,7 @@ export class EntryStore implements OnDestroy { public openEntry(entryId : string) { - this._canLeaveWithoutSaving() + this.canLeave() .cancelOnDestroy(this) .subscribe( response => @@ -363,15 +368,15 @@ export class EntryStore implements OnDestroy { ); } - private _canLeaveWithoutSaving() : Observable<{ allowed : boolean}> + public canLeave() : Observable<{ allowed : boolean}> { return Observable.create(observer => { if (this._entryIsDirty) { this._browserService.confirm( { - header: 'Cancel Edit', - message: 'Discard all changes?', + header: this._appLocalization.get('applications.content.entryDetails.captions.cancelEdit'), + message: this._appLocalization.get('applications.content.entryDetails.captions.discard'), accept: () => { observer.next({allowed: true}); observer.complete(); @@ -392,25 +397,7 @@ export class EntryStore implements OnDestroy { public returnToEntries(params : {force? : boolean} = {}) { - this._canLeaveWithoutSaving() - .cancelOnDestroy(this) - .monitor('entry store: return to entries list') - .subscribe( - response => - { - if (response.allowed) - { - if (this._saveEntryInvoked) - { - this._entriesStore.reload(true); - this._saveEntryInvoked = false; - } - - this._state.next({action: ActionTypes.NavigateOut}); - - this._router.navigate(['content/entries']); - } - } - ); + this._router.navigate(['content/entries']); } + } diff --git a/src/applications/content-entries-app/entry/entry.component.ts b/src/applications/content-entries-app/entry/entry.component.ts index 97639958c4..194cfb5d94 100644 --- a/src/applications/content-entries-app/entry/entry.component.ts +++ b/src/applications/content-entries-app/entry/entry.component.ts @@ -19,6 +19,7 @@ import { EntryFormManager } from './entry-form-manager'; import { AreaBlockerMessage, AreaBlockerMessageButton } from '@kaltura-ng/kaltura-ui'; import { EntryFormWidget } from './entry-form-widget'; import { AppLocalization } from '@kaltura-ng/kaltura-common'; +import { Observable } from 'rxjs/Observable'; @Component({ selector: 'kEntry', @@ -234,9 +235,6 @@ export class EntryComponent implements OnInit, OnDestroy { ] }); break; - case ActionTypes.NavigateOut: - this._showLoader = true; - break; default: break; } @@ -296,5 +294,9 @@ export class EntryComponent implements OnInit, OnDestroy { } } + public canLeave(): Observable<{ allowed : boolean}>{ + return this._entryStore.canLeave(); + } + }