sh_discord_bot/kdb-web/src/app/app.component.ts

84 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-02-21 22:44:42 +01:00
import { Component, OnDestroy, OnInit } from "@angular/core";
2023-02-16 21:38:29 +01:00
import { TranslateService } from "@ngx-translate/core";
import { PrimeNGConfig } from "primeng/api";
import { AuthService } from "./services/auth/auth.service";
import { SocketService } from "./services/socket/socket.service";
import { ThemeService } from "./services/theme/theme.service";
2023-02-21 22:44:42 +01:00
import { Subject } from "rxjs";
import { Themes } from "./models/view/themes.enum";
import { takeUntil } from "rxjs/operators";
2022-10-16 00:13:03 +02:00
@Component({
2023-02-16 21:38:29 +01:00
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"]
2022-10-16 00:13:03 +02:00
})
2023-02-21 22:44:42 +01:00
export class AppComponent implements OnInit, OnDestroy {
2022-10-16 00:13:03 +02:00
2023-02-21 22:44:42 +01:00
themeName: string = Themes.Default;
sidebarWidth: string = '175px';
isLoggedIn: boolean = false;
2023-02-21 22:44:42 +01:00
private unsubscriber = new Subject<void>();
2022-10-16 00:13:03 +02:00
constructor(
2022-10-16 16:01:37 +02:00
private authService: AuthService,
private themeService: ThemeService,
private socket: SocketService,
private translateService: TranslateService,
2023-02-16 21:38:29 +01:00
private config: PrimeNGConfig,
) {
2023-02-21 22:44:42 +01:00
this.themeService.sidebarWidth$.pipe(
takeUntil(this.unsubscriber)
).subscribe(value => {
this.sidebarWidth = value;
});
2023-02-21 22:44:42 +01:00
this.themeService.themeName$.pipe(
takeUntil(this.unsubscriber)
).subscribe(value => {
this.themeName = value;
});
2023-02-21 22:44:42 +01:00
this.authService.isLoggedIn$.pipe(
takeUntil(this.unsubscriber)
).subscribe(value => {
2022-10-16 16:01:37 +02:00
this.isLoggedIn = value;
});
}
ngOnInit(): void {
2023-02-16 21:38:29 +01:00
this.translateService.setDefaultLang("en");
2022-10-16 00:13:03 +02:00
this.themeService.loadTheme();
2022-10-16 16:01:37 +02:00
this.socket.startSocket();
2022-10-16 00:13:03 +02:00
}
2023-02-21 22:44:42 +01:00
ngOnDestroy() {
this.unsubscriber.next();
this.unsubscriber.unsubscribe();
}
loadLang(): void {
let lang = localStorage.getItem(`default_lang`);
if (!lang) {
2023-02-16 21:38:29 +01:00
lang = "en";
this.setLang(lang);
}
this.translate(lang);
}
setLang(lang: string): void {
localStorage.setItem(`default_lang`, lang);
}
translate(lang: string) {
this.translateService.use(lang);
2023-02-16 21:38:29 +01:00
this.translateService.get("primeng").subscribe(res => this.config.setTranslation(res));
}
setSideWidth($event: any): void {
this.themeService.setSideWidth($event);
}
2022-10-16 00:13:03 +02:00
}