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

83.33% Statements 30/36
92.85% Branches 13/14
75% Functions 9/12
82.35% Lines 28/34

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                                  197x 86x 86x 86x   86x 86x               86x       86x   85x 85x 85x             86x 86x 2739x   86x   159x           27232x 6045x   21187x 175x   21012x       27232x     27232x 6220x   21012x       1x       2825x                 111x    
import { Component, inject, LOCALE_ID, OnDestroy, OnInit } from '@angular/core';
import { Router, RouterModule, RouterLink } from '@angular/router';
import { MessageService } from 'primeng/api';
import { TagModule } from 'primeng/tag';
import { interval, Subscription } from 'rxjs';
import { DatePipe, DecimalPipe } 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, TagModule, DecimalPipe],
    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;
 
    delayToServer: number = 0;
 
    ngOnInit(){
 
        this.twinpadApiService.getProfile().subscribe({
            next: user => {
                this.user = user;
                this.userAdmin = user.is_admin;
                this.imagePictureUrl = this.styleService.getCustomerLogoFromEmail(user.email);
            },
            error: _ => {
                this.userAdmin = false;
            }
        });
 
        this.localTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
        this.updateTime();
        this.subscription = interval(100).subscribe(() => this.updateTime());
 
        this.twinpadApiService.cloudStatusBehaviorSubject$?.subscribe({
            next: status => {
                this.delayToServer = Date.now() - status.timestamp * 1000;
            }
        });
    }
 
    getDelaySeverity() {
        if (this.delayToServer < -500 || this.delayToServer > 500) {
            return "danger";
        }
        if (this.delayToServer < -200 || this.delayToServer > 200) {
            return "warn";
        }
        return "success";
    }
 
    getDelayIcon() {
        Iif (this.delayToServer < 0) {
            return "pi pi-question";
        }
        if (this.delayToServer > 200) {
            return "pi pi-exclamation-triangle";
        }
        return "pi pi-check";
    }
 
    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' });
    }
}