-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
141 lines (121 loc) · 5.03 KB
/
script.js
File metadata and controls
141 lines (121 loc) · 5.03 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
134
135
136
137
138
139
140
141
const apiUrl = "https://fakestoreapi.com/products";
let currentIndex = 0;
const itemsPerPage = 4;
let products = [];
// Fetch products from the API
async function fetchProducts() {
try {
const response = await fetch(apiUrl);
products = await response.json();
displayProducts();
} catch (error) {
console.error("Error fetching products:", error);
}
}
// Display initial products
function displayProducts() {
const productGrid = document.querySelector('.product-grid');
const productsToDisplay = products.slice(currentIndex, currentIndex + itemsPerPage);
productsToDisplay.forEach(product => {
const productItem = document.createElement('div');
productItem.classList.add('product-item');
productItem.innerHTML = `
<img src="${product.image}" alt="${product.title}">
<h3>${product.title}</h3>
<p>⭐Rating: ${product.rating.rate} (${product.rating.count} reviews)</p>
<p>₹${product.price}</p>
<button class="add-to-cart-btn" onclick="addToCart(${product.id}, '${product.title}', ${product.price}, '${product.image}')">Add to Cart</button>
`;
productGrid.appendChild(productItem);
});
currentIndex += itemsPerPage;
document.getElementById('show-more-btn').style.display = currentIndex < products.length ? 'block' : 'none';
}
// Search functionality
document.getElementById('search-input').addEventListener('input', function () {
const searchTerm = this.value.toLowerCase();
const filteredProducts = products.filter(product =>
product.title.toLowerCase().includes(searchTerm)
);
currentIndex = 0;
document.querySelector('.product-grid').innerHTML = '';
filteredProducts.forEach(product => {
const productItem = document.createElement('div');
productItem.classList.add('product-item');
productItem.innerHTML = `
<img src="${product.image}" alt="${product.title}">
<h3>${product.title}</h3>
<p>⭐Rating: ${product.rating.rate} (${product.rating.count} reviews)</p>
<p>₹${product.price}</p>
<button class="add-to-cart-btn" onclick="addToCart(${product.id}, '${product.title}', ${product.price}, '${product.image}')">Add to Cart</button>
`;
document.querySelector('.product-grid').appendChild(productItem);
});
document.getElementById('show-more-btn').style.display = filteredProducts.length > itemsPerPage ? 'block' : 'none';
});
// Show more products
document.getElementById('show-more-btn').addEventListener('click', () => {
displayProducts();
});
// Initial fetch and setup
fetchProducts();
let cart = [];
function addToCart(id, title, price, image) {
const existingProduct = cart.find(item => item.id === id);
if (existingProduct) {
existingProduct.quantity += 1;
} else {
cart.push({ id, title, price, image, quantity: 1 });
}
updateCart();
document.getElementById('cart-section').scrollIntoView({ behavior: 'smooth' });
}
function removeFromCart(id) {
cart = cart.filter(item => item.id !== id);
updateCart();
}
function updateCart() {
const cartItemsSection = document.querySelector('.cart-items-section');
cartItemsSection.innerHTML = '';
let totalMRP = 0;
cart.forEach(item => {
totalMRP += item.price * item.quantity;
const cartItem = document.createElement('div');
cartItem.classList.add('cart-item');
cartItem.innerHTML = `
<img class="product-img" src="${item.image}" alt="${item.title}">
<div class="product-details">
<h3>${item.title}</h3>
<p>₹${item.price} x ${item.quantity}</p>
</div>
<div class="quantity-control">
<button class="quantity-btn" onclick="changeQuantity(${item.id}, -1)">-</button>
<span class="quantity">${item.quantity}</span>
<button class="quantity-btn" onclick="changeQuantity(${item.id}, 1)">+</button>
<button class="remove-item-btn" onclick="removeFromCart(${item.id})">×</button>
</div>
`;
cartItemsSection.appendChild(cartItem);
});
updatePriceDetails(totalMRP);
}
function updatePriceDetails(totalMRP) {
const totalAmountElement = document.querySelector('.price-details-section h3 span');
totalAmountElement.innerText = totalMRP;
const placeOrderButton = document.querySelector('.place-order-btn');
placeOrderButton.disabled = totalMRP === 0;
}
updatePriceDetails(0);
function changeQuantity(id, delta) {
const product = cart.find(item => item.id === id);
if (product) {
product.quantity += delta;
if (product.quantity <= 0) {
removeFromCart(id);
} else {
updateCart();
}
}
}
document.getElementById('show-more-btn').addEventListener('click', fetchProducts);
fetchProducts();