All files / src/app/features/user/me me.component.ts

68.42% Statements 39/57
0% Branches 0/4
50% Functions 11/22
93.75% Lines 30/32

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 821x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   1x 1x 1x                 2x     11x   11x 11x       1x       2x 2x 2x         2x       1x 1x                       1x       2x     1x           1x       1x    
import { Component } from '@angular/core';
import { catchError, EMPTY, Observable, take, throwError } from 'rxjs';
import { AsyncPipe, DatePipe } from '@angular/common';
import { Router } from '@angular/router';
import { User } from '@core/model/user.interface';
import { UserService } from '@core/services/user.service';
import { AuthService } from '@core/services/auth.service';
import { SessionService } from '@core/services/session.service';
import { MatCard, MatCardActions, MatCardContent } from '@angular/material/card';
import { MatButton } from '@angular/material/button';
import { ModalService } from '@core/services/modal.service';
import { ConfirmDialogService } from '@core/services/confirm-dialog.service';
import { ApiError } from '@core/errors/api-error';
import { NotificationService } from '@core/services/notification.service';
import { EmailStatus } from '@core/model/email-status';
import { MatIcon } from '@angular/material/icon';
import { ComponentInputDomainData } from '@core/model/component-input-data.interface';
 
@Component({
  selector: 'app-me',
  imports: [AsyncPipe, DatePipe, MatCard, MatCardContent, MatButton, MatIcon, MatCardActions],
  templateUrl: './me.component.html',
  styleUrl: './me.component.scss'
})
export class MeComponent {
 
  protected user$: Observable<User>;
  protected EmailStatus = EmailStatus;
 
  constructor(private userService: UserService, private authService: AuthService, private sessionService: SessionService, private router: Router, private modalService: ModalService, private dialogService: ConfirmDialogService, private notificationService: NotificationService) {
    this.user$ = this.userService.getUser();
  }
 
  public changePassword() :void {
    this.modalService.openPasswordModal(() => {});
  }
 
  private endDeleteAccount() :void {
    this.sessionService.logOut();
    this.notificationService.confirmation($localize `:@@info.account_deleted:Your account has been deleted. Thank your for using MyJobs.`);
    this.router.navigate([""]);
  }
 
  public confirmDeleteAccount() :void {
    // TODO delete user account
    this.userService.deleteUser().pipe(
      take(1)
    ).subscribe(() => {
      // first we logout ; it will help prevent the jwt interceptor to try and re-authenticate
      this.sessionService.logOut(false);
      this.authService.logout().
        pipe(
          catchError(() =>  {
            this.endDeleteAccount();
            return EMPTY;
          })
      )
      .subscribe(this.endDeleteAccount.bind(this));
    });
  }
 
  public deleteAccount() :void {
    this.dialogService.openConfirmDialog($localize `:@@warning.user.account_delete:Delete your accout ? All data will be definitely deleted !`, this.confirmDeleteAccount.bind(this));
  }
 
  public confirmSendVerificationEmail() {
    this.userService.sendVerificationMail()
      .subscribe(
        () => {
          this.notificationService.confirmation($localize `:@@info.user.validation_email_sent:The verification email has been sent ; please check your emails.`);
        }
      );
  }
 
  public sendVerificationEmail() :void {
    this.dialogService.openConfirmDialog($localize `:@@action.user.send_validation_email:Send validation email`, this.confirmSendVerificationEmail.bind(this));
  }
 
  public editUser(user: User) :void {
    this.modalService.openUserEditModal(user, (data: ComponentInputDomainData) => {this.user$ = this.userService.getUser();});
  }
}