| 1 | /* |
| 2 | * Copyright (c) 2024 sovity GmbH |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Apache License, Version 2.0 which is available at |
| 6 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | * |
| 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Contributors: |
| 11 | * sovity GmbH - initial implementation |
| 12 | */ |
| 13 | import {Component, Inject, OnDestroy, OnInit} from '@angular/core'; |
| 14 | import {Subject, interval} from 'rxjs'; |
| 15 | import {Store} from '@ngxs/store'; |
| 16 | import {APP_CONFIG, AppConfig} from 'src/app/core/services/config/app-config'; |
| 17 | import {GlobalState} from './core/global-state/global-state'; |
| 18 | import {RefreshUserInfo} from './core/global-state/global-state-actions'; |
| 19 | import {GlobalStateImpl} from './core/global-state/global-state-impl'; |
| 20 | |
| 21 | @Component({ |
| 22 | selector: 'app-root', |
| 23 | templateUrl: './app.component.html', |
| 24 | }) |
| 25 | export class AppComponent implements OnInit, OnDestroy { |
| 26 | state!: GlobalState; |
| 27 | favicon: HTMLLinkElement | null = document.querySelector("link[rel*='icon']"); |
| 28 | |
| 29 | private ngOnDestroy$ = new Subject(); |
| 30 | |
| 31 | constructor( |
| 32 | @Inject(APP_CONFIG) public appConfig: AppConfig, |
| 33 | private store: Store, |
| 34 | ) {} |
| 35 | |
| 36 | ngOnInit() { |
| 37 | this.startListeningToGlobalState(); |
| 38 | this.startPollingUserInfo(); |
| 39 | this.setTheme(); |
| 40 | } |
| 41 | |
| 42 | private setTheme() { |
| 43 | if (!this.favicon) { |
| 44 | this.favicon = document.createElement('link'); |
| 45 | this.favicon.rel = 'icon'; |
| 46 | document.head.appendChild(this.favicon); |
| 47 | } |
| 48 | this.favicon.href = this.appConfig.brandFaviconSrc; |
| 49 | window.document.body.classList.add(this.appConfig.theme); |
| 50 | } |
| 51 | |
| 52 | private startListeningToGlobalState() { |
| 53 | this.store.select<GlobalState>(GlobalStateImpl).subscribe((state) => { |
| 54 | this.state = state; |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | ngOnDestroy(): void { |
| 59 | this.ngOnDestroy$.next(null); |
| 60 | this.ngOnDestroy$.complete(); |
| 61 | } |
| 62 | |
| 63 | private startPollingUserInfo() { |
| 64 | this.store.dispatch(RefreshUserInfo); |
| 65 | interval(3000).subscribe(() => this.store.dispatch(RefreshUserInfo)); |
| 66 | } |
| 67 | } |