forked from Alipakkr/Voyawander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotel.js
168 lines (134 loc) · 5.18 KB
/
hotel.js
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
// Select the container where hotel data will be rendered
const boxPackageContainer = document.querySelector(".box-package-container");
const apiUrl = "https://quasarapi.onrender.com/destinations";
let mainData = [];
let debounceTimer;
// Function to fetch hotels
async function fetchHotels(query,cond,page) {
try {
const response = await fetch(`https://quasarapi.onrender.com/destinations?Country=${query ||""}${cond || ""}_page=${page}`);
if (!response.ok) {
throw new Error("Failed to fetch hotels");
}
return await response.json();
} catch (error) {
console.error("Error fetching hotels:", error);
return [];
}
}
// Function to create a single hotel card
function createHotelCard(item) {
const boxPackage = document.createElement("div");
boxPackage.className = "box-package";
const imagePackage = document.createElement("div");
imagePackage.className = "image-package";
const imageHo = document.createElement("img");
imageHo.className = "imageHotel";
imageHo.src = item.hotelPhoto;
imageHo.alt = item.name;
imagePackage.appendChild(imageHo);
const contentDiv = document.createElement("div");
contentDiv.className = "content";
const destinationsHotel = document.createElement("h3");
destinationsHotel.innerText = item.description;
contentDiv.appendChild(destinationsHotel);
const hotelPrice = document.createElement("h3");
hotelPrice.innerText = item.price;
contentDiv.appendChild(hotelPrice);
const countryHotel = document.createElement("p");
countryHotel.innerText = item.country;
contentDiv.appendChild(countryHotel);
const anchorHotel = document.createElement("a");
anchorHotel.className = "btn";
anchorHotel.innerText = "Book now";
anchorHotel.addEventListener("click", function () {
window.location.href = "payment.html";
});
contentDiv.appendChild(anchorHotel);
boxPackage.appendChild(imagePackage);
boxPackage.appendChild(contentDiv);
return boxPackage;
}
// Render fetched hotel data
async function renderHotelData() {
try {
const hotelData = await fetchHotels(apiUrl);
mainData = hotelData; // Update mainData with fetched data
// Clear previous content
boxPackageContainer.innerHTML = "";
// Render all hotel cards initially
hotelData.forEach((hotel) => {
const hotelCard = createHotelCard(hotel);
boxPackageContainer.appendChild(hotelCard);
});
} catch (error) {
console.error("Error rendering hotel data:", error);
}
}
// console.log(mainData);
// Function to render hotel data based on selected country
function renderHotelsByCountry(selectedCountry) {
const container = document.querySelector(".box-package-container");
container.innerHTML = ""; // Clear previous content
// Check if a country is selected
if (selectedCountry === "") {
// If no country selected, render all data
mainData.forEach((hotel) => {
const hotelCard = createHotelCard(hotel);
container.appendChild(hotelCard);
});
} else {
// Filter mainData based on the selected country
const filteredData = mainData.filter(
(hotel) => hotel.country === selectedCountry
);
// Render hotel cards for the filtered data
filteredData.forEach((hotel) => {
const hotelCard = createHotelCard(hotel);
container.appendChild(hotelCard);
});
}
}
// Event listener for dropdown change
const locationDropdown = document.getElementById("locationDropdown");
locationDropdown.addEventListener("change", () => {
const selectedCountry = locationDropdown.value;
renderHotelsByCountry(selectedCountry);
});
// Function to sort hotel data by price (high to low)
function sortHotelsByPriceHighToLow() {
mainData.sort((a, b) => parseFloat(b.price) - parseFloat(a.price));
renderHotelsByCountry(locationDropdown.value,`&_sort=price&_order=desc&`);
}
// Function to sort hotel data by price (low to high)
function sortHotelsByPriceLowToHigh() {
mainData.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
renderHotelsByCountry(locationDropdown.value,`&_sort=price&_order=asc&`);
}
// Event listeners for sorting buttons
const highToLowBtn = document.getElementById("high-to-low");
const lowToHighBtn = document.getElementById("low-to-high");
highToLowBtn.addEventListener("click", sortHotelsByPriceHighToLow);
lowToHighBtn.addEventListener("click", sortHotelsByPriceLowToHigh);
// Initial rendering with default selected country (optional)
const defaultCountry = locationDropdown.value;
renderHotelsByCountry(defaultCountry);
// Call the function to render hotel data when the DOM content is loaded
document.addEventListener("DOMContentLoaded", renderHotelData());
//famous place
// Function to render hotel data based on selected country
///move
let moveit = 0;
// Append Load More button
// const loadMoreBtn = document.createElement("span");
const loadMoreBtn = document.getElementsByClassName("load-more")[0];
loadMoreBtn.className = "btn";
loadMoreBtn.innerText = ">";
let pageNumber=1;
loadMoreBtn.addEventListener("click", () => {
pageNumber++;
moveit-=500;
fetchHotels(apiUrl,pageNumber);
boxPackageContainer.style.transform = `translate(${moveit}px)`;
boxPackageContainer.style.transition = "transform 0.7s cubic-bezier(.22,.44,.06,1)";
});