-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcustom.js
107 lines (92 loc) · 3.09 KB
/
custom.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
//variables
let allContainerCart = document.querySelector('.products');
let containerBuyCart = document.querySelector('.card-items');
let priceTotal = document.querySelector('.price-total')
let amountProduct = document.querySelector('.count-product');
let buyThings = [];
let totalCard = 0;
let countProduct = 0;
//functions
loadEventListenrs();
function loadEventListenrs(){
allContainerCart.addEventListener('click', addProduct);
containerBuyCart.addEventListener('click', deleteProduct);
}
function addProduct(e){
e.preventDefault();
if (e.target.classList.contains('btn-add-cart')) {
const selectProduct = e.target.parentElement;
readTheContent(selectProduct);
}
}
function deleteProduct(e) {
if (e.target.classList.contains('delete-product')) {
const deleteId = e.target.getAttribute('data-id');
buyThings.forEach(value => {
if (value.id == deleteId) {
let priceReduce = parseFloat(value.price) * parseFloat(value.amount);
totalCard = totalCard - priceReduce;
totalCard = totalCard.toFixed(2);
}
});
buyThings = buyThings.filter(product => product.id !== deleteId);
countProduct--;
}
//FIX: El contador se quedaba con "1" aunque ubiera 0 productos
if (buyThings.length === 0) {
priceTotal.innerHTML = 0;
amountProduct.innerHTML = 0;
}
loadHtml();
}
function readTheContent(product){
const infoProduct = {
image: product.querySelector('div img').src,
title: product.querySelector('.title').textContent,
price: product.querySelector('div p span').textContent,
id: product.querySelector('a').getAttribute('data-id'),
amount: 1
}
totalCard = parseFloat(totalCard) + parseFloat(infoProduct.price);
totalCard = totalCard.toFixed(2);
const exist = buyThings.some(product => product.id === infoProduct.id);
if (exist) {
const pro = buyThings.map(product => {
if (product.id === infoProduct.id) {
product.amount++;
return product;
} else {
return product
}
});
buyThings = [...pro];
} else {
buyThings = [...buyThings, infoProduct]
countProduct++;
}
loadHtml();
//console.log(infoProduct);
}
function loadHtml(){
clearHtml();
buyThings.forEach(product => {
const {image, title, price, amount, id} = product;
const row = document.createElement('div');
row.classList.add('item');
row.innerHTML = `
<img src="${image}" alt="">
<div class="item-content">
<h5>${title}</h5>
<h5 class="cart-price">${price}$</h5>
<h6>Amount: ${amount}</h6>
</div>
<span class="delete-product" data-id="${id}">X</span>
`;
containerBuyCart.appendChild(row);
priceTotal.innerHTML = totalCard;
amountProduct.innerHTML = countProduct;
});
}
function clearHtml(){
containerBuyCart.innerHTML = '';
}