aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/shared/can-deactivate-guard.service.ts
blob: 4239df737b9be9ee7e028b3247525c9ace55f1cd (plain) (tree)
























                                                                                            
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'
    )
  }

}