const catalog = [
{ id: 1, name: "Code from Zero", price: 25 },
{ id: 2, name: "Bit mug", price: 8 },
{ id: 3, name: "Bit sticker", price: 3 },
{ id: 4, name: "Bit T-shirt", price: 15 },
];
const cart = [];
// What the reader has typed so far.
let searchText = "";
// Draw one card per product
function renderProducts() {
const container = document.querySelector("#products");
container.replaceChildren();
// Only the products whose names contain the text, ignoring capital letters.
const visible = catalog.filter((p) => p.name.toLowerCase().includes(searchText.toLowerCase()));
if (visible.length === 0) {
container.textContent = "No products match.";
}
for (const product of visible) {
const card = document.createElement("article");
card.className = "card";
const name = document.createElement("h3");
name.textContent = product.name;
const price = document.createElement("p");
price.textContent = `$${product.price}`;
const button = document.createElement("button");
button.textContent = "Add to cart";
button.addEventListener("click", () => addToCart(product.id));
card.append(name, price, button);
container.append(card);
}
}
function addToCart(productId) {
const line = cart.find((item) => item.productId === productId);
if (line) {
line.quantity++;
} else {
cart.push({ productId, quantity: 1 });
}
renderCart();
}
function removeFromCart(productId) {
const i = cart.findIndex((item) => item.productId === productId);
if (i !== -1) {
cart.splice(i, 1);
}
renderCart();
}
// Redraw the cart from the data every time it changes
function renderCart() {
const list = document.querySelector("#cart-lines");
list.replaceChildren();
let total = 0;
for (const item of cart) {
const product = catalog.find((p) => p.id === item.productId);
const lineTotal = product.price * item.quantity;
total += lineTotal;
const li = document.createElement("li");
li.textContent = `${product.name} × ${item.quantity}`;
const amount = document.createElement("span");
amount.textContent = `$${lineTotal}`;
// A remove button on every line. It changes the data, then redraws.
const remove = document.createElement("button");
remove.className = "remove";
remove.textContent = "Remove";
remove.setAttribute("aria-label", `Remove ${product.name}`);
remove.addEventListener("click", () => removeFromCart(product.id));
li.append(amount, remove);
list.append(li);
}
document.querySelector("#cart-total").textContent =
cart.length === 0 ? "Your cart is empty." : `Total: $${total}`;
}
document.querySelector("#search").addEventListener("input", (event) => {
searchText = event.target.value.trim();
renderProducts();
});
renderProducts();
renderCart();