Skip to content

Commit

Permalink
fix: cookie page guard should only use NgModal on browser side (#1148)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eisie96 authored May 5, 2022
1 parent 0fd617a commit 9a26d87
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/app/pages/cookies/cookies-page.guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { CanActivate, NavigationEnd, Router } from '@angular/router';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { filter, first } from 'rxjs/operators';
Expand All @@ -9,32 +10,38 @@ import { CookiesModalComponent } from './cookies-modal/cookies-modal.component';
export class CookiesPageGuard implements CanActivate {
private currentDialog: NgbModalRef;

constructor(private modalService: NgbModal, private router: Router) {}
constructor(
@Inject(PLATFORM_ID) private platformId: string,
private modalService: NgbModal,
private router: Router
) {}

async canActivate() {
this.currentDialog = this.modalService.open(CookiesModalComponent, {
centered: true,
size: 'lg',
backdrop: 'static',
});

const cookiesModalComponent = this.currentDialog.componentInstance as CookiesModalComponent;
if (isPlatformBrowser(this.platformId)) {
this.currentDialog = this.modalService.open(CookiesModalComponent, {
centered: true,
size: 'lg',
backdrop: 'static',
});

// dialog closed
cookiesModalComponent.closeModal.pipe(first()).subscribe(() => {
this.currentDialog.dismiss();
});
const cookiesModalComponent = this.currentDialog.componentInstance as CookiesModalComponent;

// navigated away with link on dialog
this.router.events
.pipe(
filter(event => event instanceof NavigationEnd),
first()
)
.subscribe(() => {
// dialog closed
cookiesModalComponent.closeModal.pipe(first()).subscribe(() => {
this.currentDialog.dismiss();
});

return false;
// navigated away with link on dialog
this.router.events
.pipe(
filter(event => event instanceof NavigationEnd),
first()
)
.subscribe(() => {
this.currentDialog.dismiss();
});
return false;
}
return true;
}
}

0 comments on commit 9a26d87

Please sign in to comment.