Try to fix subscriptions inconsistencies
[oweals/peertube.git] / client / src / app / shared / guards / can-deactivate-guard.service.ts
1 import { Injectable } from '@angular/core'
2 import { CanDeactivate } 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 type CanComponentDeactivateResult = { text?: string, canDeactivate: Observable<boolean> | boolean }
8
9 export interface CanComponentDeactivate {
10   canDeactivate: () => CanComponentDeactivateResult
11 }
12
13 @Injectable()
14 export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {
15   constructor (
16     private confirmService: ConfirmService,
17     private i18n: I18n
18   ) { }
19
20   canDeactivate (component: CanComponentDeactivate) {
21     const result = component.canDeactivate()
22     const text = result.text || this.i18n('All unsaved data will be lost, are you sure you want to leave this page?')
23
24     return result.canDeactivate || this.confirmService.confirm(
25       text,
26       this.i18n('Warning')
27     )
28   }
29
30 }