-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
144 lines (129 loc) · 4.45 KB
/
main.js
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
142
143
144
import data from './data.js'
//define Variables
const itemsContainer = document.querySelector('#items');
const itemList = document.getElementById('item-list');
const cartQty = document.getElementById('cart-qty');
const cartTotal =document.getElementById('cart-total');
//produces images on the page
for(let i = 0; i <data.length; i += 1){
const newDiv = document.createElement('div');
newDiv.className = 'item';
const img = document.createElement('img');
img.src = data[i].image;
img.width = 300;
img.height = 300;
newDiv.appendChild(img);
const description = document.createElement ('P');
description.innerText= data[i].desc;
newDiv.append(description);
const price = document.createElement('P');
price.innerText = data[i].price;
newDiv.appendChild(price);
const button = document.createElement('button');
button.id = data[i].name;
button.dataset.price = data[i].price
button.innerHTML = "Add to Cart";
newDiv.appendChild(button) ;
itemsContainer.appendChild(newDiv);
}
//shopping cart
const cart = [];
//---------------------------------------------------------------------------
//add to cart button
const all_items_button = Array.from(document.querySelectorAll("button"))
all_items_button.forEach(elt => elt.addEventListener('click', () => {
addToCart(elt.getAttribute('id'), elt.getAttribute('data-price'))
showCartItems()
}))
//---------------------------------------------------------------------------
//handle clicks on list
itemList.onclick = function(e) {
if(e.target && e.target.classList.contains('remove')) {
const name = e.target.dataset.name;
removeItem(name);
} else if (e.target && e.target.classList.contains('add')){
console.log('add');
const name = e.target.dataset.name;
addToCart(name);
} else if (e.target && e.target.classList.contains('subtract')){
const name = e.target.dataset.name;
removeItem(name, 1);
}
}
//---------------------------------------------------------------------------
//add item
function addToCart(name, price) {
for (let i = 0; i < cart.length; i += 1){
if(cart[i].name === name){
cart[i].qty += 1
showCartItems();
return
}
}
//create an object to store name, price and qty
const item = {
name: name,
price: price,
qty: 1
}
cart.push(item); //pushes item object in the cart
console.log(cart)
}
//---------------------------------------------------------------------------
//show items
function showCartItems() {
const qty = getQty(); //calls function and returns total at the same time
cartQty.innerHTML = `You have ${qty} items in your cart`;
let itemStr = ' '
for(let i =0; i < cart.length; i += 1) {
const price = cart[i].price;
const name = cart[i].name;
const quantity = cart[i].qty;
const itemTotal = cart[i].price * cart[i].qty;
itemStr += `<li> ${name} $${price} x ${quantity} = $${itemTotal}
<button class = "remove" data-name = "${name}" > Remove </button>
<button class = "add" data-name = "${name}" > + </button>
<button class = "subtract" data-name = "${name}" > - </button </li>`;
}
itemList.innerHTML = itemStr;
let total = calculateTotal();
cartTotal.innerHTML = `Your cart total is ${total}`
}
//---------------------------------------------------------------------------
//get quantity
function getQty(){
//for loop calculates & displays qty
let qty = 0;
for(let i =0; i < cart.length; i += 1){
qty += cart[i].qty;
}
return qty;
}
//---------------------------------------------------------------------------
// calculate total
function calculateTotal(){
//for loop calculates and displays total
let total = 0;
for (let i = 0; i < cart.length; i += 1){
total += cart[i].price * cart[i].qty;
}
return total.toFixed(2);
}
//---------------------------------------------------------------------------
//remove item
function removeItem(name, qty=0){
console.log('Entered remove item')
for(let i = 0; i < cart.length; i += 1){
if(name === cart[i].name){
if (qty > 0){
console.log('it matches')
cart[i].qty -= qty;
}
if (cart[i].qty < 1 || qty === 0){
cart.splice(i,1)
}
showCartItems()
return
}
}
}