-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
120 lines (100 loc) Β· 3.84 KB
/
Copy pathscript.js
File metadata and controls
120 lines (100 loc) Β· 3.84 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
// β
Selecting all important elements
const container = document.querySelector('.container');
const cartPanel = document.getElementById('cartPanel');
const cartItemsDiv = document.getElementById('cartItems');
const closeCart = document.getElementById('closeCart');
const cartCount = document.getElementById('cart-count');
const cartIcon = document.getElementById('cart-icon');
const totalPriceEl = document.getElementById('totalPrice');
const checkoutBtn = document.getElementById('checkoutBtn');
// β
Get cart data from localStorage (if empty β make new array)
let cart = JSON.parse(localStorage.getItem('cart')) || [];
cartCount.textContent = cart.length;
// β
Fetch products from API using Axios
axios('https://fakestoreapi.com/products')
.then((res) => {
res.data.forEach((item) => {
// β
Create product card for each item
container.innerHTML += `
<div class="card">
<img src="${item.image}" alt="${item.title}">
<h3>${item.title}</h3>
<h4>π²${item.price}</h4>
<button onclick="addToCart('${item.image}', '${item.title}', ${item.price})">
Add to Cart
</button>
</div>
`;
});
})
.catch((err) => console.log('Error:', err));
// β
Function: Add item to cart
function addToCart(image, title, price) {
const product = { image, title, price }; // Create product object
cart.push(product); // Add product to array
localStorage.setItem('cart', JSON.stringify(cart)); // Save to storage
cartCount.textContent = cart.length; // Update count
showCartItems(); // Refresh cart items
cartPanel.classList.add('show'); // Open cart panel
}
// β
Function: Display all cart items
function showCartItems() {
cartItemsDiv.innerHTML = ""; // Clear old items
let total = 0;
if (cart.length === 0) {
cartItemsDiv.innerHTML = "<p>No items in cart π</p>";
totalPriceEl.textContent = "Total: $0.00";
return;
}
// β
Loop through all cart items
cart.forEach((item, i) => {
total += item.price; // Add each item price
cartItemsDiv.innerHTML += `
<div style="display:flex;align-items:center;justify-content:space-between;">
<img src="${item.image}" alt="">
<div style="flex:1;margin-left:10px;">
<p>${item.title}</p>
<strong>$${item.price}</strong>
</div>
<button onclick="removeItem(${i})">β</button>
</div>
`;
});
// β
Update total price
totalPriceEl.textContent = `Total: $${total.toFixed(2)}`;
}
// β
Function: Remove item from cart
function removeItem(index) {
cart.splice(index, 1); // Delete one item
localStorage.setItem('cart', JSON.stringify(cart)); // Update storage
cartCount.textContent = cart.length;
showCartItems(); // Refresh list
}
const modal = document.getElementById('successModal');
const modalAmount = document.getElementById('modalAmount');
const closeModal = document.getElementById('closeModal');
// β
Checkout β Show modal
checkoutBtn.onclick = () => {
let total = cart.reduce((sum, item) => sum + item.price, 0);
if (cart.length === 0) return alert("π Your cart is empty!");
modalAmount.textContent = `Total Amount: $${total.toFixed(2)}`;
modal.style.display = "flex"; // show modal
cart = [];
localStorage.removeItem('cart');
cartCount.textContent = 0;
showCartItems();
};
// β
Close modal
closeModal.onclick = () => modal.style.display = "none";
// β
Open & Close Cart Panel
cartIcon.onclick = () => cartPanel.classList.add('show');
closeCart.onclick = () => cartPanel.classList.remove('show');
// β
Run when page loads
showCartItems();
// πΉ Logout functionality
const logoutBtn = document.getElementById('logoutBtn');
logoutBtn.addEventListener('click', () => {
alert('π You have logged out!');
localStorage.removeItem('currentUser'); // remove user
window.location.href = 'index.html'; // redirect
});