From 529479f924b1849423fb087f9699f8ecb20610ca Mon Sep 17 00:00:00 2001 From: Dimitri Gilbert Date: Thu, 25 Jan 2018 18:40:23 +0100 Subject: [PATCH] Issue #195 : When uploading, warn when the user quits the page (#222) * issue #195 * fixing missing provider definition * fix styling issue * fix styling issue * removed unecessary code * using angular confirmService instead of window.confirm * gitignore unecessary folders * style fixes * using a generic canDeactivateGuard * fixing lint style --- .../shared/can-deactivate-guard.service.ts | 25 +++++++++++++++++++ .../+video-edit/video-add-routing.module.ts | 4 ++- .../videos/+video-edit/video-add.component.ts | 7 +++++- .../videos/+video-edit/video-add.module.ts | 8 +++--- 4 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 client/src/app/shared/can-deactivate-guard.service.ts diff --git a/client/src/app/shared/can-deactivate-guard.service.ts b/client/src/app/shared/can-deactivate-guard.service.ts new file mode 100644 index 000000000..4239df737 --- /dev/null +++ b/client/src/app/shared/can-deactivate-guard.service.ts @@ -0,0 +1,25 @@ +import { Injectable } from '@angular/core' +import { CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router' +import { Observable } from 'rxjs/Observable' +import { ConfirmService } from '../core' + +export interface CanComponentDeactivate { + canDeactivate: () => Observable | boolean +} + +@Injectable() +export class CanDeactivateGuard implements CanDeactivate { + constructor (private confirmService: ConfirmService) { } + + canDeactivate (component: CanComponentDeactivate, + currentRoute: ActivatedRouteSnapshot, + currentState: RouterStateSnapshot, + nextState: RouterStateSnapshot + ): Observable | boolean { + return component.canDeactivate() || this.confirmService.confirm( + 'All unsaved data will be lost, are you sure you want to leave ?', + 'Unsaved Data' + ) + } + +} diff --git a/client/src/app/videos/+video-edit/video-add-routing.module.ts b/client/src/app/videos/+video-edit/video-add-routing.module.ts index e7a32627c..9541a2bd1 100644 --- a/client/src/app/videos/+video-edit/video-add-routing.module.ts +++ b/client/src/app/videos/+video-edit/video-add-routing.module.ts @@ -4,13 +4,15 @@ import { RouterModule, Routes } from '@angular/router' import { MetaGuard } from '@ngx-meta/core' import { LoginGuard } from '../../core' +import { CanDeactivateGuard } from '../../shared/can-deactivate-guard.service' import { VideoAddComponent } from './video-add.component' const videoAddRoutes: Routes = [ { path: '', component: VideoAddComponent, - canActivate: [ MetaGuard, LoginGuard ] + canActivate: [ MetaGuard, LoginGuard ], + canDeactivate: [CanDeactivateGuard] } ] diff --git a/client/src/app/videos/+video-edit/video-add.component.ts b/client/src/app/videos/+video-edit/video-add.component.ts index 73efd60bf..58eccef48 100644 --- a/client/src/app/videos/+video-edit/video-add.component.ts +++ b/client/src/app/videos/+video-edit/video-add.component.ts @@ -12,6 +12,7 @@ import { ValidatorMessage } from '../../shared/forms/form-validators/validator-m import { populateAsyncUserVideoChannels } from '../../shared/misc/utils' import { VideoEdit } from '../../shared/video/video-edit.model' import { VideoService } from '../../shared/video/video.service' +import { CanComponentDeactivate } from '@app/shared/can-deactivate-guard.service' @Component({ selector: 'my-videos-add', @@ -22,7 +23,7 @@ import { VideoService } from '../../shared/video/video.service' ] }) -export class VideoAddComponent extends FormReactive implements OnInit { +export class VideoAddComponent extends FormReactive implements OnInit, CanComponentDeactivate { @ViewChild('videofileInput') videofileInput isUploadingVideo = false @@ -84,6 +85,10 @@ export class VideoAddComponent extends FormReactive implements OnInit { }) } + canDeactivate () { + return !this.isUploadingVideo + } + fileChange () { this.uploadFirstStep() } diff --git a/client/src/app/videos/+video-edit/video-add.module.ts b/client/src/app/videos/+video-edit/video-add.module.ts index 1efecdf4d..af9696a03 100644 --- a/client/src/app/videos/+video-edit/video-add.module.ts +++ b/client/src/app/videos/+video-edit/video-add.module.ts @@ -4,6 +4,7 @@ import { SharedModule } from '../../shared' import { VideoEditModule } from './shared/video-edit.module' import { VideoAddRoutingModule } from './video-add-routing.module' import { VideoAddComponent } from './video-add.component' +import { CanDeactivateGuard } from '../../shared/can-deactivate-guard.service' @NgModule({ imports: [ @@ -12,15 +13,14 @@ import { VideoAddComponent } from './video-add.component' SharedModule, ProgressBarModule ], - declarations: [ VideoAddComponent ], - exports: [ VideoAddComponent ], - - providers: [ ] + providers: [ + CanDeactivateGuard + ] }) export class VideoAddModule { } -- 2.25.1