All files / src/app/shell/application/cookies-banner cookies-banner.component.ts

73.91% Statements 17/23
64.7% Branches 11/17
66.66% Functions 4/6
72.72% Lines 16/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65  2x   2x 2x   2x                     2x 1x 1x 1x   1x     1x                 1x 1x     1x 1x 1x                             1x                  
import { AnimationEvent } from '@angular/animations';
import { ChangeDetectionStrategy, Component, OnInit, TransferState } from '@angular/core';
 
import bottomOutAnimation from 'ish-core/animations/bottom-out.animation';
import { COOKIE_CONSENT_VERSION } from 'ish-core/configurations/state-keys';
import { CookieConsentSettings } from 'ish-core/models/cookies/cookies.model';
import { CookiesService } from 'ish-core/utils/cookies/cookies.service';
 
/**
 * Cookies Banner Component
 */
@Component({
  selector: 'ish-cookies-banner',
  templateUrl: './cookies-banner.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  animations: [bottomOutAnimation()],
})
export class CookiesBannerComponent implements OnInit {
  showBanner = false;
  transitionBanner: string = undefined;
  private cookiesConsentFor: string[] = undefined;
 
  constructor(private transferState: TransferState, private cookiesService: CookiesService) {}
 
  ngOnInit() {
    this.showBannerIfNecessary();
  }
 
  /**
   * show banner if:
   * - consent not yet given
   * - consent outdated
   */
  private showBannerIfNecessary() {
    if (!SSR) {
      const cookieConsentSettings = JSON.parse(
        this.cookiesService.get('cookieConsent') || 'null'
      ) as CookieConsentSettings;
      const cookieConsentVersion = this.transferState.get<number>(COOKIE_CONSENT_VERSION, 1);
      if (!cookieConsentSettings || cookieConsentSettings.version < cookieConsentVersion) {
        this.showBanner = true;
      }
    }
  }
 
  acceptAll() {
    this.transitionBanner = 'bottom-out';
  }
 
  acceptOnlyRequired() {
    this.transitionBanner = 'bottom-out';
    this.cookiesConsentFor = ['required'];
  }
 
  setCookiesConsent(event: AnimationEvent): void {
    Iif (event.toState === this.transitionBanner) {
      if (this.cookiesConsentFor === undefined) {
        this.cookiesService.setCookiesConsentForAll();
      } else {
        this.cookiesService.setCookiesConsentFor(this.cookiesConsentFor);
      }
    }
  }
}