-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.js
109 lines (97 loc) · 3.25 KB
/
tools.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
var basketItem=JSON.parse(localStorage.getItem("basketItem"))||[];
var sephoraItemsData=JSON.parse(localStorage.getItem("sephoraItemsData"));
var toolsArr=sephoraItemsData.filter(function(ele){
return ele.category=="tools";
})
createDisplay(toolsArr);
document.querySelector("#all").addEventListener("click",function(){
createDisplay(toolsArr);
});
document.querySelector("#hair").addEventListener("click",hair);
function hair(){
var hairArr=toolsArr.filter(function(ele){
return ele.type=="hair";
})
createDisplay(hairArr);
}
document.querySelector("#beauty").addEventListener("click",beauty);
function beauty(){
var beautyArr=toolsArr.filter(function(ele){
return ele.type=="beauty";
})
createDisplay(beautyArr);
}
document.querySelector("#travelcases").addEventListener("click",travelcases);
function travelcases(){
var travelcasesArr=toolsArr.filter(function(ele){
return ele.type=="travel cases";
})
createDisplay(travelcasesArr);
}
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(toolsArr);
}
else{
if(selected=="low"){
var sortArr=toolsArr.sort(function(a,b){
return a.price-b.price;
})
}
if(selected=="high"){
var sortArr=toolsArr.sort(function(a,b){
return b.price-a.price;
})
}
if(selected=="rated"){
var sortArr=toolsArr.sort(function(a,b){
return b.numReviews-a.numReviews;
})
}
createDisplay(sortArr);
}
}