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

66.66% Statements 16/24
33.33% Branches 5/15
50% Functions 3/6
65.21% Lines 15/23

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 66 67 68 69 70 712x   2x   2x                     2x 1x 1x 1x     1x 1x       1x                 1x 1x     1x 1x 1x                                                          
import { ChangeDetectionStrategy, Component, OnInit, TransferState } from '@angular/core';
 
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',
  standalone: false,
  templateUrl: './cookies-banner.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
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() {
    Eif (!SSR) {
      const cookieConsentSettings = JSON.parse(
        this.cookiesService.get('cookieConsent') || 'null'
      ) as CookieConsentSettings;
      const cookieConsentVersion = this.transferState.get<number>(COOKIE_CONSENT_VERSION, 1);
      Eif (!cookieConsentSettings || cookieConsentSettings.version < cookieConsentVersion) {
        this.showBanner = true;
      }
    }
  }
 
  acceptAll() {
    this.transitionBanner = 'bottom-out';
    this.cookiesConsentFor = undefined;
  }
 
  acceptOnlyRequired() {
    this.transitionBanner = 'bottom-out';
    this.cookiesConsentFor = ['required'];
  }
 
  setCookiesConsent(event: TransitionEvent): void {
    if (
      event.target === event.currentTarget &&
      event.propertyName === 'transform' &&
      this.transitionBanner === 'bottom-out'
    ) {
      if (this.cookiesConsentFor === undefined) {
        this.cookiesService.setCookiesConsentForAll();
      } else {
        this.cookiesService.setCookiesConsentFor(this.cookiesConsentFor);
      }
    }
  }
}