]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/guards/can-deactivate-guard.service.ts
Improve blacklist management
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / guards / can-deactivate-guard.service.ts
1 import { Injectable } from '@angular/core'
2 import { ActivatedRouteSnapshot, CanDeactivate, RouterStateSnapshot } from '@angular/router'
3 import { Observable } from 'rxjs'
4 import { ConfirmService } from '../../core/index'
5 import { I18n } from '@ngx-translate/i18n-polyfill'
6
7 export interface CanComponentDeactivate {
8 canDeactivate: () => { text?: string, canDeactivate: Observable<boolean> | boolean }
9 }
10
11 @Injectable()
12 export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {
13 constructor (
14 private confirmService: ConfirmService,
15 private i18n: I18n
16 ) { }
17
18 canDeactivate (component: CanComponentDeactivate,
19 currentRoute: ActivatedRouteSnapshot,
20 currentState: RouterStateSnapshot,
21 nextState: RouterStateSnapshot
22 ) {
23 const result = component.canDeactivate()
24 const text = result.text || this.i18n('All unsaved data will be lost, are you sure you want to leave this page?')
25
26 return result.canDeactivate || this.confirmService.confirm(
27 text,
28 this.i18n('Warning')
29 )
30 }
31
32 }