-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (105 loc) · 3.66 KB
/
Copy pathscript.js
File metadata and controls
133 lines (105 loc) · 3.66 KB
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
/* https://fakestoreapi.in/api/products */
let productsSection = document.getElementById("productssection");
// console.log(productsSection);
async function getProducts() {
try {
let response = await fetch("https://fakestoreapi.in/api/products");
let data = await response.json();
// console.log(data.products);
displayAllProducts(data.products); // ! Sending products as an argument
}
catch (error) {
console.log("error while fetching products", error);
}
}
getProducts();
function displayAllProducts(allProducts) {
// console.log(allProducts);
allProducts.map((ele) => {
// console.log(ele);
// ! Creating elements
let cardContainer = document.createElement("article");
// let productImage = document.createElement("img");
// let productTitle = document.createElement("h2");
// let productPrice = document.createElement("h4");
// ! setting attribute
cardContainer.setAttribute("class", "cardcontainer");
// productImage.setAttribute("src", ele.image);
// productImage.setAttribute("alt", ele.title);
//! adding text content
// productTitle.textContent = `${ele.title.slice(0,30)}...`;
// productPrice.textContent =``
cardContainer.innerHTML = `
<div>
<img src =${ele.image}>
<h2>${ele.title.slice(0, 60)}...</h2>
</div>
<h4>Rs.${ele.price}</h4>
<button onclick = 'handleAddToCard(${JSON.stringify(ele)})'>Add to Cart</button>
`;
//! append element
// cardContainer.append(productImage,productTitle);
// console.log(cardContainer);
productsSection.append(cardContainer);
});
}
function handleAddToCard(product) {
console.log("Added to cart");
console.log(product);
// console.log(product.id);
// console.log(product.title);
// console.log(product.price);
// console.log(product.image);
//! initializing cart variable
let cart = JSON.parse(localStorage.getItem("cart")) || [];
//! finding index of exesting products
let exestingProductsIndex = cart.findIndex((ele) => ele.id === product.id);
if (exestingProductsIndex !== -1) {
cart[exestingProductsIndex].quantity += 1;
}
else {
cart.push({ ...product, quantity: 1 });
}
//! store cart in localStorage
localStorage.setItem("cart", JSON.stringify(cart));
//! alert msg
alert(`${product.title} Added to Cart`);
displayCartItems();
}
function displayCartItems() {
let cart = JSON.parse(localStorage.getItem("cart")) || [];
// console.log(cart);
let cartsection = document.getElementById("cartsection");
cartsection.innerHTML = "<h1>My Cart</h1>";
if (cart.length === 0) {
cartsection.innerHTML = "<h1>Cart is empty</h1>"
}
else {
cart.map((item,index) => {
let div = document.createElement("div");
div.innerHTML = `
<img src = '${item.image}'>
<h2>${item.title}</h2>
<p>Quantity ${item.quantity}</p>
<p>price ${item.quantity * item.price}</p>
<button onclick ='removeCartItem(${index})'>remove</button>
`;
cartsection.append(div);
});
}
}
function removeCartItem(index){
console.log("Removed",index);
let cart = JSON.parse(localStorage.getItem("cart")) || [];
if (cart[index].quantity >1) {
cart[index].quantity -= 1
}
else{
cart.splice(index,1)
}
localStorage.setItem("cart" ,JSON.stringify(cart));
displayCartItems();
}
window.addEventListener("load", () => {
displayCartItems();
});