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

75% Statements 36/48
33.33% Branches 3/9
64.28% Functions 9/14
100% Lines 33/33

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 901x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x                 2x       3x     3x 3x 3x 3x 3x 3x         68x       12x       3x 3x 3x                                             2x 2x 2x         1x 1x       1x 1x 1x        
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { MatFormField, MatHint, MatInput, MatLabel } from '@angular/material/input';
import { MatButton } from '@angular/material/button';
import { catchError, take, throwError } from 'rxjs';
import { NotificationService } from '@core/services/notification.service';
import { PasswordValidator } from '@core/validators/password-validator';
import { ConfirmPasswordValidator } from '@core/validators/confirm-password-validator';
import { NewPasswordRequest } from '@core/model/new-password-request.interface';
import { UserService } from '@core/services/user.service';
import { StatusIconComponent } from "@layout/shared/status-icon/status-icon.component";
import { ErrorProcessorService } from '@core/services/error-processor.service';
 
@Component({
  selector: 'app-new-password',
  standalone: true,
  imports: [ReactiveFormsModule, MatInput, MatFormField, MatButton, MatLabel, MatHint, StatusIconComponent],
  templateUrl: './new-password.component.html',
  styleUrl: './new-password.component.scss'
})
export class NewPasswordComponent implements OnInit {
 
  public token!: String
  public form!: FormGroup;
  public isSubmitting = false;
 
  constructor(
    private activatedRoute: ActivatedRoute, 
    private fb: FormBuilder, 
    private userService: UserService, 
    private notificationService: NotificationService, 
    private router: Router,
    private errorProcessorService: ErrorProcessorService) {
 
  }
 
  get password() {
    return this.form.get('password');
  }
 
  get passwordConfirmation() {
    return this.form.get('passwordConfirmation');
  }
 
  ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe((params) => {
      this.token = params['token'];
      this.form = this.fb.group({
        password: [
          '', 
          [
            Validators.required,
            PasswordValidator
          ],
        ],
        passwordConfirmation: [
          '',
          [
            Validators.required,
            // PasswordValidator
          ]
        ]
      },
      { validators: ConfirmPasswordValidator}
      );
      
    });
  }
 
  submit() :void {
    if(!this.isSubmitting && this.form.valid) {
      this.isSubmitting = true;
      this.userService.newPassword({password: this.password!.value, token: this.token} as NewPasswordRequest)
        .pipe(
          take(1),
          catchError(
            () => {
              this.isSubmitting = false;
              return this.errorProcessorService.processError(new Error($localize `:@@error.password.creation:Password creation failed`));
            }
        ))
        .subscribe(() => {
            this.isSubmitting = false;
            this.notificationService.confirmation($localize `:@@info.password.updated:Your password has been updated.` + ' '+ $localize `:@@info.login.possible:You may now log in.`);
            this.router.navigate(["/login"])
        });
    }
  }
}