-
Notifications
You must be signed in to change notification settings - Fork 2
/
bath.js
117 lines (104 loc) · 3.51 KB
/
bath.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
var basketItem=JSON.parse(localStorage.getItem("basketItem"))||[];
var sephoraItemsData=JSON.parse(localStorage.getItem("sephoraItemsData"));
var bathArr=sephoraItemsData.filter(function(ele){
return ele.category=="bath";
})
createDisplay(bathArr);
document.querySelector("#all").addEventListener("click",function(){
createDisplay(bathArr);
});
document.querySelector("#shower").addEventListener("click",shower);
function shower(){
var showerArr=bathArr.filter(function(ele){
return ele.type=="shower";
})
createDisplay(showerArr);
}
document.querySelector("#bodymoisturizers").addEventListener("click",bodymoisturizers);
function bodymoisturizers(){
var bodymoisturizersArr=bathArr.filter(function(ele){
return ele.type=="body moisturizers";
})
createDisplay(bodymoisturizersArr);
}
document.querySelector("#bodycare").addEventListener("click",bodycare);
function bodycare(){
var bodycareArr=bathArr.filter(function(ele){
return ele.type=="body care";
})
createDisplay(bodycareArr);
}
document.querySelector("#tanners").addEventListener("click",tanners);
function tanners(){
var tannersArr=bathArr.filter(function(ele){
return ele.type=="tanners";
})
createDisplay(tannersArr);
}
function createDisplay(arr){
document.querySelector("#items").textContent="";
document.querySelector("#total").innerHTML=arr.length+" Results Found";
arr.map(function(ele){
var img1=document.createElement("img");
var head1=document.createElement("p");
var p1=document.createElement("h4");
var head2=document.createElement("h4");
var p2=document.createElement("p");
var head3=document.createElement("h3");
var div1=document.createElement("div");
div1.setAttribute("id","rate");
head1.setAttribute("id","dis");
var div2=document.createElement("div");
var btn=document.createElement("button");
btn.setAttribute("id","addToCart");
img1.setAttribute("id","images");
img1.setAttribute("src",ele.imageUrl);
head1.textContent=ele.name;
var i=document.createElement("i");
p1.textContent=ele.brand;
head2.innerHTML=ele.stars+'<i class="fas fa-star"></i>'+" "+ele.numReviews+" (Reviews)";
i.setAttribute("id","i");
head3.textContent="Price is: $"+ele.price;
head3.setAttribute("id","price");
btn.textContent="Add to Basket ";
btn.addEventListener("click",function(){
addItem(ele);
});
head2.style.margin="20px";
div1.append(head2);
div2.append(img1,p1,head1,div1,head3,btn);
div2.style.textAlign="center";
div2.style.height="400px"
document.querySelector("#items").append(div2);
})
}
function addItem(ele){
basketItem.push(ele);
alert("Successfully Added to the Basket");
localStorage.setItem("basketItem",JSON.stringify(basketItem));
}
function sorting(){
var selected=document.querySelector("#selectSort").value;
if(selected=="")
{
createDisplay(bathArr);
}
else{
if(selected=="low"){
var sortArr=bathArr.sort(function(a,b){
return a.price-b.price;
})
}
if(selected=="high"){
var sortArr=bathArr.sort(function(a,b){
return b.price-a.price;
})
}
if(selected=="rated"){
var sortArr=bathArr.sort(function(a,b){
return b.numReviews-a.numReviews;
})
}
createDisplay(sortArr);
}
}