forked from Bhavya022/David-s-Bridal-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
265 lines (224 loc) · 7.97 KB
/
script.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// document.addEventListener('DOMContentLoaded', function () {
let url = "https://main-server-qat8.onrender.com/product"
let products = document.querySelector('.product-container')
window.addEventListener("load", () => {
fetchproducts()
})
async function fetchproducts() {
try {
let data = await fetch(url);
let response = await data.json();
console.log(response)
checkboxFilter(response)
getCard(response)
} catch (error) {
console.log(error);
}
}
function getCard(data) {
products.innerHTML = ""
let card = `<div class="container">
${data.map((item) => createCard(item.id, item.brand, item.title, item.price, item.color, item.fabric, item.image)).join("")}
</div> `
products.innerHTML = card;
let btns = document.querySelectorAll("button");
for (let i of btns) {
i.addEventListener("click", () => {
localStorage.setItem("singleProduct", (i.id))
window.location.href = "./single_prod.html"
})
}
}
function createCard(id, brand, title, price, color, fabric, image) {
let card = `<div class="product">
<img src="${image}" alt="error" class="product-img">
<div class="product-brand">
<p>${brand}</p>
<h3>${title}</h3>
<h5> $${price}</h5>
<h4>${color}</h4>
<h5>${fabric}</h5>
<div class="container-box">
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4"></div>
</div>
</div>
<button id=${id} class="goToSingleProduct">Buy Now</button>
</div>
`
return card;
}
var dropdown = document.getElementsByClassName("dropdown-btn");
var i;
for (i = 0; i < dropdown.length; i++) {
dropdown[i].addEventListener("click", function () {
this.classList.toggle("active");
var dropdownContent = this.nextElementSibling;
if (dropdownContent.style.display === "block") {
dropdownContent.style.display = "none";
} else {
dropdownContent.style.display = "block";
}
});
}
var checkList = document.getElementById('list1');
checkList.getElementsByClassName('anchor')[0].onclick = function (evt) {
if (checkList.classList.contains('visible'))
checkList.classList.remove('visible');
else
checkList.classList.add('visible');
}
var checkList = document.getElementById('list2');
checkList.getElementsByClassName('fab')[0].onclick = function (evt) {
if (checkList.classList.contains('visible'))
checkList.classList.remove('visible');
else
checkList.classList.add('visible');
}
// -----------------------------------------------------------------
window.onscroll = function () { myFunction() };
var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;
function myFunction() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky")
} else {
navbar.classList.remove("sticky");
}
}
// -----------------------------------------------------------
let arr = [];
function checkboxFilter(data) {
let checkboxes = document.querySelectorAll('.items input');
for (let i = 0; i < checkboxes.length; i++) {
checkboxes[i].addEventListener("click", () => {
if (checkboxes[i].checked) {
let filtering = data.filter((item) => {
if (item.brand.toUpperCase().includes(checkboxes[i].id.toUpperCase()) == true) {
return true;
} else {
return false;
}
})
getCard(filtering)
} else {
console.log("");
}
})
}
}
// -------------------------
function checkboxFilter(data) {
let brandCheckboxes = document.querySelectorAll('.items input[id^="brand"]');
let fabricCheckboxes = document.querySelectorAll('.items2 input');
for (let i = 0; i < brandCheckboxes.length; i++) {
brandCheckboxes[i].addEventListener("click", () => {
if (brandCheckboxes[i].checked) {
let filtering = data.filter((item) => {
if (item.brand.toUpperCase().includes(brandCheckboxes[i].id.toUpperCase())) {
return true;
} else {
return false;
}
})
getCard(filtering);
} else {
console.log("");
}
})
}
for (let i = 0; i < fabricCheckboxes.length; i++) {
fabricCheckboxes[i].addEventListener("click", () => {
if (fabricCheckboxes[i].checked) {
let filtering = data.filter((item) => {
if (item.fabric.toUpperCase().includes(fabricCheckboxes[i].id.toUpperCase())) {
return true;
} else {
return false;
}
})
getCard(filtering);
} else {
console.log("");
}
})
}
}
// -------------------------------------------
let mybutton = document.getElementById("myBtn");
window.onscroll = function () { scrollFunction() };
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
// ------------
function checkboxFilter(data) {
let brandCheckboxes = document.querySelectorAll('.items input[id^="brand"]');
let fabricCheckboxes = document.querySelectorAll('.items2 input');
let sortHighLink = document.getElementById('sortHigh');
let sortLowLink = document.getElementById('sortLow');
for (let i = 0; i < brandCheckboxes.length; i++) {
brandCheckboxes[i].addEventListener("click", () => {
if (brandCheckboxes[i].checked) {
let filtering = data.filter((item) => {
if (item.brand.toUpperCase().includes(brandCheckboxes[i].id.toUpperCase())) {
return true;
} else {
return false;
}
})
sortData(filtering);
getCard(filtering);
} else {
console.log("");
}
})
}
for (let i = 0; i < fabricCheckboxes.length; i++) {
fabricCheckboxes[i].addEventListener("click", () => {
if (fabricCheckboxes[i].checked) {
let filtering = data.filter((item) => {
if (item.fabric.toUpperCase().includes(fabricCheckboxes[i].id.toUpperCase())) {
return true;
} else {
return false;
}
})
sortData(filtering);
getCard(filtering);
} else {
console.log("");
}
})
}
sortHighLink.addEventListener("click", () => {
let sorting = data.sort((a, b) => b.price - a.price);
getCard(sorting);
})
sortLowLink.addEventListener("click", () => {
let sorting = data.sort((a, b) => a.price - b.price);
getCard(sorting);
})
}
function sortData(data) {
let sortHighLink = document.getElementById('sortHigh');
let sortLowLink = document.getElementById('sortLow');
sortHighLink.addEventListener("click", () => {
let sorting = data.sort((a, b) => b.price - a.price);
getCard(sorting);
})
sortLowLink.addEventListener("click", () => {
let sorting = data.sort((a, b) => a.price - b.price);
getCard(sorting);
})
}
// ------------------------------