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

79.16% Statements 19/24
100% Branches 2/2
70% Functions 7/10
77.27% Lines 17/22

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                                205x 90x 90x 90x   90x 90x                     90x   89x             90x 90x 2846x 90x 88x 88x           1x       2936x                 115x    
import { Component, inject, LOCALE_ID, OnDestroy, OnInit } from '@angular/core';
import { Router, RouterModule, RouterLink } from '@angular/router';
import { MessageService } from 'primeng/api';
import { interval, Subscription } from 'rxjs';
import { DatePipe } from '@angular/common';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { AvatarModule } from 'primeng/avatar';
import { StyleService } from '../../services/style.service';
import { User } from '../../models/users';
 
@Component({
    selector: 'app-header',
    imports: [RouterModule, RouterLink, DatePipe, AvatarModule],
    templateUrl: './header.component.html',
    styleUrl: './header.component.scss'
})
export class HeaderComponent implements OnInit, OnDestroy{
    private router = inject(Router);
    private twinpadApiService = inject(TwinpadApiService);
    private styleService= inject(StyleService);
 
    locale = inject(LOCALE_ID);
    messageService = inject(MessageService);
    userAdmin: boolean;
    localTimeZone: string;
    now: Date;
    subscription: Subscription;
    user: User;
    imagePictureUrl: string;
 
 
    ngOnInit(){
 
        this.twinpadApiService.getProfile().subscribe({
            next: user => {
                this.userAdmin = user.is_admin;
            },
            error: _ => {
                this.userAdmin = false;
            }
        });
 
        this.localTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
        this.updateTime();
        this.subscription = interval(100).subscribe(() => this.updateTime());
        this.twinpadApiService.getProfile().subscribe({next: user =>{
            this.user = user;
            this.imagePictureUrl = this.styleService.getCustomerLogoFromEmail(this.user.email);
            }});
    }
 
 
    ngOnDestroy(){
        this.subscription?.unsubscribe();
    }
 
    private updateTime() {
        this.now = new Date();
      }
 
    logout(){
        this.twinpadApiService.logout();
        setTimeout(() => {
            this.twinpadApiService.redirectToLogin(this.router);
        }, 1500);
        this.messageService.add({severity:'success', summary: 'Logout success', detail: 'Successfully logout' });
    }
}