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