All files / app/layout/header/account-dropdown account-dropdown.component.ts

100% Statements 35/35
66.66% Branches 4/6
100% Functions 8/8
100% Lines 32/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 674x 4x 4x 4x 4x 4x 4x           4x                 4x 51x 51x 51x 51x 51x   51x   51x     51x 51x 51x 51x 3x 3x   51x 4x 4x       4x 4x 4x       4x           1x       1x 1x      
import { AuthService } from '../../../services/auth/auth.service';
import { ChangeDetectionStrategy, Component, computed, DestroyRef, inject, signal } from '@angular/core';
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
import { Router } from '@angular/router';
import { LogExportService, LoggerService } from '@drevo-web/core';
import { USER_ROLE_LABELS } from '@drevo-web/shared';
import {
    DropdownMenuComponent,
    DropdownMenuItemComponent,
    DropdownMenuTriggerDirective,
    IconButtonComponent,
} from '@drevo-web/ui';
import { finalize } from 'rxjs/operators';
 
@Component({
    selector: 'app-account-dropdown',
    imports: [IconButtonComponent, DropdownMenuComponent, DropdownMenuItemComponent, DropdownMenuTriggerDirective],
    templateUrl: './account-dropdown.component.html',
    styleUrl: './account-dropdown.component.scss',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AccountDropdownComponent {
    private readonly destroyRef = inject(DestroyRef);
    private readonly authService = inject(AuthService);
    private readonly logExportService = inject(LogExportService);
    private readonly router = inject(Router);
    private readonly logger = inject(LoggerService).withContext('AccountDropdown');
 
    private readonly user = toSignal(this.authService.user$);
 
    readonly isLoading = toSignal(this.authService.isLoading$, {
        initialValue: false,
    });
    private readonly _isLoggingOut = signal(false);
    readonly isLoggingOut = this._isLoggingOut.asReadonly();
    readonly isAuthenticated = computed(() => !!this.user());
    readonly displayName = computed(() => {
        const user = this.user();
        return user ? user.name || user.login : '';
    });
    readonly roleLabel = computed(() => {
        const user = this.user();
        return user ? USER_ROLE_LABELS[user.role] : '';
    });
 
    logout(): void {
        this.logger.info('User initiated logout');
        this._isLoggingOut.set(true);
        this.authService
            .logout()
            .pipe(
                takeUntilDestroyed(this.destroyRef),
                finalize(() => this._isLoggingOut.set(false))
            )
            .subscribe();
    }
 
    downloadLogs(): void {
        void this.logExportService.downloadLogs();
    }
 
    navigateToLogin(): void {
        this.logger.info('Navigating to login page');
        void this.router.navigate(['/login']);
    }
}