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

68.55% Statements 133/194
76.92% Branches 10/13
45% Functions 9/20
68.55% Lines 133/194

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 1951x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 39x 39x 39x 1x 1x 39x 39x 39x       39x 39x 39x     39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 1x 1x 39x 39x       1x 1x         1x 1x 2x 2x 2x 1x 1x 1x 1x                                         1x 1x   1x 1x 1x 1x 1x 2x 2x 1x 1x     1x 1x     1x     1x     1x 1x 3x 3x 1x 1x                 1x 1x     1x 1x         1x 1x             1x  
import { NgClass, NgStyle, isPlatformBrowser } from '@angular/common';
import { Component, HostListener, OnInit, PLATFORM_ID, inject, input } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { UserClass } from '@classes/user.class';
import { environment } from '@environments/environment';
import { AuthService } from '@services/auth.service';
import { SharedDataService } from '@services/menu.service';
import { NetworkService } from '@services/network.service';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
import Swal from 'sweetalert2';
 
@Component({
	selector: 'grt-header-element',
	templateUrl: './header-element.component.html',
	styleUrls: ['./header-element.component.scss'],
	imports: [NgClass, NgStyle, BsDropdownModule],
})
export class HeaderElementComponent implements OnInit {
	network = inject(NetworkService);
	private router = inject(Router);
	private route = inject(ActivatedRoute);
	private sharedDataService = inject(SharedDataService);
	private authService = inject(AuthService);
 
	readonly showCustReview = input<boolean>(false);
	readonly showDestination = input<boolean>(true);
	readonly showTopTrips = input<boolean>(true);
	readonly showReqCall = input<boolean>(true);
	readonly showLogIn = input<boolean>(true);
	readonly showContactUs = input<boolean>(true);
	readonly showHam = input<boolean>(true);
	readonly showBlog = input<boolean>(true);
	readonly headerSticky = input<boolean>(true);
	readonly showTrips = input<boolean>(true);
	readonly indexPage = input<boolean>(true);
 
	isOpen = false;
	isAboutOpen = false; 
	state = 0;
	isActive = false;
	loggedIn = false;
	dropdownOpened = false;
	production = environment.production;
	isBrowser: boolean;
	isCollapsed = true;
	tripMenus: any;
	environment = environment;
	user: UserClass = new UserClass();
 
	@HostListener('document:click', ['$event'])
	onDocumentClick(event: any) {
		if (!event.target.closest('.dropdown-container')) {
			this.isOpen = false;
		}
		if (!event.target.closest('.about-us')) {
			this.isOpen = false;
		}
	}
 
	@HostListener('document:mousemove', ['$event'])
	onDocumentMove(event: any) {
		if (!event.target.closest('.mouse')) {
			this.isOpen = false;
			this.isAboutOpen = false;
		}
	}
 
	constructor() {
		const platformId = inject(PLATFORM_ID);
		this.isBrowser = isPlatformBrowser(platformId);
	}
 
	ngOnInit() {
		// ✅ Step 1: Check if user already exists in memory
		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();
		});
 
		// ✅ Step 4: Load menus
		this.getMenus();
	}
 
	async getMenus() {
		if (!this.sharedDataService.isDataLoaded()) {
			await this.sharedDataService.loadTripMenus();
		}
		this.tripMenus = this.sharedDataService.getTripMenus();
	}
 
	allMyTrip() {
		if (this.isBrowser) {
			window.location.href = `${environment.backendUrl}/client/itineraries`;
		}
	}
 
	toggleNav() {
		this.isActive = !this.isActive;
		this.setState(0);
	}
 
	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);
			},
		});
	}
 
	toggleDropdown(): void {
		this.isOpen = !this.isOpen;
	}
 
	toggleAboutDropdown(): void {
		this.isAboutOpen = !this.isAboutOpen;
	}
 
	onHidden(): void {
		console.log('Dropdown is hidden');
	}
	onShown(): void {
		console.log('Dropdown is shown');
	}
	isOpenChange(): void {
		console.log('Dropdown state changed');
	}
 
	setState(data: number) {
		this.state = data;
	}
 
	changePage(link: string) {
		console.log(link);
		this.isOpen = false;
		this.isAboutOpen = false;
		this.isActive = false;
		if (this.isBrowser) {
			window.location.href = link;
		}
	}
 
	toggleDropdownLogout(): void {
		this.dropdownOpened = !this.dropdownOpened;
	}
 
	redirectToBlog() {
		if (this.isBrowser) {
			window.location.href = 'https://www.gorealtravel.com/blog/';
		}
	}
 
	adminDashboard() {
		if (this.isBrowser && !environment.production) {
			window.location.href = `https://staging.gorealtravel.com/admin/itineraries`;
		} else {
			window.location.href = `https://trip.gorealtravel.com/admin/itineraries`;
		}
	}
}