All files / src/app/components/profile profile.component.ts

89.28% Statements 25/28
83.33% Branches 5/6
75% Functions 6/8
88% Lines 22/25

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                                98x   3x 3x     3x                 3x     2x 2x 1x     2x   2x                 2x   1x 1x 1x 1x 1x 1x             2x 2x       2x           1x     1x                  
import { Component, inject, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { ToggleButtonModule } from 'primeng/togglebutton';
import { MessageService } from 'primeng/api';
 
import { FormsModule } from '@angular/forms';
import { User } from '../../models/users';
import { ActivatedRoute } from '@angular/router';
import { TwinpadApiService } from '../../services/twinpad-api.service';
 
@Component({
    selector: 'app-profile',
    imports: [FormsModule, ReactiveFormsModule, ToggleButtonModule],
    templateUrl: './profile.component.html',
    styleUrl: './profile.component.scss'
})
export class ProfileComponent implements OnInit {
  userId: string;
  ownProfile: boolean = true;
  messageService = inject(MessageService);
  user: User;
  profileForm: FormGroup;
  theme: string = 'system';  // Default to system theme
  themeOptions: string[];
  firstname: string;
  lastname: string;
  mail: string;
  password: string;
  userBlocked: boolean;
  userAdministrator: boolean;
 
  constructor(private fb: FormBuilder, private twinpadApiService: TwinpadApiService, private route: ActivatedRoute){}
 
  ngOnInit() {
    this.userId = this.route.snapshot.params['id'];
    if (this.userId) {
      this.ownProfile = false;
    }
    // Set themes options
    this.themeOptions = ["system", "light", "dark"];
    // Initialize the form
    this.profileForm = this.fb.group({
      firstname: ['', [Validators.required]],
      lastname: ['', [Validators.required]],
      email: ['', [Validators.required, Validators.email]],
      password: [''],
      userBlocked: [''],
      userAdministrator: ['']
    });
 
    this.twinpadApiService.getProfile(this.userId).subscribe({
      next: value => {
        this.userId = value.id;
        this.profileForm.controls['firstname'].setValue(value.firstname);
        this.profileForm.controls['lastname'].setValue(value.lastname);
        this.profileForm.controls['email'].setValue(value.email);
        this.profileForm.controls['userBlocked'].setValue(value.is_active);
        this.profileForm.controls['userAdministrator'].setValue(value.is_admin);
      },
      error: err => {
        console.log(err.error);
      }
    });
    // Check if the user has a saved theme preference in localStorage
    const savedTheme = localStorage.getItem('theme');
    Iif (savedTheme) {
      this.theme = savedTheme;
    } else {
      // Apply system theme if no saved preference
      this.theme = "system";
    }
  }
 
 
  editProfile(){
    return this.twinpadApiService.editProfile(this.profileForm, this.userId).subscribe(
      {
        next: _value => {
          this.messageService.add({severity:'success', summary: 'Saving success', detail: 'Your profile has been updated '});
        },
        error: err => {
          this.messageService.add({severity:'error', summary: 'Saving failed', detail: err.error.detail});
        }
      }
    );
  }
}