You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
argos/dmp-frontend/src/app/ui/user-profile/user-profile.component.ts

143 lines
5.0 KiB
TypeScript

import { Component, OnDestroy, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import * as moment from 'moment-timezone';
import { Observable, of } from 'rxjs';
import { map, takeUntil } from 'rxjs/operators';
import { BaseComponent } from '../../core/common/base/base.component';
import { CultureInfo } from '../../core/model/culture-info';
import { DmpModel } from '../../core/model/dmp/dmp';
import { UserListingModel } from '../../core/model/user/user-listing';
import { AuthService } from '../../core/services/auth/auth.service';
import { CultureService } from '../../core/services/culture/culture-service';
import { UserService } from '../../core/services/user/user.service';
const availableLanguages: any[] = require('../../../assets/resources/language.json');
@Component({
selector: 'app-user-profile',
templateUrl: './user-profile.component.html',
styleUrls: ['./user-profile.component.scss'],
})
export class UserProfileComponent extends BaseComponent implements OnInit, OnDestroy {
user: Observable<UserListingModel>;
currentUserId: string;
cultures: Observable<CultureInfo[]>;
timezones: Observable<any[]>;
editMode = false;
languages = availableLanguages;
formGroup: FormGroup;
constructor(
private userService: UserService,
private route: ActivatedRoute,
private router: Router,
private authService: AuthService,
private language: TranslateService,
private cultureService: CultureService,
private translate: TranslateService,
private authentication: AuthService
) { super(); }
ngOnInit() {
this.route.params
.pipe(takeUntil(this._destroyed))
.subscribe((params: Params) => {
this.currentUserId = this.authService.current().id;
const userId = !params['id'] ? 'me' : params['id'];
this.user = this.userService.getUser(userId).pipe(map(result => {
result['additionalinfo'] = JSON.parse(result['additionalinfo']);
this.formGroup = new FormBuilder().group({
language: new FormControl(result['additionalinfo']['language'] ? availableLanguages.filter(x => x.value === result['additionalinfo']['language']['value']).pop() : '', [Validators.required]),
timezone: new FormControl(result['additionalinfo']['timezone'], [Validators.required]),
culture: new FormControl(result['additionalinfo']['culture'], [Validators.required])
});
//this.formGroup.get('language').valueChanges.pipe(takeUntil(this._destroyed)).subscribe(x => { if (x) this.translate.use(x.value) })
this.formGroup.get('timezone').valueChanges
.pipe(takeUntil(this._destroyed))
.subscribe(x => { if (x) { this.timezones = this._filterTimezone(x); } });
this.formGroup.get('culture').valueChanges
.pipe(takeUntil(this._destroyed))
.subscribe(x => { if (x) { this.cultures = this._filterCulture(x); } });
this.formGroup.disable();
return result;
}));
});
}
ngOnDestroy(): void {
}
logout(): void {
this.authentication.logout();
}
getUserRole(dmp: DmpModel) {
if (dmp.creator.id === this.currentUserId) { return this.language.instant('USER-PROFILE.DMPS.CREATOR'); } else if (dmp.associatedUsers.map(x => x.id).indexOf(this.currentUserId) !== -1) { return this.language.instant('USER-PROFILE.DMPS.MEMBER'); }
return '';
}
showAllDmps() {
this.router.navigate(['/plans']);
}
navigateToDmp(dmp: DmpModel) {
this.router.navigate(['/plans/edit/' + dmp.id]);
}
private _filterTimezone(value: string): Observable<any[]> {
if (value && typeof value === 'string') {
const filterValue = value.toLowerCase();
return of(moment.tz.names().filter(option => option.toLowerCase().includes(filterValue)));
} else {
return of(moment.tz.names());
}
}
private _filterCulture(value: string): Observable<any[]> {
if (value && typeof value === 'string') {
const filterValue = value.toLowerCase();
return of(this.cultureService.getCultureValues().filter(option => option.displayName.toLowerCase().includes(filterValue)));
} else {
return of(this.cultureService.getCultureValues());
}
}
displayFn(culture?: CultureInfo): string | undefined {
return culture ? culture.displayName + '-' + culture.nativeName : undefined;
}
save() {
}
public unlock() {
this.editMode = true;
this.formGroup.enable();
}
public lock() {
if (!this.formGroup.valid) { return; }
this.userService.updateUserSettings(this.formGroup.value)
.pipe(takeUntil(this._destroyed))
.subscribe(
x => {
this.editMode = false;
this.translate.use(this.formGroup.value.language);
this.authService.current().culture = this.formGroup.value.culture.name;
this.formGroup.disable();
this.authService.me()
.pipe(takeUntil(this._destroyed))
.subscribe(result => this.router.navigate(['/profile']));
// .subscribe(result => window.location.reload());
},
error => {
console.log(error);
});
}
}