113 lines
3.7 KiB
TypeScript
113 lines
3.7 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
import { ProfileModel, UserService } from '@services/user.service';
|
|
import { PermissionsService } from '@services/permissions.service';
|
|
import { LanguageService } from '@i18n/language.service';
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
import { UserControllerService } from '@redaction/red-ui-http';
|
|
import { AppConfigKey, AppConfigService } from '@app-config/app-config.service';
|
|
import { DomSanitizer } from '@angular/platform-browser';
|
|
|
|
@Component({
|
|
selector: 'redaction-user-profile-screen',
|
|
templateUrl: './user-profile-screen.component.html',
|
|
styleUrls: ['./user-profile-screen.component.scss']
|
|
})
|
|
export class UserProfileScreenComponent implements OnInit {
|
|
viewReady = false;
|
|
formGroup: FormGroup;
|
|
changePasswordUrl: any;
|
|
|
|
private _profileModel: ProfileModel;
|
|
|
|
constructor(
|
|
readonly permissionsService: PermissionsService,
|
|
private readonly _formBuilder: FormBuilder,
|
|
private readonly _userService: UserService,
|
|
private readonly _appConfigService: AppConfigService,
|
|
private readonly _userControllerService: UserControllerService,
|
|
private readonly _languageService: LanguageService,
|
|
private readonly _domSanitizer: DomSanitizer,
|
|
private readonly _translateService: TranslateService
|
|
) {
|
|
this.formGroup = this._formBuilder.group({
|
|
email: [undefined, [Validators.required, Validators.email]],
|
|
firstName: [undefined],
|
|
lastName: [undefined],
|
|
language: [undefined]
|
|
});
|
|
|
|
this.changePasswordUrl = this._domSanitizer.bypassSecurityTrustResourceUrl(
|
|
this._appConfigService.getConfig(AppConfigKey.OAUTH_URL) + '/account/password'
|
|
);
|
|
}
|
|
|
|
get languageChanged(): boolean {
|
|
return this._profileModel['language'] !== this.formGroup.get('language').value;
|
|
}
|
|
|
|
get profileChanged(): boolean {
|
|
const keys = Object.keys(this.formGroup.getRawValue());
|
|
keys.splice(keys.indexOf('language'), 1);
|
|
|
|
for (const key of keys) {
|
|
if (this._profileModel[key] !== this.formGroup.get(key).value) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
get languages(): string[] {
|
|
return this._translateService.langs;
|
|
}
|
|
|
|
ngOnInit() {
|
|
this._initializeForm();
|
|
}
|
|
|
|
async save(): Promise<void> {
|
|
this.viewReady = false;
|
|
|
|
if (this.languageChanged) {
|
|
await this._languageService.changeLanguage(this.formGroup.get('language').value);
|
|
}
|
|
|
|
if (this.profileChanged) {
|
|
const value = this.formGroup.value as ProfileModel;
|
|
delete value.language;
|
|
|
|
await this._userControllerService
|
|
.updateMyProfile({
|
|
...value
|
|
})
|
|
.toPromise();
|
|
|
|
await this._userService.loadCurrentUser();
|
|
await this._userService.loadAllUsers();
|
|
}
|
|
|
|
this._initializeForm();
|
|
}
|
|
|
|
private _initializeForm(): void {
|
|
try {
|
|
this._profileModel = {
|
|
email: this._userService.user.email,
|
|
firstName: this._userService.user.firstName,
|
|
lastName: this._userService.user.lastName,
|
|
language: this._languageService.currentLanguage
|
|
};
|
|
if (this._userService.user.email) {
|
|
// disable email if it's already set
|
|
this.formGroup.get('email').disable();
|
|
}
|
|
this.formGroup.patchValue(this._profileModel, { emitEvent: false });
|
|
} catch (e) {
|
|
} finally {
|
|
this.viewReady = true;
|
|
}
|
|
}
|
|
}
|