-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproduct.js
75 lines (72 loc) · 13.3 KB
/
product.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
let product = [ { "name": "MacBook Pro Save up to $200 on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6450/6450859_sd.jpg;maxHeight=300;maxWidth=450", "price": 115000 }, { "name": "hp victus new 2022.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6503/6503849_sd.jpg;maxHeight=400;maxWidth=400", "price": 25000 }, { "name": "apple ipad new series.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/5985/5985620_sd.jpg;maxHeight=300;maxWidth=450", "price": 35000 }, { "name": "airpods 2334 .", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/4900/4900944_sd.jpg;maxHeight=640;maxWidth=550", "price": 15000 }, { "name": "hey mANbooh range 34.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/GL-73044-flex-persona-middle-220516-5cb33289-f4ed-4e09-ac3e-2ba0e695496b.jpg;maxHeight=432;maxWidth=432", "price": 15200 }, { "name": "bread roster", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/GL-73044-flex-persona-college-220516-7a6d93f6-df65-46e3-be9c-a114e1204842.jpg;maxHeight=432;maxWidth=432", "price": 1560 }, { "name": "for safe dads", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/GL-72996-flex-persona-outdoors-220509-510c4e8d-b671-4e4c-b1ec-8fd8c41bcd13.jpg;maxHeight=432;maxWidth=432", "price": 15564 }, { "name": "for excercise lovers", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/GL-72996-flex-persona-fitness-220509-9d40595e-4ce1-4d7d-8eb4-9d0c9c26e64f.jpg;maxHeight=432;maxWidth=432", "price": 150000 }, { "name": "new range of washing machine", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/pol_MMT-549800_der-b2458fef-e15a-4f61-87e6-66976b17ba3a.jpg;maxHeight=504;maxWidth=740", "price": 10400 }, { "name": "all watch of 2022.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/MMT-549477_pol_COPE_DER-bb7580c0-afa1-4b6b-9775-d78aa1be89e3.jpg;maxHeight=504;maxWidth=740", "price": 17000 }, { "name": "samsung 55inch led tv", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6401/6401735_sd.jpg;maxHeight=272;maxWidth=400", "price": 150060 }, { "name": "Go Pro camera", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6474/6474501_rd.jpg;maxHeight=272;maxWidth=400", "price": 40000 }, { "name": "Dell Gaming Laptop on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6470/6470463_sd.jpg;maxHeight=272;maxWidth=400", "price": 85000 }, { "name": "New Range of cameras", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/pol-MMT-552676-220606_DER-b3ba4051-f64a-439b-88f2-99e7308c0c6a.jpg;maxHeight=504;maxWidth=740", "price": 38000 }, { "name": "Window Air Conditinor", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6390/6390677_rd.jpg;maxHeight=640;maxWidth=550", "price": 45000 }, { "name": "Energy Guide For all range of Air Conditinors.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6390/6390677_eg.jpg", "price": 150 }, { "name": "MacBook Pro Save up to $200 on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6450/6450859_sd.jpg;maxHeight=300;maxWidth=450", "price": 115000 }, { "name": "hp victus new 2022.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6503/6503849_sd.jpg;maxHeight=400;maxWidth=400", "price": 25000 }, { "name": "apple ipad new series.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/5985/5985620_sd.jpg;maxHeight=300;maxWidth=450", "price": 35000 }, { "name": "airpods 2334 .", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/4900/4900944_sd.jpg;maxHeight=640;maxWidth=550", "price": 15000 }, { "name": "hey mANbooh range 34.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/GL-73044-flex-persona-middle-220516-5cb33289-f4ed-4e09-ac3e-2ba0e695496b.jpg;maxHeight=432;maxWidth=432", "price": 15200 }, { "name": "bread roster", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/GL-73044-flex-persona-college-220516-7a6d93f6-df65-46e3-be9c-a114e1204842.jpg;maxHeight=432;maxWidth=432", "price": 1560 }, { "name": "for safe dads", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/GL-72996-flex-persona-outdoors-220509-510c4e8d-b671-4e4c-b1ec-8fd8c41bcd13.jpg;maxHeight=432;maxWidth=432", "price": 15564 }, { "name": "for excercise lovers", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/GL-72996-flex-persona-fitness-220509-9d40595e-4ce1-4d7d-8eb4-9d0c9c26e64f.jpg;maxHeight=432;maxWidth=432", "price": 150000 }, { "name": "new range of washing machine", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/pol_MMT-549800_der-b2458fef-e15a-4f61-87e6-66976b17ba3a.jpg;maxHeight=504;maxWidth=740", "price": 10400 }, { "name": "all watch of 2022.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/MMT-549477_pol_COPE_DER-bb7580c0-afa1-4b6b-9775-d78aa1be89e3.jpg;maxHeight=504;maxWidth=740", "price": 17000 }, { "name": "samsung 55inch led tv", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6401/6401735_sd.jpg;maxHeight=272;maxWidth=400", "price": 150060 }, { "name": "Go Pro camera", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6474/6474501_rd.jpg;maxHeight=272;maxWidth=400", "price": 40000 }, { "name": "Dell Gaming Laptop on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6470/6470463_sd.jpg;maxHeight=272;maxWidth=400", "price": 85000 }, { "name": "New Range of cameras", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/pol-MMT-552676-220606_DER-b3ba4051-f64a-439b-88f2-99e7308c0c6a.jpg;maxHeight=504;maxWidth=740", "price": 38000 }, { "name": "Window Air Conditinor", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6390/6390677_rd.jpg;maxHeight=640;maxWidth=550", "price": 45000 }, { "name": "Energy Guide For all range of Air Conditinors.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6390/6390677_eg.jpg", "price": 150 }, { "name": "MacBook Pro Save up to $200 on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6450/6450859_sd.jpg;maxHeight=300;maxWidth=450", "price": 115000 }, { "name": "hp victus new 2022.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6503/6503849_sd.jpg;maxHeight=400;maxWidth=400", "price": 25000 }, { "name": "apple ipad new series.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/5985/5985620_sd.jpg;maxHeight=300;maxWidth=450", "price": 35000 }, { "name": "airpods 2334 .", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/4900/4900944_sd.jpg;maxHeight=640;maxWidth=550", "price": 15000 }, { "name": "hey mANbooh range 34.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/GL-73044-flex-persona-middle-220516-5cb33289-f4ed-4e09-ac3e-2ba0e695496b.jpg;maxHeight=432;maxWidth=432", "price": 15200 }, { "name": "bread roster", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/GL-73044-flex-persona-college-220516-7a6d93f6-df65-46e3-be9c-a114e1204842.jpg;maxHeight=432;maxWidth=432", "price": 1560 }, { "name": "for safe dads", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/GL-72996-flex-persona-outdoors-220509-510c4e8d-b671-4e4c-b1ec-8fd8c41bcd13.jpg;maxHeight=432;maxWidth=432", "price": 15564 }, { "name": "for excercise lovers", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/GL-72996-flex-persona-fitness-220509-9d40595e-4ce1-4d7d-8eb4-9d0c9c26e64f.jpg;maxHeight=432;maxWidth=432", "price": 150000 }, { "name": "new range of washing machine", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/pol_MMT-549800_der-b2458fef-e15a-4f61-87e6-66976b17ba3a.jpg;maxHeight=504;maxWidth=740", "price": 10400 }, { "name": "all watch of 2022.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/MMT-549477_pol_COPE_DER-bb7580c0-afa1-4b6b-9775-d78aa1be89e3.jpg;maxHeight=504;maxWidth=740", "price": 17000 }, { "name": "samsung 55inch led tv", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6401/6401735_sd.jpg;maxHeight=272;maxWidth=400", "price": 150060 }, { "name": "Go Pro camera", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6474/6474501_rd.jpg;maxHeight=272;maxWidth=400", "price": 40000 }, { "name": "Dell Gaming Laptop on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6470/6470463_sd.jpg;maxHeight=272;maxWidth=400", "price": 85000 }, { "name": "New Range of cameras", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/dam/pol-MMT-552676-220606_DER-b3ba4051-f64a-439b-88f2-99e7308c0c6a.jpg;maxHeight=504;maxWidth=740", "price": 38000 }, { "name": "Window Air Conditinor", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6390/6390677_rd.jpg;maxHeight=640;maxWidth=550", "price": 45000 }, { "name": "Energy Guide For all range of Air Conditinors.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6390/6390677_eg.jpg", "price": 150 }, { "name": "MacBook Pro Save up to $200 on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6450/6450859_sd.jpg;maxHeight=300;maxWidth=450", "price": 150000 }, { "name": "MacBook Pro Save up to $200 on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6450/6450859_sd.jpg;maxHeight=300;maxWidth=450", "price": 150000 }, { "name": "MacBook Pro Save up to $200 on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6450/6450859_sd.jpg;maxHeight=300;maxWidth=450", "price": 150000 }, { "name": "MacBook Pro Save up to $200 on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6450/6450859_sd.jpg;maxHeight=300;maxWidth=450", "price": 150000 }, { "name": "MacBook Pro Save up to $200 on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6450/6450859_sd.jpg;maxHeight=300;maxWidth=450", "price": 150000 }, { "name": "MacBook Pro Save up to $200 on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6450/6450859_sd.jpg;maxHeight=300;maxWidth=450", "price": 150000 }, { "name": "MacBook Pro Save up to $200 on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6450/6450859_sd.jpg;maxHeight=300;maxWidth=450", "price": 150000 }, { "name": "MacBook Pro Save up to $200 on select models.", "image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6450/6450859_sd.jpg;maxHeight=300;maxWidth=450", "price": 150000 }]
function setData(){
localStorage.setItem("products", JSON.stringify(product));
}
setData();
let productData = JSON.parse(localStorage.getItem("products")) || [];
document.querySelector("#filter").addEventListener("change", handleSorting)
function handleSorting() {
let filter = document.querySelector("#filter").value;
if (filter == "All") {
displayProduct(productData)
window.location.reload();
}
if (filter == "low-to-high") {
productData.sort(function (a, b) {
return a.price - b.price;
})
displayProduct(productData)
}
if (filter == "high-to-low") {
productData.sort(function (a, b) {
return b.price - a.price;
})
displayProduct(productData)
}
}
function displayProduct(productData) {
document.querySelector("#productPage").innerHTML = "";
productData.forEach(function (elem) {
let parent_box = document.createElement("div");
parent_box.setAttribute("id", "card");
let image = document.createElement("img");
image.setAttribute("src", elem.image_url);
image.setAttribute("id", "pimg");
let name = document.createElement("h3");
name.innerText = elem.name;
name.setAttribute("id", "pname");
let price = document.createElement("p");
price.innerText = "Price - " + elem.price;
price.setAttribute("id", "pprice");
let button = document.createElement("button");
button.innerText = "Add to cart";
button.setAttribute("id", "pbutton");
button.addEventListener("click", function () {
addToCart(elem)
});
parent_box.append(image, name, price, button);
document.querySelector("#productPage").append(parent_box)
})
}
displayProduct(productData)
let cartProduct = JSON.parse(localStorage.getItem("cartProduct")) || [];
function addToCart(elem) {
let name = elem.name;
let filtered = cartProduct.filter(function (elem) {
if (name == elem.name) {
return elem;
}
})
if (filtered.length > 0) {
alert("Product already in cart");
} else {
cartProduct.push(elem)
localStorage.setItem("cartProduct", JSON.stringify(cartProduct))
alert("Added successfully to Cart")
}
}
// document.querySelector("#signOut").addEventListener("click", signOut)
// function signOut() {
// localStorage.setItem("system", "offline");
// window.location.href = "Best_Buy.html"
// }