Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 39 additions & 43 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles/reset.css" />
<link rel="stylesheet" href="styles/index.css" />
<title>Greengrocers</title>
<script defer src="index.js"></script>
</head>
<body>
<header id="store">
<h1>Greengrocers</h1>
<ul class="item-list store--item-list">
<!-- Take a look at store-item.html -->
</ul>
</header>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles/reset.css" />
<link rel="stylesheet" href="styles/index.css" />
<title>Greengrocers</title>
<script defer src="index.js"></script>
</head>
<body>
<header id="store">
<h1>Greengrocers</h1>
<ul class="item-list store--item-list" id="itemsList">
<!-- Take a look at store-item.html -->
</ul>
</header>

<main id="cart">
<h2>Your Cart</h2>
<main id="cart">
<h2>Your Cart</h2>

<div class="cart--item-list-container">
<ul class="item-list cart--item-list">
<!-- Take a look at cart-item.html -->
</ul>
</div>
<div class="cart--item-list-container">
<ul class="item-list cart--item-list">
<!-- Take a look at cart-item.html -->
</ul>
</div>

<div class="total-section">
<div>
<h3>Total</h3>
</div>
<div class="total-section">
<div>
<h3>Total</h3>
</div>

<div>
<span class="total-number">£0.00</span>
</div>
</div>
</main>
<div>
Icons made by
<a href="https://www.flaticon.com/authors/icongeek26" title="Icongeek26">
Icongeek26
</a>
from
<a href="https://www.flaticon.com/" title="Flaticon">
www.flaticon.com
</a>
</div>
</body>
<div>
<span class="total-number">£0.00</span>
</div>
</div>
</main>
<div>
Icons made by
<a href="https://www.flaticon.com/authors/icongeek26" title="Icongeek26"> Icongeek26 </a>
from
<a href="https://www.flaticon.com/" title="Flaticon"> www.flaticon.com </a>
</div>
</body>
</html>
225 changes: 171 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,172 @@
const state = {
items: [
{
id: "001-beetroot",
name: "beetroot",
price: 0.35
},
{
id: "002-carrot",
name: "carrot",
price: 0.35
},
{
id: "003-apple",
name: "apple",
price: 0.35
},
{
id: "004-apricot",
name: "apricot",
price: 0.35
},
{
id: "005-avocado",
name: "avocado",
price: 0.35
},
{
id: "006-bananas",
name: "bananas",
price: 0.35
},
{
id: "007-bell-pepper",
name: "bell pepper",
price: 0.35
},
{
id: "008-berry",
name: "berry",
price: 0.35
},
{
id: "009-blueberry",
name: "blueberry",
price: 0.35
},
{
id: "010-eggplant",
name: "eggplant",
price: 0.35
}
],
cart: []
};
items: [
{
id: "001-beetroot",
name: "beetroot",
price: 0.35,
},
{
id: "002-carrot",
name: "carrot",
price: 0.35,
},
{
id: "003-apple",
name: "apple",
price: 0.35,
},
{
id: "004-apricot",
name: "apricot",
price: 0.35,
},
{
id: "005-avocado",
name: "avocado",
price: 0.35,
},
{
id: "006-bananas",
name: "bananas",
price: 0.35,
},
{
id: "007-bell-pepper",
name: "bell pepper",
price: 0.35,
},
{
id: "008-berry",
name: "berry",
price: 0.35,
},
{
id: "009-blueberry",
name: "blueberry",
price: 0.35,
},
{
id: "010-eggplant",
name: "eggplant",
price: 0.35,
},
],
cart: [],
};

const itemsList = document.querySelector("#itemsList");

const itemsRender = () => {
itemsList.innerHTML = "";
for (let i = 0; i < state.items.length; i++) {
const item = state.items[i];
const listItem = document.createElement("li");

const iconDiv = document.createElement("div");
iconDiv.className = "store--item-icon";

const img = document.createElement("img");
img.src = `assets/icons/${item.id}.svg`;
img.alt = item.name;
iconDiv.appendChild(img);

const button = document.createElement("button");
button.classList.add("addBtn");
button.setAttribute("data-index", i);
button.textContent = "Add to cart";

button.addEventListener("click", () => {
addToCart(item);
});

listItem.appendChild(iconDiv);
listItem.appendChild(button);

itemsList.appendChild(listItem);
}
};

function addToCart(item) {
const cartItem = state.cart.find((cartItem) => cartItem.id === item.id);
if (cartItem) {
cartItem.quantity += 1;
} else {
state.cart.push({ ...item, quantity: 1 });
}
renderCart();
}

const cartList = document.querySelector(".cart--item-list");
const totalNumber = document.querySelector(".total-number");

function renderCart() {
cartList.innerHTML = "";
let total = 0;

for (const cartItem of state.cart) {
const li = document.createElement("li");

const img = document.createElement("img");
img.className = "cart--item-icon";
img.src = `assets/icons/${cartItem.id}.svg`;
img.alt = cartItem.name;

const nameP = document.createElement("p");
nameP.textContent = cartItem.name;

const minusBtn = document.createElement("button");
minusBtn.className = "quantity-btn remove-btn center";
minusBtn.textContent = "-";
minusBtn.addEventListener("click", () => {
updateCartItem(cartItem, -1);
});

const quantitySpan = document.createElement("span");
quantitySpan.className = "quantity-text center";
quantitySpan.textContent = cartItem.quantity;

const plusBtn = document.createElement("button");
plusBtn.className = "quantity-btn add-btn center";
plusBtn.textContent = "+";
plusBtn.addEventListener("click", () => {
updateCartItem(cartItem, 1);
});

li.appendChild(img);
li.appendChild(nameP);
li.appendChild(minusBtn);
li.appendChild(quantitySpan);
li.appendChild(plusBtn);

cartList.appendChild(li);

total += cartItem.price * cartItem.quantity;
}

totalNumber.textContent = `£${total.toFixed(2)}`;
}

function updateCartItem(item, change) {
const cartItem = state.cart.find((ci) => ci.id === item.id);
if (!cartItem) return;
cartItem.quantity += change;
if (cartItem.quantity <= 0) {
state.cart = state.cart.filter((ci) => ci.id !== item.id);
}
renderCart();
}

function addToCart(item) {
const cartItem = state.cart.find((cartItem) => cartItem.id === item.id);
if (cartItem) {
cartItem.quantity += 1;
} else {
state.cart.push({ ...item, quantity: 1 });
}
renderCart();
}

document.addEventListener("DOMContentLoaded", () => {
itemsRender();
renderCart();
});