-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (103 loc) · 3.37 KB
/
index.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
const btnCart = document.querySelector('.container-cart-icon');
const containerCartProducts = document.querySelector(
'.container-cart-products'
);
btnCart.addEventListener('click', () => {
containerCartProducts.classList.toggle('hidden-cart');
});
/* ========================= */
const cartInfo = document.querySelector('.cart-product');
const rowProduct = document.querySelector('.row-product');
// Lista de todos los contenedores de productos
const productsList = document.querySelector('.container-items');
// Variable de arreglos de Productos
let allProducts = [];
const valorTotal = document.querySelector('.total-pagar');
const countProducts = document.querySelector('#contador-productos');
const cartEmpty = document.querySelector('.cart-empty');
const cartTotal = document.querySelector('.cart-total');
productsList.addEventListener('click', e => {
if (e.target.classList.contains('btn-add-cart')) {
const product = e.target.parentElement;
const infoProduct = {
quantity: 1,
title: product.querySelector('h2').textContent,
price: product.querySelector('p').textContent,
};
const exits = allProducts.some(
product => product.title === infoProduct.title
);
if (exits) {
const products = allProducts.map(product => {
if (product.title === infoProduct.title) {
product.quantity++;
return product;
} else {
return product;
}
});
allProducts = [...products];
} else {
allProducts = [...allProducts, infoProduct];
}
showHTML();
}
});
rowProduct.addEventListener('click', e => {
if (e.target.classList.contains('icon-close')) {
const product = e.target.parentElement;
const title = product.querySelector('p').textContent;
allProducts = allProducts.filter(
product => product.title !== title
);
console.log(allProducts);
showHTML();
}
});
// Funcion para mostrar HTML
const showHTML = () => {
if (!allProducts.length) {
cartEmpty.classList.remove('hidden');
rowProduct.classList.add('hidden');
cartTotal.classList.add('hidden');
} else {
cartEmpty.classList.add('hidden');
rowProduct.classList.remove('hidden');
cartTotal.classList.remove('hidden');
}
// Limpiar HTML
rowProduct.innerHTML = '';
let total = 0;
let totalOfProducts = 0;
allProducts.forEach(product => {
const containerProduct = document.createElement('div');
containerProduct.classList.add('cart-product');
containerProduct.innerHTML = `
<div class="info-cart-product">
<span class="cantidad-producto-carrito">${product.quantity}</span>
<p class="titulo-producto-carrito">${product.title}</p>
<span class="precio-producto-carrito">${product.price}</span>
</div>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="icon-close"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
`;
rowProduct.append(containerProduct);
total =
total + parseInt(product.quantity * product.price.slice(1));
totalOfProducts = totalOfProducts + product.quantity;
});
valorTotal.innerText = `$${total}`;
countProducts.innerText = totalOfProducts;
};