All files / app/elements/trip-card-element trip-card-element.component.ts

77.82% Statements 186/239
54.28% Branches 19/35
75% Functions 9/12
77.82% Lines 186/239

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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 2401x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 14x 14x 1x 1x 16x 16x 16x 16x 16x 16x 16x 1x 1x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 17x 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 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x     1x 1x 1x 1x 1x 1x 1x                                                                       1x 1x           1x  
import { DatePipe, NgOptimizedImage, NgStyle, isPlatformBrowser } from '@angular/common';
import {
	Component,
	EventEmitter,
	HostListener,
	Input,
	OnInit,
	Output,
	PLATFORM_ID,
	inject,
	input,
	output,
} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { NetworkService } from '@services/network.service';
import { WizardService } from '@services/wizard.service';
import { CookieService } from 'ngx-cookie-service';
import { imageAltTexts } from '../../constants/trips.constants';
 
@Component({
	selector: 'grt-trip-card-element',
	templateUrl: './trip-card-element.component.html',
	styleUrls: ['./trip-card-element.component.scss'],
	providers: [DatePipe],
	imports: [NgStyle, NgOptimizedImage],
})
export class TripCardElementComponent implements OnInit {
	route = inject(ActivatedRoute);
	router = inject(Router);
	wizard = inject(WizardService);
	network = inject(NetworkService);
	private datePipe = inject(DatePipe);
	private cookieService = inject(CookieService);
	private platformId = inject(PLATFORM_ID);
	private MAX_DAYS: number = 35;
 
	@Input() topTrip: any;
	readonly index = input.required<number>();
	readonly favoriteSelection = output();
	readonly favoriteRemoval = output();
	@Output() mapClick = new EventEmitter<string>();
	isFavorite = false;
	showSummary = false;
	showDestinations = false;
	showCountries = false;
	showMoreCountries = false;
	showMoreDestinations = false;
	showMoreSummary = false;
	endDate: Date | any = null;
	date: Date = new Date();
	submitted = false;
	buttonText = 'Customize';
	countries = '';
	destinations = '';
	summary = '';
	name = '';
	price = 0;
	currency = '';
	isMobile = false;
	days = 0;
	theme1 = '';
	theme2 = '';
	mainImage = '';
	image = '';
	id = '';
	altTexts = imageAltTexts;
 
	@HostListener('window:resize', ['$event'])
	onResize(): void {
		if (isPlatformBrowser(this.platformId)) {
			const offsetWidth = document.body.offsetWidth;
			this.isMobile = offsetWidth <= 1025;
		}
	}
 
	constructor() {
		this.MAX_DAYS = this.network.isAdmin ? 60 : 35;
	}
 
	ngOnInit() {
		if (isPlatformBrowser(this.platformId)) {
			const offsetWidth = document.body.offsetWidth;
 
			this.isMobile = offsetWidth <= 1023;
		}
		this.getDetails();
	}
 
	getNewUrl(url: any): string {
		if (!url['img-url'] || typeof url['img-url'] !== 'string') return '';
 
		// Extract the base URL and parameters
		const [baseUrl, params] = url['img-url'].split('/upload/');
		const parts = params.split('/');
		const transformationsIndex = parts.findIndex((part: any) => part.match(/v[0-9]+/));
 
		// Extract transformations and the rest of the URL
		let transformations = parts.slice(0, transformationsIndex).join(',');
		let restOfUrl = parts.slice(transformationsIndex).join('/');
 
		const supportedFormatsRegex = /\.(png|jpg|jpeg)$/i;
 
		// Replace the supported image format with .webp
		restOfUrl = restOfUrl.replace(supportedFormatsRegex, '.webp');
 
		// Split transformations into an array
		const transformationArray = transformations.split(',');
 
		// Define new transformations with specified width and height
		const newTransformations = [`f_auto`, `q_50`];
 
		// Map to replace existing width and height transformations
		const updatedTransformations = transformationArray
			.map((trans: any) => {
				if (trans.startsWith('w_') || trans.startsWith('h_')) {
					return null; // Mark for removal
				}
				return trans;
			})
			.filter((trans: any) => trans !== null); // Remove existing width/height
 
		// Merge transformations ensuring unique values
		const mergedTransformations = new Set([...updatedTransformations, ...newTransformations]);
 
		// Reconstruct transformations string
		transformations = Array.from(mergedTransformations).join(',');
 
		// Construct and return the new URL
		return `${baseUrl}/upload/${transformations}/${restOfUrl}`;
	}
 
