-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproducts.js
184 lines (130 loc) · 4.4 KB
/
products.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
let productDiv = document.getElementById("right-bottom");
let url = "https://glamorous-helmet-yak.cyclic.app/products";
let bag = [];
let cartItems = JSON.parse(localStorage.getItem("cartProducts")) || [];
fetch(url)
.then((res) => res.json())
.then(data =>
{ bag = data;
displayprod(data);
});
// search function
let searchbtn = document.querySelector("#s")
searchbtn.addEventListener("input",searchfun)
function searchfun(){
let inp = document.querySelector("#s").value
console.log(inp)
let newData = bag.filter(function(elem){
return elem.title.toLowerCase().includes(inp.toLowerCase())
})
if(inp!=""){
displayprod(newData)
}else{
console.log("nothing")
displayprod(bag)
}
}
// filter function
let sortData = document.querySelector("#sort")
sortData.addEventListener("change",sorting)
function sorting(){
let svalue = document.querySelector("#sort").value
// console.log(svalue)
if(svalue=="LTH"){
bag.sort(function(a,b){return a.price - b.price})
}
if(svalue=="HTL"){
bag.sort(function(a,b) {return b.price - a.price} )
}
if(svalue==""){
bag.sort(function(a,b){return a.id - b.id})
}
displayprod(bag)
}
// sort category functionality
function handleCat(){
let selectCat = document.querySelector("#prodType").value;
if(selectCat == "Body Spray"){
let newData = bag.filter(function(elem){
return elem.category.includes("Body Spray")
})
displayprod(newData);
}
if(selectCat == "Body Cream"){
let newData = bag.filter(function(elem){
return elem.category.includes("Body Cream")
})
displayprod(newData);
}
if(selectCat == "Body Lotion"){
let newData = bag.filter(function(elem){
return elem.category.includes("Body Lotion")
})
displayprod(newData);
}
if(selectCat == "Body Wash"){
let newData = bag.filter(function(elem){
return elem.category.includes("Body Wash")
})
displayprod(newData);
}
if(selectCat == "Shower Gel"){
let newData = bag.filter(function(elem){
return elem.category.includes("Shower Gel")
})
displayprod(newData);
}
if(selectCat == "Candle"){
let newData = bag.filter(function(elem){
return elem.category.includes("Candle")
})
displayprod(newData);
}
if(selectCat == "Cleansing Bar"){
let newData = bag.filter(function(elem){
return elem.category.includes("Cleansing Bar")
})
displayprod(newData);
}
if(selectCat == "Hand Soap"){
let newData = bag.filter(function(elem){
return elem.category.includes("Hand Soap")
})
displayprod(newData);
}
if(selectCat == ""){
bag.sort((a,b) => a.id - b.id);
displayprod(bag)
}
}
// Display products
function displayprod(data){
productDiv.innerHTML ="";
data.forEach(function (elem){
let div1 = document.createElement("div");
let imageDis = document.createElement("img")
imageDis.setAttribute("src",elem.image);
let tiTle = document.createElement("h3")
tiTle.innerText = elem.title;
let cateGory = document.createElement("p")
cateGory.innerText = elem.category;
let dealPrice = document.createElement("h2")
dealPrice.innerText =`$${elem.price}`;
let btn = document.createElement("button")
btn.innerText = "ADD TO BAG";
btn.addEventListener("click", function () {
let x = cartItems.filter(data => data.title == elem.title);
if(x.length == 0){
cartItems.push({...elem , count:1});
console.log("inside if")
alert("product added in the cart")
}else{
console.log(x[0].count++, "inside else")
alert("product quantity increased")
}
localStorage.setItem("cartProducts",JSON.stringify(cartItems))
})
div1.append(imageDis,tiTle,cateGory,dealPrice,btn);
productDiv.append(div1);
})
}