-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcart.html
100 lines (92 loc) · 3.68 KB
/
cart.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cart</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="assets/css/profile-p.css">
<link rel="stylesheet" href="Sanskar's_work/testcart.css">
</head>
<body>
<nav class="navbar">
<div class="container">
<div class="fillkart">
FILLKART
</div>
<div class="nav-links">
<a href="/index.html">Our Products</a>
<a href="#">New Arrivals</a>
<a href="/profile.html">Sign up/Profile</a>
<a class="active" href="#" class="cart-icon"><i class="fas fa-shopping-cart"></i></a>
</div>
</div>
</nav>
<div class="product-grid" id="product-grid">
<!-- Product Cards will be inserted here -->
</div>
<script >
const retFun = () => {
const retArr = JSON.parse(localStorage.getItem('products')) || [];
return retArr;
}
const createProductCard = (product, index) => {
return `
<div class="product-card">
<img class="product-img" src="${product.url}" alt="${product.productName}">
<div class="product-details">
<h3 class="product-name">${product.productName}</h3>
<p class="product-price">${product.price}</p>
<a href="#" class="remove-from-cart" data-index="${index}">REMOVE</a>
</div>
</div>
`;
};
const cartContainer = document.getElementById('product-grid');
const cart = retFun();
if (cart.length > 0) {
cart.forEach((product) => {
cartContainer.innerHTML += createProductCard(product);
});
} else {
cartContainer.innerHTML = `
<div class="fixed inset-0 bg-gray-800 bg-opacity-50 flex items-center justify-center">
<div class="bg-white p-10 rounded-lg shadow-lg text-center">
<h1 class="text-3xl font-bold mb-8">Your Cart is Empty</h1>
<p class="text-gray-600 mb-6">Looks like you haven't added anything to your cart yet.</p>
<a href="/index.html" class="bg-blue-500 text-white px-6 py-2 rounded-lg hover:bg-blue-600">Browse Products</a>
</div>
</div>
`;
}
// addRemoveEventListeners();
// };
// remove functionality
cartContainer.addEventListener('click', (e) => {
if (e.target.classList.contains('remove-from-cart')) {
const index = e.target.getAttribute('data-index');
cart.splice(index, 1);
localStorage.setItem('products', JSON.stringify(cart));
cartContainer.innerHTML = '';
if (cart.length > 0) {
cart.forEach((product, index) => {
cartContainer.innerHTML += createProductCard(product, index);
});
} else {
cartContainer.innerHTML = `
<div class="fixed inset-0 bg-gray-800 bg-opacity-50 flex items-center justify-center">
<div class="bg-white p-10 rounded-lg shadow-lg text-center">
<h1 class="text-3xl font-bold mb-8">Your Cart is Empty</h1>
<p class="text-gray-600 mb-6">Looks like you haven't added anything to your cart yet.</p>
<a href="/index.html" class="bg-blue-500 text-white px-6 py-2 rounded-lg hover:bg-blue-600">Browse Products</a>
</div>
</div>
`;
}
alert("item removed")
}
});
</script>
</body>
</html>