forked from platzi/curso-frontend-developer-practico
-
Notifications
You must be signed in to change notification settings - Fork 473
/
main.js
119 lines (91 loc) · 3.85 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
const menuEmail = document.querySelector('.navbar-email');
const menuHamIcon = document.querySelector('.menu');
const menuCarritoIcon = document.querySelector('.navbar-shopping-cart');
const productDetailCloseIcon = document.querySelector('.product-detail-close')
const desktopMenu = document.querySelector('.desktop-menu');
const mobileMenu = document.querySelector('.mobile-menu');
const shoppingCartContainer = document.querySelector('#shoppingCartContainer');
const productDetailContainer = document.querySelector('#productDetail');
const cardsContainer = document.querySelector('.cards-container');
menuEmail.addEventListener('click', toggleDesktopMenu);
menuHamIcon.addEventListener('click', toggleMobileMenu);
menuCarritoIcon.addEventListener('click', toggleCarritoAside);
productDetailCloseIcon.addEventListener('click', closeProductDetailAside);
function toggleDesktopMenu() {
const isAsideClosed = shoppingCartContainer.classList.contains('inactive');
if (!isAsideClosed) {
shoppingCartContainer.classList.add('inactive');
}
desktopMenu.classList.toggle('inactive');
}
function toggleMobileMenu() {
const isAsideClosed = shoppingCartContainer.classList.contains('inactive');
if (!isAsideClosed) {
shoppingCartContainer.classList.add('inactive');
}
closeProductDetailAside();
mobileMenu.classList.toggle('inactive');
}
function toggleCarritoAside() {
const isMobileMenuClosed = mobileMenu.classList.contains('inactive');
if (!isMobileMenuClosed) {
mobileMenu.classList.add('inactive');
}
const isProductDetailClosed = productDetailContainer.classList.contains('inactive');
if (!isProductDetailClosed) {
productDetailContainer.classList.add('inactive');
}
shoppingCartContainer.classList.toggle('inactive');
}
function openProductDetailAside() {
shoppingCartContainer.classList.add('inactive');
productDetailContainer.classList.remove('inactive');
}
function closeProductDetailAside() {
productDetailContainer.classList.add('inactive');
}
const productList = [];
productList.push({
name: 'Bike',
price: 120,
image: 'https://images.pexels.com/photos/276517/pexels-photo-276517.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940',
});
productList.push({
name: 'Pantalla',
price: 220,
image: 'https://images.pexels.com/photos/276517/pexels-photo-276517.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940',
});
productList.push({
name: 'Compu',
price: 620,
image: 'https://images.pexels.com/photos/276517/pexels-photo-276517.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940',
});
function renderProducts(arr) {
for (product of arr) {
const productCard = document.createElement('div');
productCard.classList.add('product-card');
// product= {name, price, image} -> product.image
const productImg = document.createElement('img');
productImg.setAttribute('src', product.image);
productImg.addEventListener('click', openProductDetailAside);
const productInfo = document.createElement('div');
productInfo.classList.add('product-info');
const productInfoDiv = document.createElement('div');
const productPrice = document.createElement('p');
productPrice.innerText = '$' + product.price;
const productName = document.createElement('p');
productName.innerText = product.name;
productInfoDiv.appendChild(productPrice);
productInfoDiv.appendChild(productName);
const productInfoFigure = document.createElement('figure');
const productImgCart = document.createElement('img');
productImgCart.setAttribute('src', './icons/bt_add_to_cart.svg');
productInfoFigure.appendChild(productImgCart);
productInfo.appendChild(productInfoDiv);
productInfo.appendChild(productInfoFigure);
productCard.appendChild(productImg);
productCard.appendChild(productInfo);
cardsContainer.appendChild(productCard);
}
}
renderProducts(productList);