All files / app/pages/logistics logistics.component.ts

58.45% Statements 121/207
62.5% Branches 5/8
37.5% Functions 3/8
58.45% Lines 121/207

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 195 196 197 198 199 200 201 202 203 204 205 206 207 2081x 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 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 6x 6x 6x 6x 6x 6x 6x 1x 1x 6x 6x 1x 1x 6x 6x 6x 6x 6x       6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x                 6x 6x 6x 1x 1x                                                               1x 1x       1x 1x       1x 1x                                                       1x 1x                       1x  
import { isPlatformBrowser } from '@angular/common';
import { Component, OnInit, PLATFORM_ID, inject, viewChild } from '@angular/core';
import { Meta, Title } from '@angular/platform-browser';
import { ActivatedRoute, Router } from '@angular/router';
import { WizardClass } from '@classes/wizard.class';
import {
	AccomodationComponent,
	AccomodationComponent as AccomodationComponent_1,
} from '@elements/accomodation/accomodation.component';
import { HeaderElementComponent } from '@elements/header-element/header-element.component';
import { LineElementComponent } from '@elements/line-element/line-element.component';
import { ProgressBarMobileComponent } from '@elements/progress-bar-mobile/progress-bar-mobile.component';
import { ProgressBarComponent } from '@elements/progress-bar/progress-bar.component';
import {
	TransportationBCComponent,
	TransportationBCComponent as TransportationBCComponent_1,
} from '@elements/transportation-b-c/transportation-b-c.component';
import * as Sentry from '@sentry/angular';
import { AnalyticsIntegrationService } from '@services/analyticsIntegration.service';
import { MetaTagService } from '@services/meta-tag.service';
import { NetworkService } from '@services/network.service';
import { WizardService } from '@services/wizard.service';
import Swal from 'sweetalert2';
 
@Component({
	selector: 'grt-logistics',
	templateUrl: './logistics.component.html',
	styleUrls: ['./logistics.component.scss'],
	imports: [
		HeaderElementComponent,
		ProgressBarComponent,
		ProgressBarMobileComponent,
		AccomodationComponent_1,
		TransportationBCComponent_1,
		LineElementComponent,
	],
})
export class LogisticsComponent implements OnInit {
	private wizard = inject(WizardService);
	private titleService = inject(Title);
	private meta = inject(Meta);
	private route = inject(ActivatedRoute);
	private analyticsIntegrationService = inject(AnalyticsIntegrationService);
	private metaTag = inject(MetaTagService);
	network = inject(NetworkService);
	router = inject(Router);
 
	public wizardLocal: WizardClass = new WizardClass();
	addedCitiesNamesBottom: string = '';
	isBrowser: boolean;
	daysOfTrip: string = '';
	cityIds: string[] = [];
	readonly accomodation = viewChild<AccomodationComponent>('accomodation');
	readonly transbc = viewChild<TransportationBCComponent>('transportationbc');
 
	constructor() {
		const platformId = inject(PLATFORM_ID);
 
		this.isBrowser = isPlatformBrowser(platformId);
		this.titleService.setTitle('Logistics | Trip Planner | Go Real Travel');
		this.metaTag.removeMetaTags();
		this.meta.addTags(
			this.metaTag.getMetaTags('Transportation | Trip Planner | Go Real Travel', 'Transportation Page'),
		);
		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');
			}
		}
	}
 
	ngOnInit() {
		this.refreshPage();
		if (this.isBrowser) {
			this.analyticsIntegrationService.loadScript();
		}
		if (this.isBrowser) {
			const data = localStorage.getItem('step-completion');
			if (data && parseInt(data) < 2) {
				localStorage.setItem('step-completion', '2');
			}
		}
	}
 
	refreshPage() {
		this.daysOfTrip = '';
		this.addedCitiesNamesBottom = '';
		if (this.isBrowser) {
			const data = localStorage.getItem('wizard');
			if (data) {
				const jsonWizard = JSON.parse(data);
				this.wizardLocal.fromJson(jsonWizard);
			}
		}
 
		if (this.wizardLocal) {
			if (this.wizardLocal.id === null || this.wizardLocal.id === undefined) {
				Sentry.captureMessage('[LogisticsComponent] wizardLocal.id is null or undefined', 'warning');
			}
		}
 
		this.cityIds = this.wizardLocal.getCities();
		this.cityIds &&
			this.cityIds?.length &&
			this.cityIds.forEach((id) => {
				this.network.getCity(id).then((city) => {
					if (this.addedCitiesNamesBottom == '') {
						this.addedCitiesNamesBottom += city.name;
					} else {
						this.addedCitiesNamesBottom += ', ' + city.name;
					}
				});
			});
		this.daysOfTrip = this.wizardLocal.getDaysString();
	}
 
	nextStep(): void {
		if (!this.validationOnLogisticsPage()) {
			return;
		}
		const data = {
			id: this.wizardLocal.id,
			'city-ids': this.cityIds,
			'hotel-occupancy-rooms': this.accomodation()?.rooms,
			'additional-comments': this.wizardLocal.additional_comments,
			'hotel-include-breakfast': this.accomodation()?.breakfast,
			'hotel-include-non-refundable': this.accomodation()?.refund,
			'hotel-requested-stars': this.accomodation()?.rating,
			'hotel-occupancy-rooms-count': this.accomodation()?.rooms?.length,
			interests: this.wizardLocal.interests,
			'arrive-day': this.wizardLocal.arrive_day,
			'depart-day': this.wizardLocal.depart_day,
			'arrive-city-id': this.wizardLocal.arrive_city_id,
			'depart-city-id': this.wizardLocal.depart_city_id,
			'include-overnight': this.wizardLocal.include_overnight,
			'purchased-flights': this.wizardLocal.purchased_flights,
			'days-set': this.wizardLocal.days_set,
		};

		this.wizard
			.updateWizard(data)
			.then(() => {
				this.next();
			})
			.catch((error) => {
				alert(error);
			});
	}
 
	next() {
		this.route.snapshot.params['continent'];
		this.router.navigate([`/trip/sign-up`], { queryParamsHandling: 'preserve' });
	}
 
	back() {
		this.route.snapshot.params['continent'];
		this.router.navigate([`/trip/interests`], { queryParamsHandling: 'preserve' });
	}
 
	validationOnLogisticsPage(): boolean {
		const transbc = this.transbc();
		if (transbc) {
			const error = transbc.prepare();
			console.log(error);
			if (error && error !== 'Success') {
				Swal.fire({
					toast: true,
					position: 'top-end',
					showConfirmButton: false,
					showCloseButton: true,
					timerProgressBar: true,
					didOpen: (toast) => {
						toast.onmouseenter = Swal.stopTimer;
						toast.onmouseleave = Swal.resumeTimer;
					},
					icon: 'error',
					text: error,
					background: '#bd362f',
					color: '#fff',
					width: '28em',
					timer: 5000,
				});
				return false; // prevent moving forward
			}
		}
		return true;
	}
 
	moveWizardToStep(stepNumber: number) {
		if (stepNumber > 2 && !this.validationOnLogisticsPage()) {
			return; // Stop navigation if validation fails
		}
		if (stepNumber === 4) {
			this.nextStep();
		} else if (stepNumber === 2) {
			this.back();
		} else if (stepNumber === 3) {
			this.router.navigate(['/trip-planner'], { queryParamsHandling: 'preserve' });
		}
	}
}