import { Component, OnInit } from '@angular/core'; import {AbstractControl, UntypedFormControl, ValidationErrors, Validators} from "@angular/forms"; import {AccountService} from "../../../../api"; import {MatSnackBar} from "@angular/material/snack-bar"; @Component({ selector: 'app-change-password', templateUrl: './change-password.component.html', styleUrls: ['./change-password.component.css'] }) export class ChangePasswordComponent implements OnInit { currentPassword: string = ""; newPassword: string = ""; hide: boolean = true; pending: boolean = false; oldPasswordControl = new UntypedFormControl('', [Validators.required]) newPasswordControl = new UntypedFormControl('', [Validators.required, Validators.minLength(6)]) duplicatePasswordControl = new UntypedFormControl('', [ Validators.required, (control: AbstractControl): ValidationErrors | null => { return control.value != this.newPassword ? {duplicate: {value: control.value}} : null } ]) constructor(private accountService: AccountService, private snackbar: MatSnackBar) { } ngOnInit(): void { } onChangePassword() { this.pending = true; this.accountService.accountChangePasswordPost({ oldPassword: this.currentPassword, newPassword: this.newPassword }).subscribe({ next: resp => { if(resp.status == "success") { this.snackbar.open("Password updated successfully!", "", {duration: 2000}); this.pending = false; } }, error: err => { if(err.status == 409) { this.snackbar.open("Invalid Password!", "", {duration: 2000}) } this.pending = false; } }) } }