aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core/routing/can-deactivate-guard.service.ts
blob: 638e8e6993264be4f1393c2d7462e9878780850a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { Observable } from 'rxjs'
import { Injectable } from '@angular/core'
import { ConfirmService } from '@app/core/confirm'

export type CanComponentDeactivateResult = { text?: string, canDeactivate: Observable<boolean> | boolean }

export interface CanComponentDeactivate {
  canDeactivate: () => CanComponentDeactivateResult
}

@Injectable()
export class CanDeactivateGuard {

  constructor (private confirmService: ConfirmService) { }

  canDeactivate (component: CanComponentDeactivate) {
    const result = component.canDeactivate()
    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`
    )
  }

}