PeerTube/client/src/app/core/routing/can-deactivate-guard.servic...

33 lines
940 B
TypeScript
Raw Normal View History

2024-02-12 10:50:29 +01:00
import * as debug from 'debug'
2020-06-23 14:10:17 +02:00
import { Observable } from 'rxjs'
import { Injectable } from '@angular/core'
2020-06-23 14:10:17 +02:00
import { ConfirmService } from '@app/core/confirm'
2018-10-18 14:35:31 +02:00
export type CanComponentDeactivateResult = { text?: string, canDeactivate: Observable<boolean> | boolean }
2024-02-12 10:50:29 +01:00
const debugLogger = debug('peertube:routing:CanComponentDeactivate')
export interface CanComponentDeactivate {
2018-10-18 14:35:31 +02:00
canDeactivate: () => CanComponentDeactivateResult
}
@Injectable()
2023-05-23 11:15:00 +02:00
export class CanDeactivateGuard {
constructor (private confirmService: ConfirmService) { }
2018-09-22 20:11:16 +02:00
canDeactivate (component: CanComponentDeactivate) {
const result = component.canDeactivate()
2024-02-12 10:50:29 +01:00
debugLogger('Checking if component can deactivate', result)
const text = result.text || $localize`All unsaved data will be lost, are you sure you want to leave this page?`
return result.canDeactivate || this.confirmService.confirm(
text,
$localize`Warning`
)
}
}