2020-07-02 10:25:06 +02:00
|
|
|
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
|
2020-02-18 15:47:58 +01:00
|
|
|
import { Router } from '@angular/router';
|
|
|
|
import { AuthService } from '@app/core/services/auth/auth.service';
|
|
|
|
import { LanguageService } from '@app/core/services/language/language.service';
|
2023-11-24 17:42:23 +01:00
|
|
|
import { UserServiceOld } from '@app/core/services/user/user.service-old';
|
2020-02-18 15:47:58 +01:00
|
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
import { BaseComponent } from '@common/base/base.component';
|
2023-02-11 13:38:13 +01:00
|
|
|
import { ConfigurationService } from '@app/core/services/configuration/configuration.service';
|
2023-11-29 14:26:40 +01:00
|
|
|
import { MatButtonToggleChange } from '@angular/material/button-toggle';
|
|
|
|
import { UserService } from '@app/core/services/user/user.service';
|
2020-02-18 15:47:58 +01:00
|
|
|
|
|
|
|
@Component({
|
2020-07-02 10:25:06 +02:00
|
|
|
selector: 'app-language',
|
|
|
|
templateUrl: './language.component.html',
|
|
|
|
styleUrls: ['./language.component.scss']
|
2020-02-18 15:47:58 +01:00
|
|
|
})
|
|
|
|
export class LanguageComponent extends BaseComponent implements OnInit {
|
|
|
|
|
2020-07-02 10:25:06 +02:00
|
|
|
@Output() languageChange: EventEmitter<any> = new EventEmitter();
|
2023-11-29 14:26:40 +01:00
|
|
|
languages: string[] = [];
|
2020-02-18 15:47:58 +01:00
|
|
|
|
2020-07-02 10:25:06 +02:00
|
|
|
constructor(
|
|
|
|
private router: Router,
|
|
|
|
private authentication: AuthService,
|
2023-11-29 14:26:40 +01:00
|
|
|
private userService: UserService,
|
|
|
|
private languageService: LanguageService
|
2023-02-11 13:38:13 +01:00
|
|
|
) {
|
|
|
|
super();
|
2023-11-29 14:26:40 +01:00
|
|
|
this.languages = this.languageService.getAvailableLanguagesCodes();
|
2023-02-11 13:38:13 +01:00
|
|
|
}
|
2020-02-18 15:47:58 +01:00
|
|
|
|
2020-07-02 10:25:06 +02:00
|
|
|
ngOnInit() {
|
2023-11-29 14:26:40 +01:00
|
|
|
this.languageChange.emit(this.getCurrentLanguage())
|
2020-07-02 10:25:06 +02:00
|
|
|
}
|
2020-02-18 15:47:58 +01:00
|
|
|
|
2020-07-02 10:25:06 +02:00
|
|
|
public isAuthenticated(): boolean {
|
2023-10-11 16:53:12 +02:00
|
|
|
return this.authentication.currentAccountIsAuthenticated();
|
2020-07-02 10:25:06 +02:00
|
|
|
}
|
2020-02-18 15:47:58 +01:00
|
|
|
|
2023-11-29 14:26:40 +01:00
|
|
|
public getCurrentLanguage(): string {
|
|
|
|
const lang = this.languages.find(lang => lang === this.languageService.getCurrentLanguage());
|
2020-07-02 10:25:06 +02:00
|
|
|
return lang;
|
|
|
|
}
|
2020-02-18 15:47:58 +01:00
|
|
|
|
2023-11-29 14:26:40 +01:00
|
|
|
onLanguageSelected(selectedLanguage: MatButtonToggleChange) {
|
2020-07-02 10:25:06 +02:00
|
|
|
if (this.isAuthenticated()) {
|
|
|
|
const langMap = new Map<string, string>();
|
|
|
|
langMap.set('language', selectedLanguage.value);
|
2023-11-29 14:26:40 +01:00
|
|
|
//TODO: refactor - save language selection to user profile
|
|
|
|
// this.userService.updateUserSettings({ language: this.languages.find(lang => lang === selectedLanguage.value) })
|
|
|
|
// .pipe(takeUntil(this._destroyed))
|
|
|
|
// .subscribe((response) => {
|
|
|
|
// this.languageService.changeLanguage(selectedLanguage.value);
|
|
|
|
// this.authentication.refresh()
|
|
|
|
// .pipe(takeUntil(this._destroyed))
|
|
|
|
// .subscribe(innerResponse => { this.router.navigateByUrl(this.router.url); });
|
|
|
|
// },
|
|
|
|
// error => {
|
|
|
|
// console.log(error);
|
|
|
|
// });
|
|
|
|
this.languageService.changeLanguage(selectedLanguage.value);
|
|
|
|
this.router.navigateByUrl(this.router.url);
|
2020-07-02 10:25:06 +02:00
|
|
|
} else {
|
|
|
|
this.languageService.changeLanguage(selectedLanguage.value);
|
|
|
|
this.router.navigateByUrl(this.router.url);
|
|
|
|
}
|
|
|
|
this.languageChange.emit(selectedLanguage.value);
|
2020-02-18 15:47:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|