All files / app/elements/brand-header-element brand-header-element.component.ts

37.03% Statements 30/81
100% Branches 0/0
0% Functions 0/4
37.03% Lines 30/81

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 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x     1x 1x                                     1x 1x                                                         1x  
import { isPlatformBrowser, NgStyle } from '@angular/common';
import { Component, inject, OnInit, PLATFORM_ID } from '@angular/core';
import { UserClass } from '@classes/user.class';
import { AuthService } from '@services/auth.service';
import { BrandService } from '@services/brand.service';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
import Swal from 'sweetalert2';
 
@Component({
	selector: 'grt-brand-header-element',
	templateUrl: './brand-header-element.component.html',
	styleUrl: './brand-header-element.component.scss',
	imports: [NgStyle, BsDropdownModule],
})
export class BrandHeaderElementComponent implements OnInit {
	private authService = inject(AuthService);
	branding: any = inject(BrandService).getBrand();
	isBrowser: boolean;
	user: UserClass = new UserClass();
	loggedIn = false;
	dropdownOpened = false;
 
	constructor() {
		const platformId = inject(PLATFORM_ID);
		this.isBrowser = isPlatformBrowser(platformId);
	}
 
	toggleDropdownLogout(): void {
		this.dropdownOpened = !this.dropdownOpened;
	}
 
	ngOnInit(): void {
		const cachedUser = this.authService.getCurrentUser();
		if (cachedUser) {
			this.loggedIn = true;
			// create a proper UserClass instance from the plain object
			this.user = Object.assign(new UserClass(), cachedUser);
		} else {
			// ✅ Step 2: Verify session once (if no cached user)
			this.authService.verifySession().subscribe((user) => {
				this.loggedIn = !!user;
				this.user = user ? Object.assign(new UserClass(), user) : new UserClass();
			});
		}
		// ✅ Step 3: Reactive user subscription (handles login/logout updates)
		this.authService.currentUser$.subscribe((user) => {
			this.loggedIn = !!user;
			this.user = user ? Object.assign(new UserClass(), user) : new UserClass();
		});
	}
 
	logout(): void {
		this.authService.logout().subscribe({
			next: () => {
				if (this.isBrowser) {
					Swal.fire({
						toast: true,
						position: 'top-end',
						showConfirmButton: false,
						timerProgressBar: true,
						showCloseButton: true,
						didOpen: (toast) => {
							toast.addEventListener('mouseenter', Swal.stopTimer);
							toast.addEventListener('mouseleave', Swal.resumeTimer);
						},
						icon: 'success',
						text: 'You have been successfully logged out.',
						background: '#51a351',
						color: '#fff',
						width: '28em',
						timer: 5000,
					});
				}
				this.loggedIn = false;
			},
			error: (err) => {
				console.error('Logout failed', err);
			},
		});
	}
}