	getDetails() {
		if (this.topTrip && this.topTrip?.attributes) {
			this.id = this.topTrip.id;
			const data = this.getNewUrl(this.topTrip.attributes['marketing-trip-image']);
			const data2 = this.getNewUrl(this.topTrip.attributes['marketing-trip-image']['img-url']);
			this.mainImage = data && data.length ? data : data2;
			this.image = this.mainImage;
			this.mainImage = this.mainImage.replace(
				/https:\/\/res-(?:[1-5]\.|)\bcloudinary.com\/gorealtravel\/image\/upload\//g,
				'',
			);
			this.theme1 = this.topTrip.attributes['marketing-theme1'] || '';
			this.theme2 = this.topTrip.attributes['marketing-theme2'] || '';
			this.days = this.topTrip.attributes['trip-length']; // Updated path for trip length
			this.name = this.topTrip.attributes['marketing-trip-name'];
			this.currency = this.topTrip.attributes['marketing-price']
				? this.topTrip.attributes['marketing-price'].currency_iso
				: '';
			this.price = this.topTrip?.attributes['marketing-price']
				? this.topTrip?.attributes['marketing-price'].cents / 100
				: 0;
			this.summary = this.topTrip?.attributes['marketing-summary'];
			if (this.summary && this.summary?.length > 240) {
				this.showMoreSummary = true;
			}
			const cities = this.topTrip?.attributes['cities'];
			this.countries = Array.from(new Set(cities.map((city: any) => city['country-name']))).join(', ');
			if (!this.isMobile && this.countries && this.countries?.length > 62) {
				this.showMoreCountries = true;
			}
			if (this.isMobile && this.countries && this.countries?.length > 30) {
				this.showMoreCountries = true;
			}
			this.destinations = cities.map((city: any) => city.name).join(', ');
			if (!this.isMobile && this.destinations && this.destinations?.length > 62) {
				this.showMoreDestinations = true;
			}
			if (this.isMobile && this.destinations && this.destinations?.length > 30) {
				this.showMoreDestinations = true;
			}
		}
	}
 
	onFavoriteSelect() {
		this.favoriteSelection.emit();
	}
 
	onFavoriteRemove() {
		this.favoriteRemoval.emit();
	}
 
	mapOpen() {
		this.mapClick.emit(this.id);
	}
 
	navigateToDetailPage() {
		this.router.navigate([`/trips/${this.topTrip?.attributes.slug}`]);
	}
 
	getRandomAltText(): string {
		const randomIndex = Math.floor(Math.random() * this.altTexts?.length);
		return this.altTexts[randomIndex];
	}
 
	initCustomPlan() {
		this.submitted = true;
		this.buttonText = 'loading...';
		const cityIds: any = Array.from(new Set(this.topTrip.attributes.cities.map((i: any) => i.id)));
		this.endDate = new Date(+this.date);
		this.endDate.setDate(this.endDate.getDate() + 4);
		this.wizard.mintriplength(cityIds, cityIds[0], cityIds[0]).then((response: any) => {
			const arrive = new Date();
			arrive.setDate(arrive.getDate() + 60);
			const leave = new Date(arrive);
			leave.setDate(arrive.getDate() + Math.min(Math.floor(response['min-trip-length'] || 0 * 1.5), this.MAX_DAYS));
			const departDay = this.datePipe.transform(leave, 'yyyy-MM-dd');

			const data = {
				id: this.topTrip.id,
				'city-ids': cityIds,
				'arrive-day': this.datePipe.transform(arrive, 'yyyy-MM-dd'),
				'depart-day': departDay,
				'hotel-occupancy-rooms': [],
				'hotel-occupancy-rooms-count': 0,
				'user-agent': '',
				'hotel-requested-stars': 3,
			};

			this.wizard
				.createWizard(data)
				.then(() => {
					this.customNext();
				})
				.catch((error) => {
					this.submitted = false;
					this.buttonText = 'Customize';
					console.error(error);
				});
		});
	}
 
	customNext() {
		const url = this.router.url && this.router.url.includes('tours') ? 'clplist' : 'triplist';
		this.router.navigate(['/trip-planner'], {
			queryParams: { path: url },
		});
	}
}