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

export interface CanComponentDeactivate {
  canDeactivate: () => Observable<boolean> | boolean
}

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

  canDeactivate (component: CanComponentDeactivate,
    currentRoute: ActivatedRouteSnapshot,
    currentState: RouterStateSnapshot,
    nextState: RouterStateSnapshot
  ): Observable<boolean> | boolean {
    return component.canDeactivate() || this.confirmService.confirm(
      'All unsaved data will be lost, are you sure you want to leave ?',
      'Unsaved Data'
    )
  }

}