119602: Fix SSR error by making klaroService optional

KlaroService handles cookies which are not applicable during SSR. By
making the service optional, and handling the case when it is not
available, SSR can do its work without throwing NullInjectorErrors.
This commit is contained in:
Andreas Awouters
2025-07-01 09:06:40 +02:00
parent b44f74a20a
commit c6ef2467bc

View File

@@ -1,4 +1,4 @@
import { Component, OnDestroy, OnInit } from '@angular/core'; import { Component, OnDestroy, OnInit, Optional } from '@angular/core';
import { AuthService } from '../../core/auth/auth.service'; import { AuthService } from '../../core/auth/auth.service';
import { import {
AccessibilitySetting, AccessibilitySetting,
@@ -8,7 +8,7 @@ import {
import { BehaviorSubject, distinctUntilChanged, map, Subscription, take } from 'rxjs'; import { BehaviorSubject, distinctUntilChanged, map, Subscription, take } from 'rxjs';
import { NotificationsService } from '../../shared/notifications/notifications.service'; import { NotificationsService } from '../../shared/notifications/notifications.service';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { isEmpty } from 'src/app/shared/empty.util'; import { hasValue, isEmpty } from 'src/app/shared/empty.util';
import { AlertType } from '../../shared/alert/alert-type'; import { AlertType } from '../../shared/alert/alert-type';
import { KlaroService } from '../../shared/cookies/klaro.service'; import { KlaroService } from '../../shared/cookies/klaro.service';
@@ -35,7 +35,7 @@ export class AccessibilitySettingsComponent implements OnInit, OnDestroy {
protected settingsService: AccessibilitySettingsService, protected settingsService: AccessibilitySettingsService,
protected notificationsService: NotificationsService, protected notificationsService: NotificationsService,
protected translateService: TranslateService, protected translateService: TranslateService,
protected klaroService: KlaroService, @Optional() protected klaroService: KlaroService,
) { ) {
} }
@@ -45,11 +45,19 @@ export class AccessibilitySettingsComponent implements OnInit, OnDestroy {
this.subscriptions.push( this.subscriptions.push(
this.authService.isAuthenticated().pipe(distinctUntilChanged()) this.authService.isAuthenticated().pipe(distinctUntilChanged())
.subscribe(val => this.isAuthenticated.next(val)), .subscribe(val => this.isAuthenticated.next(val)),
this.klaroService.getSavedPreferences().pipe(
map(preferences => preferences?.accessibility === true),
distinctUntilChanged(),
).subscribe(val => this.cookieIsAccepted.next(val)),
); );
if (hasValue(this.klaroService)) {
this.subscriptions.push(
this.klaroService.getSavedPreferences().pipe(
map(preferences => preferences?.accessibility === true),
distinctUntilChanged(),
).subscribe(val => this.cookieIsAccepted.next(val))
);
} else {
this.cookieIsAccepted.next(false);
}
} }
ngOnDestroy() { ngOnDestroy() {