-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnp.html
149 lines (140 loc) · 4.19 KB
/
np.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
<!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>product</title>
<style>
/* Write all necessery css here */
#product-container {
/* border: 2px solid red; */
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 20px;
}
#product-container > div {
background-color: #EFEBE9;
box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
padding: 20px;
}
#product-container > div > img {
width: 100%;
height: 200px;
}
#product-container > div > button {
display: block;
margin: auto;
}
#filter {
display: block;
margin: auto;
font-size: 15px;
padding: 10px;
margin-top: 10px;
margin-bottom: 10px;
}
.nav {
display: flex;
align-items: center;
justify-content: space-around;
box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
background-color: #232f3e;
color: aliceblue;
}
.nav a{
color: white;
text-decoration: none;
}
</style>
</head>
<body>
<div class="nav">
<a href="./index.html"><img src="./logo.png" alt=""></a>
<h1>Avira @ Your DoorStep</h1>
<a href="./index.html">Home</a>
<a href="./ncart.html">Cart</a>
</div>
<!-- Use this Select Tag for Filtering the Products -->
<select id="filter">
<option value="">Filter By Category</option>
<option value="Laptop">Laptop</option>
<option value="Mobile">Mobile</option>
<option value="Speakers">Speakers</option>
</select>
<div id="product-container">
<!-- Here Append All the Products -->
</div>
</body>
<script>
// Write all necessery JS here
async function FetchData() {
try {
let res = await fetch(
"https://dbioz2ek0e.execute-api.ap-south-1.amazonaws.com/mockapi/get-tech-products"
);
res = await res.json();
FilterData(res.data);
// console.log(data)
} catch (err) {
console.log("error", err);
}
}
FetchData();
let filterby = document.getElementById("filter");
filterby.addEventListener("change", () => {
FetchData();
});
function FilterData(data) {
if (filterby.value === "") {
DisplayData(data);
} else {
data = data.filter((ele) => {
return ele.category == filterby.value;
});
DisplayData(data);
}
// console.log("data",data)
}
let Cart = JSON.parse(localStorage.getItem("cart")) || [];
let Container = document.getElementById("product-container");
function DisplayData(data) {
Container.innerHTML = "";
data.forEach((product) => {
let Card = document.createElement("div");
let Image = document.createElement("img");
let Brand = document.createElement("h2");
let Category = document.createElement("h4");
let Price = document.createElement("h3");
let Detail = document.createElement("p");
let AddtoCart = document.createElement("button");
Image.src = product.img;
Brand.textContent = product.brand;
Category.textContent = product.category;
Price.textContent = `₹${product.price}`;
Detail.textContent = product.details;
AddtoCart.textContent = "Add To Cart";
AddtoCart.addEventListener("click", () => {
if (checkDuplicate(product)) {
alert("Product Already in Cart");
} else {
Cart.push({ ...product, quantity: 1 });
localStorage.setItem("cart", JSON.stringify(Cart));
alert("Product Added To Cart");
}
});
Card.append(Image, Brand, Price, Detail, Category, AddtoCart);
Container.append(Card);
});
console.log("display", data);
}
function checkDuplicate(product) {
for (let i = 0; i < Cart.length; i++) {
if (Cart[i].id == product.id) {
return true;
}
}
return false;
}
</script>
</html>