Skip to content

Commit

Permalink
fix: verify leave entry details when data is not saved
Browse files Browse the repository at this point in the history
  • Loading branch information
amirch1 committed Jul 17, 2017
1 parent 7ca9be2 commit a0bd1b1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -90,7 +91,8 @@ import { SharedComponentsList } from './shared/shared-components-list';
BulkChangeOwnerService,
BulkRemoveCategoriesService,
BulkDeleteService,
BulkDownloadService
BulkDownloadService,
EntryCanDeactivate
],
})
export class ContentEntriesAppModule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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<EntryComponent> {
constructor() {}
canDeactivate(component: EntryComponent, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot, nextState?: RouterStateSnapshot):Observable<boolean> {
return Observable.create((observer : any) =>
{
component.canLeave().subscribe(
result => {
observer.next(result.allowed);
observer.complete();
},
error => {
observer.next(false);
observer.complete();
}
);
});
}
}
43 changes: 15 additions & 28 deletions src/applications/content-entries-app/entry/entry-store.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -29,8 +29,7 @@ export enum ActionTypes
EntryPrepareSavingFailed,
EntrySavingFailed,
EntryDataIsInvalid,
ActiveSectionBusy,
NavigateOut
ActiveSectionBusy
}

declare type StatusArgs =
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -350,7 +355,7 @@ export class EntryStore implements OnDestroy {

public openEntry(entryId : string)
{
this._canLeaveWithoutSaving()
this.canLeave()
.cancelOnDestroy(this)
.subscribe(
response =>
Expand All @@ -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();
Expand All @@ -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']);
}

}
8 changes: 5 additions & 3 deletions src/applications/content-entries-app/entry/entry.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -234,9 +235,6 @@ export class EntryComponent implements OnInit, OnDestroy {
]
});
break;
case ActionTypes.NavigateOut:
this._showLoader = true;
break;
default:
break;
}
Expand Down Expand Up @@ -296,5 +294,9 @@ export class EntryComponent implements OnInit, OnDestroy {
}
}

public canLeave(): Observable<{ allowed : boolean}>{
return this._entryStore.canLeave();
}

}

0 comments on commit a0bd1b1

Please sign in to comment.