All files / app/services menu.service.ts

86.48% Statements 64/74
81.25% Branches 13/16
100% Functions 6/6
86.48% Lines 64/74

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 751x 1x 1x 1x 1x 1x 1x 1x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x           38x     38x 38x 3x 3x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 3x 2x 3x 1x 1x 38x 38x 38x 38x 38x 38x 38x 1x 1x 1x 1x 1x 1x     1x 1x   1x 1x 1x 1x 38x  
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { environment } from '@environments/environment';
 
@Injectable({
	providedIn: 'root',
})
export class SharedDataService {
	private http = inject(HttpClient);
 
	private tripMenus: any;
	private isLoaded = false;
	headers: HttpHeaders = new HttpHeaders({
		Accept: 'application/vnd.api+json',
		'Content-Type': 'application/vnd.api+json',
	});
 
	async loadTripMenus(): Promise<any> {
		if (!this.isLoaded) {
			try {
				const response: any = await this.getTripMenu();
				this.tripMenus = response.menu_trips_data[0].menus;
				this.isLoaded = true;
			} catch (error) {
				console.error('Error fetching trip data:', error);
			}
		}
		return this.tripMenus;
	}
 
	getTripMenus(): any {
		return this.tripMenus;
	}
 
	isDataLoaded(): boolean {
		return this.isLoaded;
	}
 
	getTripMenu(): Promise<string[]> {
		return new Promise<string[]>((resolve, reject) => {
			this.http
				.get<any>(environment.apiBaseUrl + '/v3/marketing/menu_trips', {
					headers: this.headers,
				})
				.subscribe(
					(response) => {
						if (response.data) {
							resolve(response.data);
						} else {
							reject('No data found'); // or handle this case as you see fit
						}
					},
					(error) => reject(error), // Properly forward the error
				);
		});
	}
 
	getTripPageMenu(): any {
		return new Promise<any>((resolve, reject) => {
			this.http.get<any>(environment.apiBaseUrl + '/v3/marketing/tour_destinations').subscribe(
				(response) => {
					if (response.success) {
						resolve(response.data);
					} else {
						reject();
					}
				},
				(error) => {
					reject(error);
				},
			);
		});
	}
}