forked from neelmani0/internal-mark-2946
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cases&Sleevespproducts.js
194 lines (132 loc) · 4.7 KB
/
Cases&Sleevespproducts.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
let container = document.getElementById("Products-container");
let h2 = document.querySelector("h2");
let y = JSON.parse(localStorage.getItem("products")) || []
let x = y[y.length - 1]
h2.innerText = x;
console.log(x)
let bag = "";
for (let i = 0; i < x.length; i++) {
if (x[i] == " ") {
bag += "-";
}
else {
bag += x[i];
}
}
console.log(bag)
fetch(`http://127.0.0.1:9090/${bag}`)
.then((res) => {
return res.json();
})
.then((res) => {
display(res)
localStorage.setItem("storedata",JSON.stringify(res))
})
function display(data) {
container.innerHTML = "";
data.forEach((element) => {
let card = document.createElement("div");
let img = document.createElement("img");
let name = document.createElement("p");
let pricediv = document.createElement("div")
let price = document.createElement("span");
priceRemove = document.createElement("span");
priceRemove.textContent = element.price + 850;
price.textContent = `Rs.${element.price}`;
priceRemove.setAttribute("class", "span1")
price.setAttribute("class", "span2")
pricediv.setAttribute("class", "pricediv")
img.src = element.thumbnail;
name.textContent = element.title;
price.textContent = `Rs.${element.price}`;
pricediv.append(price, priceRemove)
container.append(card);
card.append(img, name, pricediv)
card.addEventListener("click", () => {
let obj = {}
obj.name = element.title;
obj.img = element.thumbnail;
obj.price = element.price;
obj.images = element.images;
obj.id = element.id;
let arr = [];
let recentArr = JSON.parse(localStorage.getItem("recently-viewed")) || [];
if (recentArr.length === 12) {
recentArr.shift();
}
recentArr.push(obj);
arr.push(obj);
localStorage.setItem("single-data", JSON.stringify(arr))
localStorage.setItem("recently-viewed", JSON.stringify(recentArr))
window.location.href = "./single-product-page.html"
})
});
}
let logo = document.getElementById("logo")
logo.addEventListener("click", () => {
window.location.href = "./index.html"
})
let cartcount = document.querySelector(".cartcount");
let cartdata = JSON.parse(localStorage.getItem("cartdata")) || [];
if (cartdata.length === 0) {
cartcount.style.display = "none"
}
cartcount.innerText = cartdata.length;
fetch(`http://127.0.0.1:9090/CHARGING-SOLUTIONS-CATEGORY`)
.then((res) => {
return res.json();
})
.then((res) => {
let filterdata = res[`${bag}`];
console.log(filterdata)
displaycategory(filterdata)
})
let filterEl = document.querySelector("#filter-div");
function displaycategory(data) {
data.forEach((element) => {
let filterCard = document.createElement("div");
filterCard.setAttribute("class", "filterCard");
let image = document.createElement("img");
image.src = element.image;
let name = document.createElement("p");
name.textContent = element.name;
filterCard.append(image, name)
filterEl.append(filterCard);
filterCard.addEventListener("click",()=>{
// console.log(storedata);
filterproduct(element.name)
// if(filterCard.value==="ALL"){
// display(storedata)
// }
// else{
// let filteredData = storedata.filter((element) => {
// if (element.sub-category === filterCard.value) {
// return true;
// }
// else{
// return false;
// }
// })
// display(filteredData);
// }
})
})
}
function filterproduct(cardvalue){
let storedata = JSON.parse(localStorage.getItem("storedata"))||[];
console.log(storedata)
if(cardvalue==="ALL"){
display(storedata)
}
else{
let filteredData = storedata.filter((element) => {
if (element.subcategory === cardvalue) {
return true;
}
else{
return false;
}
})
display(filteredData);
}
}