-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
211 lines (177 loc) · 6.77 KB
/
main.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
let banner = ["https://www.jiomart.com/images/category/6047/premium-fruits-20220911.jpeg","https://www.jiomart.com/images/category/2/groceries-20210201.jpeg","https://www.jiomart.com/images/category/3/fashion-20200902.jpeg"]
let baseURL = "http://localhost:3000/data";
let itemcount;
let currData = null;
let fetchedData = null;
let cart = JSON.parse(localStorage.getItem("cart")) || []
fetch(`${baseURL}`).then((res)=>res.json()).then((data)=>{
fetchedData = data;
// console.log(fetchedData);
// console.log(data.length)
itemcount=data.length
})
let cardContainer = document.getElementById("cardContainer")
let price_low_to_high=`http://localhost:3000/data?_sort=price&_order=asc&_limit=12&_page=`
let price_high_to_low=`http://localhost:3000/data?_sort=price&_order=desc&_limit=12&_page=`
let price_without_sort=`http://localhost:3000/data?_limit=12&_page=`
function forapi(api){
window.addEventListener("load",()=>{
fetchAndRender()
})
async function fetchAndRender(page=1){
let response = await fetch(`${api}${page}`);
let result = await response.json();
// console.log(result);
let totalpage=Math.ceil(itemcount/12)
// console.log(totalpage)
paginationpagerenering(totalpage)
// cardContainer.innerHTML = display(result[0].Electronic)
// cardContainer.innerHTML = display(result.data)
currData = result;
display(result)
}
fetchAndRender()
let filterByPrice = document.getElementById("filterPrice")
filterByPrice.addEventListener("change",()=>{
if(filterByPrice.value === ""){
display(result)
}else if(filterByPrice.value == 5000){
let filtered = currData.filter((ele)=>ele.price>5000)
paginationwrapper.innerHTML = null
display(filtered)
}else if(filterByPrice.value == 100){
let filtered = currData.filter((ele)=>ele.price<100)
paginationwrapper.innerHTML = null
display(filtered)
}else if(filterByPrice.value == 1000){
let filtered = currData.filter((ele)=>ele.price>=100 && ele.price<1000)
paginationwrapper.innerHTML = null
display(filtered)
}else if(filterByPrice.value == 3000){
let filtered = currData.filter((ele)=>ele.price>=1000 && ele.price<3000)
paginationwrapper.innerHTML = null
display(filtered)
}else if(filterByPrice.value == 4999){
let filtered = currData.filter((ele)=>ele.price>=3000 && ele.price<5000)
paginationwrapper.innerHTML = null
display(filtered)
}
})
let btn = document.querySelectorAll(".filterBtn")
btn.forEach(element => {
element.addEventListener("click",(e)=>{
e.preventDefault()
let filtered = fetchedData.filter((el)=>(el.category === e.target.dataset.val))
console.log(filtered)
console.log(e.target.dataset.val)
paginationwrapper.innerHTML = null
display(filtered)
})
});
// `<div class="card">
// <img src="${img}" alt="image">
// <p>${name}</p>
// <div class ="stPrice"><p> ₹${stPrice}</p> <h3> ₹${price}</h3> <p> ${Math.floor(((stPrice-price)/stPrice)*100)}% OFF</p></div>
// <button class="elementToFadeInAndOut">Add +</button>
// </div>
// `
function display(data){
cardContainer.innerHTML = ""
data.forEach((elem,ind)=>{
let card = document.createElement("div")
card.className = "card"
let img = document.createElement("img")
img.src = elem.image
img.addEventListener("click",()=>{
localStorage.setItem("singleProduct", JSON.stringify(elem))
window.location.href="./singleProduct.html"
})
let p = document.createElement("p")
p.textContent = elem.name;
let div = document.createElement("div")
div.className = "stPrice"
let p2 = document.createElement('p')
p2.textContent = `₹${elem.strikeprice}`
let h3 = document.createElement("h3")
h3.textContent = `₹${elem.price}`
let p3 = document.createElement("p")
p3.textContent = `${Math.floor(((elem.strikeprice-elem.price)/elem.strikeprice)*100)}% Off`;
div.append(p2,h3,p3)
let button = document.createElement("button")
button.className = "elementToFadeInAndOut"
button.textContent = "Add +"
button.addEventListener("click",()=>{
if(checkDuplicate(elem.id)){
cart.push({...elem,quantity:1})
localStorage.setItem("cart",JSON.stringify(cart))
console.log("Added to the cart")
Swal.fire(
'Added to Cart!',
'You are one step ahead of buying the Product!',
'success'
)
}else{
console.log("Already Present in the cart")
Swal.fire(
'Item is already present in the cart!',
'Go to the cart page!',
'error'
)
}
})
card.append(img,p,div,button)
cardContainer.append(card)
})
// cardContainer.innerHTML=`${data.map((elem)=> getcard(elem.name,elem.image,elem.price,elem.strikeprice)).join("")}`
// cardContainer.innerHTML = AllCards
}
// function getcard(name,img,price,stPrice){
// return `<div class="card">
// <img src="${img}" alt="image">
// <p>${name}</p>
// <div class ="stPrice"><p> ₹${stPrice}</p> <h3> ₹${price}</h3> <p> ${Math.floor(((stPrice-price)/stPrice)*100)}% OFF</p></div>
// <button class="elementToFadeInAndOut">Add +</button>
// </div>
// `
// }
///pagination
let paginationwrapper=document.getElementById("pagination-wrapper")
function paginationpagerenering(pages){
let btn_arr=[];
for(let i=1;i<=pages;i++){
btn_arr.push(` <button class="pagination-button" data-page-number=${i}>${i}</button>
`)
}
paginationwrapper.innerHTML=btn_arr.join("")
let all_button_check=document.querySelectorAll("#pagination-wrapper button")
for(let btn of all_button_check){
btn.addEventListener("click",(e)=>{
fetchAndRender(e.target.dataset.pageNumber)
})
}
}
}
forapi(price_without_sort)
let filter=document.getElementById("sort")
filter.addEventListener("change",(e)=>{
e.preventDefault();
let filtervalue=filter.value;
if(filtervalue==""){
forapi(price_without_sort)
}else if(filtervalue=="asc"){
forapi(price_low_to_high)
}else if(filtervalue=="desc"){
forapi(price_high_to_low)
}
})
function checkDuplicate(id){
// console.log(cart)
for(let i=0;i<cart.length;i++){
// console.log(cart[i])
if(cart[i].id == id){
return false
}
}
return true
}
// http://localhost:3000/data?_sort=price&_order=desc&_limit=12&_page=1