All files / src/app/features/user/user-edit user-edit.component.ts

70% Statements 35/50
36.36% Branches 4/11
53.84% Functions 7/13
96.96% Lines 32/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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 10510x   10x 10x 10x   10x   10x 10x 10x 10x 10x               10x       3x     3x 3x 3x 3x 3x 3x   3x             3x 3x                                                                     2x       2x   2x   2x 2x         1x 1x 1x       1x 1x 1x          
import { Component, OnDestroy, OnInit } from '@angular/core';
import { User } from '@core/model/user.interface';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { AuthValidators } from '@core/services/auth.validators';
import { UserService } from '@core/services/user.service';
import { EditUserRequest } from '@core/model/edit-user-request.interface';
import { catchError } from 'rxjs';
import { ApiError } from '@core/errors/api-error';
import { NotificationService } from '@core/services/notification.service';
import { BaseChildComponent } from '@core/component/base-child.component';
import { FormErrorService } from '@core/services/form-error.service';
import { UserFormComponent } from "@features/user/user-form/user-form.component";
import { ErrorProcessorService } from '@core/services/error-processor.service';
 
@Component({
  selector: 'app-user-edit',
  imports: [ReactiveFormsModule, UserFormComponent],
  templateUrl: './user-edit.component.html',
  styleUrl: './user-edit.component.scss'
})
export class UserEditComponent extends BaseChildComponent implements OnInit, OnDestroy {
  protected user!: User;
 
  protected form!: FormGroup;
  protected isSubmitting = false;
  
  constructor(
    private authValidators: AuthValidators,
    private fb: FormBuilder,
    private userService: UserService,
    private notificationService: NotificationService,
    private formErrorService: FormErrorService,
    private errorProcessorService: ErrorProcessorService
  ) {
    super();
  }
  ngOnDestroy(): void {
    this.formErrorService.cleanup(); // free subscriptions 
  }
 
  ngOnInit(): void {
    this.user  = this.data.user!;
    this.form = this.fb.group({
      email: [
        this.user.email, 
        [
          Validators.required,
          Validators.email
        ],
        this.authValidators.checkEmailExistsAsync(this.user.email).bind(this.authValidators)
      ],
      username: [
        this.user.username, 
        [
          Validators.required,
          Validators.minLength(5)
        ],
        this.authValidators.checkUsernameExistsAsync(this.user.username).bind(this.authValidators)
      ],
      firstName: [
        this.user.firstName, 
        [
          Validators.required,
          Validators.minLength(1)
        ]
      ],
      lastName: [
        this.user.lastName, 
        [
          Validators.required,
          Validators.minLength(1)
        ]
      ]
    });
  }
 
  get email() {
    return this.form.get('email');
  }
 
  submit() :void {
      if(!this.isSubmitting && this.form.valid) {
 
        const emailChanged: boolean = this.email?.value  !== this.user.email;
 
        this.isSubmitting = true;
        this.userService.editUser(this.form.value as EditUserRequest)
            .pipe(
              // take(1),
              catchError(
                (error: ApiError) => {
                  this.isSubmitting = false;
                  this.formErrorService.setBackendErrors(this.form, error.errors);
                  return this.errorProcessorService.processError(error);
                }
            ))
            .subscribe(() => {
                this.isSubmitting = false;
                this.notificationService.confirmation($localize `:@@info.user.edit.success:Your information has been updated.` + (emailChanged ? ' '+$localize `:@@info.email.verification.needed:Your new email address needs verification ; please check your emails.` : ''));
                this.success();
            });
      }
    }
}