All files / src/app/features/user/password-form password-form.component.ts

68.62% Statements 35/51
33.33% Branches 3/9
53.33% Functions 8/15
96.66% Lines 29/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 9110x 10x 10x 10x 10x 10x   10x 10x 10x 10x   10x 10x 10x               10x     3x   3x 3x       2x       2x               3x                                                     3x 2x 2x         1x 1x 1x       1x 1x 1x        
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { UserService } from '@core/services/user.service';
import { NotificationService } from '@core/services/notification.service';
import { PasswordValidator } from '@core/validators/password-validator';
import { ConfirmPasswordValidator } from '@core/validators/confirm-password-validator';
import { ChangePasswordRequest } from '@core/model/change-password-request.interface';
import { catchError, take, throwError } from 'rxjs';
import { MatFormField, MatHint, MatLabel } from '@angular/material/form-field';
import { MatButton } from '@angular/material/button';
import { MatInput } from '@angular/material/input';
import { ApiError } from '@core/errors/api-error';
import { BaseChildComponent } from '@core/component/base-child.component';
import { StatusIconComponent } from "@layout/shared/status-icon/status-icon.component";
import { ErrorProcessorService } from '@core/services/error-processor.service';
 
@Component({
  selector: 'app-password-form',
  imports: [ReactiveFormsModule, MatFormField, MatInput, MatButton, MatLabel, MatHint, StatusIconComponent],
  templateUrl: './password-form.component.html',
  styleUrl: './password-form.component.scss'
})
export class PasswordFormComponent extends BaseChildComponent implements OnInit {
 
  public form!: FormGroup;
  public isSubmitting = false;
 
  constructor(private fb: FormBuilder, private userService: UserService, private notificationService: NotificationService, private errorProcessService: ErrorProcessorService) {
    super();
  }
 
  get oldPassword() {
    return this.form.get('oldPassword');
  }
 
  get password() {
    return this.form.get('password');
  }
 
  get passwordConfirmation() {
    return this.form.get('passwordConfirmation');
  }
 
  ngOnInit(): void {
    this.form = this.fb.group({
      oldPassword: [
        '', 
        [
          Validators.required,
        ],
      ],
      password: [
        '', 
        [
          Validators.required,
          PasswordValidator
        ],
      ],
      passwordConfirmation: [
        '',
        [
          Validators.required,
          // PasswordValidator
        ]
      ]
    },
    { validators: ConfirmPasswordValidator}
    );
  }
 
  submit() :void {
    if(!this.isSubmitting && this.form.valid) {
      this.isSubmitting = true;
      this.userService.changePassword({password: this.password!.value, oldPassword: this.oldPassword!.value } as ChangePasswordRequest)
                .pipe(
                  take(1),
                  catchError(
                    (error: ApiError) => {
                      this.isSubmitting = false;
                      this.fail();
                      return this.errorProcessService.processError(new Error(`Password update failed : ${error.message}`));
                    }
                ))
                .subscribe(() => {
                    this.isSubmitting = false;
                    this.notificationService.confirmation($localize `:@@info.password.updated:Your password has been updated.`);
                    this.success();
                });
    }
  }
}