Files
maptology/web-angular/src/app/views/user.component.ts
T
Marcus Handte ca6ce85dd1
LocationBasedServices/maptology/master There was a failure building this commit
Switch build system to gradle.
Update wildfly to latest version.
Update android project to latest version.
Add support for Java 11.
2019-07-22 12:36:04 +02:00

137 lines
4.7 KiB
TypeScript

import { Component, OnInit, OnDestroy, ViewChild, HostListener } from '@angular/core';
import { UserState, UserService } from '../services/user.service';
import { BehaviorSubject, Subscription , Observable } from 'rxjs';
import { Router } from '@angular/router';
import { FormErrorCode, FormErrorEncoder } from '../utils/formcodes';
import { LoginFieldComponent } from '../widgets/login-field.component';
import { PasswordFieldComponent } from '../widgets/password-field.component';
import { HttpStatusCode } from '../utils/httpcodes';
import { MatSnackBar, MatDialog, MatSelect, MatOption } from '@angular/material';
import { NgForm } from '@angular/forms';
import { Snackbars } from '../utils/snackbars';
import { User, Email } from '../../api/models';
import { NameFieldComponent } from '../widgets/name-field.component';
import { EmailFieldComponent } from '../widgets/email-field.component';
import { MessageDialogComponent } from '../widgets/message-dialog.component';
import { ResourceService } from '../services/resource.service';
@Component({
selector: 'user',
templateUrl: './user.component.html',
host: { class:'match-parent' },
})
export class UserComponent implements OnInit, OnDestroy {
@ViewChild(NameFieldComponent, { static: false }) nameField: NameFieldComponent;
@ViewChild(MatSelect, { static: false }) emailSelect: MatSelect;
// add email
@ViewChild(EmailFieldComponent, { static: false }) emailField: EmailFieldComponent;
// change passwords
@ViewChild("oldPassword", { static: false }) oldPasswordField: PasswordFieldComponent;
@ViewChild("newPassword", { static: false }) newPasswordField: PasswordFieldComponent;
readonly idle = new BehaviorSubject<boolean>(true);
columns: number = 1;
correspondence: Email;
emails: Email[] = [];
user: BehaviorSubject<User>;
private userSubscription: Subscription;
constructor(public res:ResourceService, private userService: UserService, private snackbar: MatSnackBar, private dialog: MatDialog, private router: Router) {
this.user = userService.user;
}
ngOnInit(): void {
this.updateColumns(window.innerWidth);
this.userSubscription = this.userService.user.subscribe((user)=>{
this.idle.next(user != null);
this.correspondence = this.getCorrespondence();
this.emails = this.getEmails();
});
}
@HostListener('window:resize', ['$event'])
onResize(event:Event) {
this.updateColumns((event.target as Window).innerWidth);
}
updateColumns(width: number): void {
if (width < 800) {
this.columns = 1;
} else {
this.columns = 2;
}
}
ngOnDestroy():void {
this.userSubscription.unsubscribe();
}
private getEmails():Email[] {
if (this.user.getValue() == null) return [];
return this.user.getValue().emails.sort((a, b)=>{
return a.address.localeCompare(b.address);
});
}
private getCorrespondence():Email {
if (this.user.getValue() == null) return null;
for (let email of this.user.getValue().emails) {
if (email.correspondence) return email;
}
return null;
}
onAddEmail() {
if (this.user.getValue() == null) return;
let u = this.user.getValue();
let email = this.emailField.email;
for (var i = 0, s = u.emails.length; i < s; i++) {
if (u.emails[i].address === email) {
Snackbars.showError(this.snackbar, this.res.strings('user_email_registered_already'), Snackbars.DURATION_SHORT);
this.emailField.email = "";
return;
}
}
u.emails.push({address:email, id:0, correspondence: false, creationTime: 0, modificationTime: 0, deleted: false, verified: false});
this.userService.changeUser(u);
}
onChangePassword() {
if (this.user.getValue() == null) return;
if (!this.userService.changePassword(this.oldPasswordField.password, this.newPasswordField.password)) {
this.oldPasswordField.password = "";
this.newPasswordField.password = "";
MessageDialogComponent.open(this.dialog, {title: this.res.strings('user_password_wrong_title'), message: this.res.strings('user_password_wrong_message')});
}
}
onChangeUser() {
if (this.user.getValue() == null) return;
let u = this.user.getValue();
u.name = this.nameField.name;
let id = this.correspondence.id;
for (var i = 0, s = u.emails.length; i < s; i++) {
u.emails[i].correspondence = (u.emails[i].id === id);
}
this.userService.changeUser(u);
}
onDeleteEmail(id:number) {
if (this.user.getValue() == null) return;
let u = this.user.getValue();
for (var i = 0, s = u.emails.length; i < s; i++) {
if (u.emails[i].id === id) {
u.emails[i].deleted = true;
this.userService.changeUser(u);
return;
}
}
}
}