All files / src/app/shell/header/language-switch language-switch.component.ts

85.71% Statements 18/21
60% Branches 3/5
80% Functions 4/5
85% Lines 17/20

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 592x 2x 2x 2x   2x 2x   2x                   2x 4x       4x         4x 4x 4x 4x       4x             4x   1x                            
import { LocationStrategy } from '@angular/common';
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
import { Observable, combineLatest } from 'rxjs';
import { filter, map } from 'rxjs/operators';
 
import { AppFacade } from 'ish-core/facades/app.facade';
import { FeatureToggleService } from 'ish-core/feature-toggle.module';
import { DeviceType } from 'ish-core/models/viewtype/viewtype.types';
import { CookiesService } from 'ish-core/utils/cookies/cookies.service';
 
/**
 * The Language Switch Component shows a dropdown allowing to switch the current language/locale.
 */
@Component({
  selector: 'ish-language-switch',
  templateUrl: './language-switch.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LanguageSwitchComponent implements OnInit {
  @Input() deviceType: DeviceType = 'desktop';
  /**
   * determines position of dropbox
   */
  @Input() placement: '' | 'up' = '';
 
  languageSwitchData$: Observable<{ locale: string; availableLocales: string[] }>;
 
  constructor(
    private appFacade: AppFacade,
    private cookiesService: CookiesService,
    private featureToggleService: FeatureToggleService,
    public location: LocationStrategy
  ) {}
 
  ngOnInit() {
    this.languageSwitchData$ = combineLatest([
      this.appFacade.currentLocale$,
      this.appFacade.availableLocales$,
      this.appFacade.serverConfigurationLoaded$,
    ]).pipe(
      filter(
        ([locale, availableLocales, serverConfigLoaded]) =>
          serverConfigLoaded && !!locale && availableLocales?.length > 1
      ),
      map(([locale, availableLocales]) => ({ locale, availableLocales }))
    );
  }
 
  setLocaleCookie(locale: string) {
    if (this.featureToggleService.enabled('saveLanguageSelection')) {
      this.cookiesService.put('preferredLocale', locale, {
        expires: new Date(new Date().setFullYear(new Date().getFullYear() + 1)),
        path: '/',
      });
      return true;
    }
  }
}