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

70.96% Statements 22/31
33.33% Branches 5/15
57.14% Functions 4/7
68.96% Lines 20/29

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 922x 2x                     2x   2x                             2x 1x   1x 1x     1x 1x 1x 1x   1x       1x               1x 1x     1x 1x 1x 1x                                                              
import { DOCUMENT } from '@angular/common';
import {
  ChangeDetectionStrategy,
  Component,
  Inject,
  OnInit,
  Renderer2,
  TransferState,
  afterNextRender,
  signal,
} 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
 *
 * The banner markup is always rendered (also during SSR) but kept hidden by default. It is revealed via the
 * 'show-cookie-banner' root class, which is set pre-hydration by an inline script in index.html when no consent
 * cookie exists, so first-time visitors see the banner at FCP instead of waiting for Angular to bootstrap.
 */
@Component({
  selector: 'ish-cookies-banner',
  standalone: false,
  templateUrl: './cookies-banner.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CookiesBannerComponent implements OnInit {
  transitionBanner: string = undefined;
  // The banner buttons require client-side JS, so they stay disabled until the component hydrates.
  protected interactive = signal(false);
  private cookiesConsentFor: string[] = undefined;
 
  constructor(
    private transferState: TransferState,
    private cookiesService: CookiesService,
    private renderer: Renderer2,
    @Inject(DOCUMENT) private document: Document
  ) {
    afterNextRender(() => this.interactive.set(true));
  }
 
  ngOnInit() {
    this.reconcileBannerVisibility();
  }
 
  /**
   * Reconcile the pre-hydration reveal with the authoritative consent version:
   * show if consent is missing or outdated, otherwise hide.
   */
  private reconcileBannerVisibility() {
    Eif (!SSR) {
      const cookieConsentSettings = JSON.parse(
        this.cookiesService.get('cookieConsent') || 'null'
      ) as CookieConsentSettings;
      const cookieConsentVersion = this.transferState.get<number>(COOKIE_CONSENT_VERSION, 1);
      const showBanner = !cookieConsentSettings || cookieConsentSettings.version < cookieConsentVersion;
      if (showBanner) {
        this.renderer.addClass(this.document.documentElement, 'show-cookie-banner');
      } else E{
        this.renderer.removeClass(this.document.documentElement, 'show-cookie-banner');
      }
    }
  }
 
  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);
      }
    }
  }
}