import {Component, Inject, OnInit} from '@angular/core'; import {MAT_DIALOG_DATA as MAT_DIALOG_DATA, MatDialogRef as MatDialogRef} from "@angular/material/dialog"; import {UserInfo, UsersService} from "../../../../api"; import {MatSnackBar as MatSnackBar} from "@angular/material/snack-bar"; import {AbstractControl, UntypedFormControl, ValidationErrors, Validators} from "@angular/forms"; import {EditData} from "./edit-data"; @Component({ selector: 'app-edit', templateUrl: './edit.component.html', styleUrls: ['./edit.component.css'] }) export class EditComponent implements OnInit { username: string = ""; email: string = ""; password: string = ""; duplicatePassword: string = ""; admin: boolean = false; hide: boolean = true; pending: boolean = false; addMode: boolean = false; adminUpdateDisabled: boolean = false; usernameFormControl = new UntypedFormControl('', [Validators.required, Validators.minLength(3), Validators.maxLength(32)]) emailFormControl = new UntypedFormControl('', [Validators.required, Validators.email]) passwordEditControl = new UntypedFormControl('', [ (control: AbstractControl): ValidationErrors | null => { return control.value.length > 0 && control.value.length < 6 ? {minlength: {value: control.value}} : null }, ]) passwordAddControl = new UntypedFormControl('', [Validators.required, Validators.minLength(6)]) passwordDuplicateEditControl = new UntypedFormControl('', [ (control: AbstractControl): ValidationErrors | null => { return control.value != this.password ? {duplicate: {value: control.value}} : null } ]) passwordDuplicateAddControl = new UntypedFormControl('', [ Validators.required, Validators.minLength(6), (control: AbstractControl): ValidationErrors | null => { return control.value != this.password ? {duplicate: {value: control.value}} : null } ]) constructor( public dialogRef: MatDialogRef, private userService: UsersService, private snackbar: MatSnackBar, @Inject(MAT_DIALOG_DATA) public user: EditData ) { } ngOnInit(): void { if(this.user) { this.username = this.user.username; this.email = this.user.email; this.admin = this.user.admin; this.addMode = true; this.adminUpdateDisabled = this.user.lastadmin && this.admin; console.log("Admin Mode Update Disabled: " + this.adminUpdateDisabled) } } onSubmitChanged() { this.pending = true; this.userService.usersUsernamePost(this.username, { admin: this.admin, password: this.password, email: this.email }).subscribe({ next: response => { if(response.status == "success") { const updatedUser: UserInfo = { username: this.username, admin: this.admin, email: this.email }; this.dialogRef.close(updatedUser); this.snackbar.open("Edit User was successfull", "", {duration: 2000}); this.pending = false; } }, error: err => { if(err.status == 403) { if(err.status == 403) { this.snackbar.open("You have no authorization to perform this Operation!","", {duration: 2000}) this.pending = false; } else if(err.status == 404) { this.snackbar.open("The user could not be found", "", {duration: 2000}); this.pending = false; } } } }) } onClose() { this.dialogRef.close(); } onAddUser() { this.pending = true; this.userService.usersPut({ username: this.username, password: this.password, email: this.email, admin: this.admin }).subscribe({ next: response => { if(response.status == "success") { const addedUser: UserInfo = { username: this.username, email: this.email, admin: this.admin }; this.dialogRef.close(addedUser); this.snackbar.open("User has been added", "", {duration: 2000}) this.pending = false } }, error: err => { if(err.status == 403) { this.snackbar.open("You have no authorization to perform this Operation!","", {duration: 2000}) this.pending = false; }else if(err.status == 409) { this.snackbar.open("User already exist!", "", {duration: 2000}) this.pending = false; } } }) } getUsernameErrorMessage() : string { if(this.usernameFormControl.hasError('requiered')) { return "Please enter a username!"; } else if(this.usernameFormControl.hasError('minlength')) { return "The username is to short!" } else if(this.usernameFormControl.hasError("maxlength")) { return "The username is to long!" } return ""; } getEmailErrorMessage(): string { if(this.emailFormControl.hasError('requiered')) { return "Please enter a email!"; } else if(this.emailFormControl.hasError('email')) { return "Please enter a valid Email!" } return ""; } getPasswordErrorMessage(): string { if(this.user) { //Add mode if(this.passwordAddControl.hasError('requiered')) { return "Please enter a password!" } else if(this.passwordAddControl.hasError('minlength')) { return "The password needs to contain at least 6 chars!" } return ""; } else { return ""; } } getPasswordDuplicateErrorMessage(): string { if(this.user) { //Add mode if(this.passwordDuplicateAddControl.hasError('requiered')) { return "Please enter a password!" } else if(this.passwordDuplicateAddControl.hasError('minlength')) { return "The password needs to contain at least 6 chars!" } else if(this.passwordDuplicateAddControl.hasError('duplicate')) { return "The passwords arent identical!" } return ""; } else { if(this.passwordDuplicateEditControl.hasError('requiered')) { return "Please enter a password!" } else if(this.passwordDuplicateEditControl.hasError('minlength')) { return "The password needs to contain at least 6 chars!" } else if(this.passwordDuplicateAddControl.hasError('duplicate')) { return "The passwords arent identical!" } return ""; } } }