aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/guards/can-deactivate-guard.service.ts
blob: 550dd656eaf5d8ae348f38e89dd35dfa9bbe1fed (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
27
28
import { Injectable } from '@angular/core'
import { ActivatedRouteSnapshot, CanDeactivate, RouterStateSnapshot } from '@angular/router'
import { Observable } from 'rxjs'
import { ConfirmService } from '../../core/index'

export interface CanComponentDeactivate {
  canDeactivate: () => { text?: string, canDeactivate: Observable<boolean> | boolean }
}

@Injectable()
export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {
  constructor (private confirmService: ConfirmService) { }

  canDeactivate (component: CanComponentDeactivate,
    currentRoute: ActivatedRouteSnapshot,
    currentState: RouterStateSnapshot,
    nextState: RouterStateSnapshot
  ) {
    const result = component.canDeactivate()
    const text = result.text || 'All unsaved data will be lost, are you sure you want to leave this page?'

    return result.canDeactivate || this.confirmService.confirm(
      text,
      'Warning'
    )
  }

}