-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsearch.js
159 lines (124 loc) · 4.57 KB
/
search.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
const url = 'https://mock-final-copy-api.onrender.com/locations';
let searchArea = document.getElementById("Search_area");
let form = document.getElementById("form");
async function fetchData(page, searchData = "") {
try {
let searchQuery = new URLSearchParams(window.location.search).get("query");
let res = await fetch(`${url}?_page=${page || 1}&${searchData}&location_like=${searchQuery}`);
let data = await res.json();
searchArea.innerHTML = ""
appendData(data);
} catch (err) {
console.log(err);
}
}
function appendData(data) {
data.forEach(item => searchArea.append(createCard(item)));
}
function createCard(data) {
const card = document.createElement('div');
card.className = 'card';
const image = document.createElement('img');
image.src = data.image;
image.alt = data.location;
card.appendChild(image);
const info = document.createElement('div');
info.className = 'info'; // Change this to 'info'
const heading = document.createElement('h2');
heading.textContent = data.location;
const price = document.createElement('p');
price.textContent = `Price: $${data.price}`;
const description = document.createElement('p');
description.textContent = data.description;
info.appendChild(heading);
info.appendChild(price);
info.appendChild(description);
let read = document.createElement('a');
read.className = 'btnn';
read.href = '#';
read.innerText = 'Read more';
info.appendChild(read);
card.appendChild(info);
card.addEventListener('click', function () {
window.location.href = `./CardPage.html?location=${data.location}`;
});
read.addEventListener('click', function () {
window.location.href = `./CardPage.html?location=${data.location}`;
});
return card;
}
let page = 1;
fetchData(page);
let scrollHeight = document.documentElement.scrollHeight;
let clientHeight = document.documentElement.clientHeight;
window.addEventListener('scroll', (e) => {
let scrollTop = document.documentElement.scrollTop;
if (Math.ceil(clientHeight + scrollTop) >= scrollHeight) {
page++;
fetchData(page);
}
});
document.addEventListener('DOMContentLoaded', function () {
const signupButton = document.getElementById('signupButton');
const loginButton = document.getElementById('loginButton');
const Tours = document.getElementById('tourr');
signupButton.addEventListener('click', function () {
window.location.href = "./registerlogin/login.html";
});
loginButton.addEventListener('click', function () {
window.location.href = "./registerlogin/login.html";
});
Tours.addEventListener('click', function () {
window.location.href = "./search.html?query=";
});
});
document.addEventListener("DOMContentLoaded", function () {
let logo = document.querySelector(".Alogo");
if (logo) {
logo.addEventListener("click", function () {
window.location.href = "index.html";
});
}
});
const searchButton = document.getElementById('sBtn');
searchButton.addEventListener('click', handleOnSubmit);
function handleOnSubmit(event) {
event.preventDefault();
let text = form.search.value;
let query = text.toLowerCase();
window.location.href = `./search.html?query=${query}`;
}
const sortByPriceDropdown = document.getElementById('sortByPrice');
// Event listener for sorting by price
sortByPriceDropdown.addEventListener('change', () => {
const selectedOption = sortByPriceDropdown.value;
let sortParam = '';
if (selectedOption === 'lowToHigh') {
sortParam = '_sort=price&_order=asc';
} else if (selectedOption === 'highToLow') {
sortParam = '_sort=price&_order=desc';
}
searchArea.innerHTML = "<h2>Sorting.. Please wait</h2>"
fetchData(page, '', sortParam);
});
function sortingByFetch (){fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(locations => {
// Assuming 'price' is the property you want to sort by
const sortedLocations = locations.sort((a, b) => a.price - b.price);
// Now 'sortedLocations' contains the locations sorted by price
console.log(sortedLocations);
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
}
const sortLowToHighButton = document.getElementById('sortLowToHighButton');
if (sortLowToHighButton) {
sortLowToHighButton.addEventListener('click', fetchDataAndSort);
}