All files / app/pages/getready-page getready-page.component.ts

67.59% Statements 121/179
63.63% Branches 7/11
44.44% Functions 4/9
67.59% Lines 121/179

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 1801x 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 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x       1x 1x 6x 6x 6x 1x 1x                               1x 1x 2x 2x 2x 1x 1x         1x 1x           1x 1x                               1x 1x                               1x  
import { isPlatformBrowser } from '@angular/common';
import { Component, OnInit, PLATFORM_ID, inject } from '@angular/core';
import { Meta, Title } from '@angular/platform-browser';
import { ActivatedRoute, Router } from '@angular/router';
import { WizardClass } from '@classes/wizard.class';
import { HeaderElementComponent } from '@elements/header-element/header-element.component';
import { environment } from '@environments/environment';
import * as Sentry from '@sentry/angular';
import { AnalyticsIntegrationService } from '@services/analyticsIntegration.service';
import { MetaTagService } from '@services/meta-tag.service';
import { WizardService } from '@services/wizard.service';
 
declare let window: any;
 
@Component({
	selector: 'grt-getready-page',
	templateUrl: './getready-page.component.html',
	styleUrls: ['./getready-page.component.scss'],
	imports: [HeaderElementComponent],
})
export class GetreadyPageComponent implements OnInit {
	private meta = inject(Meta);
	private titleService = inject(Title);
	private wizard = inject(WizardService);
	private router = inject(Router);
	private route = inject(ActivatedRoute);
	private analyticsIntegrationService = inject(AnalyticsIntegrationService);
	private metaTag = inject(MetaTagService);
 
	wizardLocal: WizardClass = new WizardClass();
	isBrowser: boolean;
	dots: string = '';
	progressValue: number = 0;
	progressMessage: string = 'Planning your itinerary route';
	citiesLength: number = 0;
 
	private dotsInterval: any;
	private progressInterval: any;
 
	constructor() {
		const platformId = inject(PLATFORM_ID);
		this.isBrowser = isPlatformBrowser(platformId);
 
		this.titleService.setTitle('Ready | Trip Dashboard | Go Real Travel');
		this.metaTag.removeMetaTags();
		this.meta.addTags(
			this.metaTag.getMetaTags('Ready | Trip Dashboard | Go Real Travel', 'Itinerary is getting ready. Please wait...'),
		);
		this.meta.updateTag({ name: 'robots', content: 'noindex, nofollow' });
 
		if (this.isBrowser) {
			const robotsTag = document.querySelector('meta[name="robots"]');
			if (robotsTag) robotsTag.setAttribute('data-seo-lock', 'true');
		}
 
		// Start dots animation
		this.dotsInterval = setInterval(() => this.processDots(), 500);
	}
 
	ngOnInit() {
		if (this.isBrowser) {
			this.analyticsIntegrationService.loadScript();
			window.uetq = window.uetq || [];
			window.uetq.push({
				ec: 'Sign - up',
				ea: 'Complete',
				el: 'Ready Page',
				gv: 0,
			});
 
			const trustboxRef = document.getElementById('trustbox');
			if (trustboxRef && window.Trustpilot) {
				window.Trustpilot.loadFromElement(trustboxRef);
			} else {
				console.error('Trustbox element not found');
			}
 
			// Load wizard from localStorage
			const data = localStorage.getItem('wizard');
			if (data) {
				try {
					const jsonWizard = JSON.parse(data);
					if (jsonWizard) this.wizardLocal.fromJson(jsonWizard);
				} catch (e) {
					console.error('Failed to parse wizard from localStorage', e);
				}
			}
		}
 
		if (!this.wizardLocal?.id) {
			Sentry.captureMessage('[GetreadyPageComponent] wizardLocal.id is null or undefined', 'warning');
		}
 
		this.citiesLength = this.wizardLocal.getCities()?.length || 0;
 
		const wizardId = this.wizardLocal?.id || this.route.snapshot.paramMap.get('id');
		if (!wizardId) {
			console.warn('No wizard ID found, redirecting...');
			this.router.navigate(['/']);
			return;
		}

		this.startProgress(wizardId);
	}
 
	ngOnDestroy() {
		clearInterval(this.dotsInterval);
		clearInterval(this.progressInterval);
	}
 
	private redirectOnce(url: string) {
		clearInterval(this.progressInterval);

		if (!url || typeof url !== 'string') {
			window.location.href = environment.frontendUrl + '/client/itineraries';
			return;
		}

		// Prevent /null and garbage URLs
		if (url.endsWith('/null') || url === 'null') {
			window.location.href = environment.frontendUrl + '/client/itineraries';
			return;
		}

		window.location.href = url;
	}
 
	processDots(): void {
		this.dots += '. ';
		if (this.dots.length >= 8) this.dots = '';
	}
 
	private updateProgressBar(done: boolean) {
		if (this.progressValue >= 100) return;
		if (!done) this.progressValue = Math.min(this.progressValue + 5, 90);
		this.updateProgressMessage();
	}
 
	private updateProgressMessage() {
		if (this.progressValue <= 20) this.progressMessage = 'Planning your itinerary route';
		else if (this.progressValue <= 40) this.progressMessage = 'Selecting transport between destinations';
		else if (this.progressValue <= 80) this.progressMessage = 'Planning tours, activities, and excursions';
		else this.progressMessage = 'Selecting hotel options';
	}
 
	private smoothlyIncreaseProgress(response: any) {
		const fallback = environment.frontendUrl + '/client/itineraries';

		const finalUrl =
			typeof response === 'string' && response.startsWith('http') && !response.endsWith('/null') ? response : fallback;

		const counterInterval = setInterval(() => {
			if (this.progressValue < 100) {
				this.progressValue++;
				this.updateProgressBar(true);
			} else {
				clearInterval(counterInterval);
				this.redirectOnce(finalUrl);
			}
		}, 500);
	}
 
	private startProgress(wizardId: string) {
		const intervalTime = this.citiesLength > 10 ? 12000 : this.citiesLength > 4 ? 8000 : 4000;

		this.progressInterval = setInterval(() => {
			this.updateProgressBar(false);
			this.wizard
				.checkWizard(wizardId)
				.then((response) => {
					if (!response || response === 'null') {
						throw new Error('Invalid redirect URL');
					}
					this.smoothlyIncreaseProgress(response);
				})
				.catch(() => this.smoothlyIncreaseProgress(environment.frontendUrl + '/client/itineraries'));
		}, intervalTime);
	}
}