-
Notifications
You must be signed in to change notification settings - Fork 2
/
products.html
122 lines (102 loc) · 3.77 KB
/
products.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
<!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>Products Page</title>
<link href="./styles/products.css" rel="stylesheet" type="text/css">
</head>
<body>
<!-- Use this Select Tag for Filtering the Products -->
<select id="filter">
<option value="">Filter By Brand</option>
<option value="Printed Fleece">Printed Fleece</option>
<option value="Full Zip">Full Zip</option>
<option value="Interchange">Interchange</option>
<option value="Hooded">Hooded</option>
</select>
<div id="product-container">
<!-- Here Append All the Products -->
</div>
</body>
<script>
async function FetchData(){
try{
let res= await fetch("http://localhost:3000/data")
res=await res.json()
// console.log(res)
FilterData(res)
}catch(err){
console.log(err)
}
}
FetchData()
let filterBy=document.getElementById("filter")
filterBy.addEventListener("change",()=>{
FetchData()
})
function FilterData(data){
let filterValue=filterBy.value
if(filterValue===""){
DisplayProduct(data)
}else{
data=data.filter((product)=>{
// it will return boolean value
return product.brand==filterValue
})
DisplayProduct(data)
}
}
let CartArr=JSON.parse(localStorage.getItem("cart"))||[]
let Cart=JSON.parse(localStorage.getItem("buy"))||[]
let Container=document.getElementById("product-container")
function DisplayProduct(data){
Container.innerHTML="";
data.forEach((product)=>{
let card=document.createElement("div")
let image=document.createElement("img")
let brand=document.createElement("h3")
let category=document.createElement("p")
let details=document.createElement("p")
let price=document.createElement("h4")
let add_to_cart=document.createElement("button")
add_to_cart.textContent="Add to Cart"
let buy_now=document.createElement("button")
buy_now.textContent="Buy Now"
image.src=product.img;
brand.textContent=product.brand;
category.textContent=product.category;
price.textContent=`₹${product.price}`;
details.textContent=product.details
add_to_cart.addEventListener("click",()=>{
if(checkDuplicate(product)){
alert("Product Already in Cart")
}else{
CartArr.push({...product,quantity:1})
localStorage.setItem("cart",JSON.stringify(CartArr))
alert("Product Added To Cart")
}
buy_now.addEventListener("click",()=>{
localStorage.setItem("buy",JSON.stringify(Cart));
})
let button=document.querySelector("buy")
button.addEventListener("click",()=>{
window.open("payment.html","_blank")
})
})
card.append(image,brand,price,details,category,add_to_cart,buy_now)
Container.append(card)
})
}
function checkDuplicate(product){
for(let i=0;i<CartArr.length;i++){
if(CartArr[i].id===product.id){
return true
}
}
return false
}
// Write all necessery JS here
</script>
</html>