Search code examples
angularguard

canDeactivate doesn't work with Angular material modal window


I successfully built canDeactivate guard and it is working fine with normal confirm but I want to implement it with angular material dialog and here I face some issues.

It is my Guard:

@Injectable()
export class CanDeactivateGuard implements CanDeactivate<CreateQuoteComponent> {
  constructor(
    public dialog: MatDialog,
  ){

  }
  canDeactivate(component: CreateQuoteComponent): boolean {
    if (!component.changesSaved) {
      return component.confirmDialog();
      //return confirm('You have not saved your current work. Do you want to proceed and discard your changes?');
    }
    return true;
  }
}

It is a function in my component:

confirmDialog(): boolean {
    const message = 'You have not saved your current work. Do you want to proceed and discard?';
    const data = { 'message': message, 'toShowCancel': true, 'buttonYesCaption': 'Yes', 'buttonNoCaption': 'No' };
    const dialogRef = this.dialog.open(YesNoComponent, {
      width: '600px',
      height: '250px',
      data: data
    });
    dialogRef.afterClosed().subscribe(result=>{
      this.dialogRef1=result;
      return this.dialogRef1;
    });
    return this.dialogRef1;
  }

 I defined a boolean variable dialogRef1 at the top of the component.

It is a part of the component with a modal window:

onCloseClick(){
    this.dialogRef.close(false);
  }

  onSubmit(){
    this.dialogRef.close(true);
  }

I have tried to implement this example: How to send return value to CanDeactivate Guard after close the mat-dialog | Angular CanDeactivate Guard | Angular Material Dialog

but it doesn't work for me or maybe I have done it wrong. Thanks in advance!


Solution

  • Your are returning a variable value which is being set by an Observable [i.e dialogRef.afterClosed()] which will be decided by the user. You should do the following:

    First, change the return type of canDeactivate as Observable<boolean> like this:

    @Injectable()
    export class CanDeactivateGuard implements CanDeactivate<CreateQuoteComponent> {
      constructor(
        public dialog: MatDialog,
      ){
    
      }
      canDeactivate(component: CreateQuoteComponent): Observable<boolean> {
        if (!component.changesSaved) {
          return component.confirmDialog();      
        }
        //please import 'of' form 'rxjs/operators'
        return of(true);
      }
    }
    

    Now lets change component.confirmDialog method to return the dialogRef.afterClosed() observable like this:

    confirmDialog(): Observable<boolean> {
        const message = 'You have not saved your current work. Do you want to proceed and discard?';
        const data = { 'message': message, 'toShowCancel': true, 'buttonYesCaption': 'Yes', 'buttonNoCaption': 'No' };
        const dialogRef = this.dialog.open(YesNoComponent, {
          width: '600px',
          height: '250px',
          data: data
        });
        return dialogRef.afterClosed();
      }
    

    Hope it helps.