-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.html
174 lines (143 loc) · 4.58 KB
/
product.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fetch Product IW</title>
<style>
body{
text-align: center;
/* background-color: rgb(241, 175, 184); */
}
#container{
display: grid;
grid-template-columns: repeat(4,1fr);
grid-template-rows: auto;
gap: 20px;
margin-top: 2%;
}
#container>div{
box-shadow: rgba(0, 0, 0, 0.07) 0px 1px 1px, rgba(0, 0, 0, 0.07) 0px 2px 2px, rgba(0, 0, 0, 0.07) 0px 4px 4px, rgba(0, 0, 0, 0.07) 0px 8px 8px, rgba(0, 0, 0, 0.07) 0px 16px 16px;
text-align: center;
}
#container>div>img{
width:80%;
}
#container>div>img:hover{
width: 90%;
}
button{
width: 150px;
height: 25px;
background-color: aquamarine;
border-radius: 20px;
}
input{
width: 400px;
height: 20px;
background-color: rgb(214, 214, 214);
}
/* Medium screens */
@media all and (min-width: 451px) and (max-width: 750px) {
#container{
grid-template-columns: repeat(2,1fr);
}
}
/* Small screens */
@media all and (min-width: 100px) and (max-width: 450px) {
#container{
grid-template-columns: repeat(1,1fr);
}
}
</style>
</head>
<body>
<h3> <a style="text-decoration: none;" href="./cart.html">Cart Page</a></h3>
<input oninput="Add_cart()" type="text" placeholder="search productshere" ><br><br>
<select id="price" onchange="Sorting()">
<option value="">search by price</option>
<option value="LTH">Lower To Higher price</option>
<option value="HTL">Higher To Lower price</option>
</select>
<br><br>
<select id="rating" onchange="Sorting()">
<option value="">search by rating</option>
<option value="LTH">Lower To Higher rate product</option>
<option value="HTL">Higher To Lower rate product</option>
</select>
<div id="container">
</div>
</body>
</html>
<script>
let bag=[];
let cartitem=JSON.parse(localStorage.getItem("cartproduct"))||[];
let url="https://makeup-api.herokuapp.com/api/v1/products.json?brand=maybelline"
fetch(url)
.then ((res)=>res.json())
.then((data)=>{
bag = data;
console.log(data);
displayTable(data)
})
.catch((err)=>alert(err));
function Add_cart(){
let q=document.querySelector("input").value;
console.log(q)
let newSearch = bag.filter((elem)=>{
return elem.product_type.toLowerCase().includes(q.toLowerCase())
});
console.log(newSearch);
displayTable(newSearch)
}
function Sorting(){
let sorted = document.querySelector("#price").value
if(sorted == "LTH"){
bag.sort((a,b)=>a.price-b.price);
}
if(sorted=="HTL") {
bag.sort((a,b)=>b.price-a.price);
}
displayTable(bag)
}
function Sorting(){
let sorted = document.querySelector("#rating").value
if(sorted == "LTH"){
bag.sort((a,b)=>a.rating-b.rating);
}
if(sorted=="HTL") {
bag.sort((a,b)=>b.rating-a.rating);
}
displayTable(bag)
}
function displayTable(array){
console.log(array)
document.querySelector("#container").innerHTML="";
array.forEach((element) => {
let div=document.createElement("div")
let img=document.createElement("img")
img.setAttribute("src",element.image_link)
let name=document.createElement("h4")
name.innerText=element.name;
let id=document.createElement("h3")
id.innerText=element.id;
let category=document.createElement("p")
category.innerText=element.category;
let product=document.createElement("p")
product.innerText=element.product_type;
let price=document.createElement("h3")
price.innerText=element.price;
let rating=document.createElement("h4")
rating.innerText=element.rating
let button=document.createElement("button")
button.innerText="add to cart"
button.addEventListener("click",function(){
cartitem.push(element)
localStorage.setItem("cartproduct",JSON.stringify(cartitem))
})
div.append(img,name,id,category,product,price,rating,button)
document.querySelector("#container").append(div)
});
}
</script>