aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/shared/guards/can-deactivate-guard.service.ts
blob: 15618f6990a41d1836cc28738476880d1f300516 (plain) (tree)
1
2
3
4
5
6
7


                                                                                            
                                                 

                                         
                                                                                      










                                                                                  





                                                                                                          



     
import { Injectable } from '@angular/core'
import { CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'
import { Observable } from 'rxjs/Observable'
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
  ): Observable<boolean> | boolean {
    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'
    )
  }

}