forked from platzi/curso-frontend-developer-practico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
122 lines (102 loc) · 4.35 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
//Variables del navbar
const deskMenuEmail = document.querySelector('.desk-menu')
const menuEmail = document.querySelector('.mail')
const menuHamIcon = document.querySelector('.menu')
const mobileMenu = document.querySelector('.mobile-menu')
const menuCarIcon = document.querySelector('.navbar-sopping-card')
//Variables productos
const shoppingCartContain = document.getElementById('shopping-cart-cont')
const productDetailContainer = document.getElementById('product-detail')
const productDetailCloseIcon = document.querySelector('.close-product-inf')
const cardsContainer = document.querySelector('.cards-container')
//Variables de la lista de productos
let productList = []
// INTERACCIÓN CON NAVBAR
menuEmail.addEventListener('click', toggleDesktopMenu)
menuHamIcon.addEventListener('click', toggleMobileMenu)
menuCarIcon.addEventListener('click', toggleCarritoAside)
productDetailCloseIcon.addEventListener('click', closeProductDetailAside)
console.log(productDetailCloseIcon);
function toggleDesktopMenu() {
const isShoppingCartContainClose = shoppingCartContain.classList.contains('inactive')
//si el asisde carrito está abierto, hay que cerrarlo
if (!isShoppingCartContainClose) {
shoppingCartContain.classList.add('inactive')
}
deskMenuEmail.classList.toggle('inactive') //quita o pone la clase inactive si ya está puesta o no
}
function toggleMobileMenu() {
const isShoppingCartContainClose = shoppingCartContain.classList.contains('inactive')
//si el asisde carrito está abierto, hay que cerrarlo
if (!isShoppingCartContainClose) {
shoppingCartContain.classList.add('inactive')
}
mobileMenu.classList.toggle('inactive')
closeProductDetailAside()
}
function toggleCarritoAside() {
const isMobileMenuClose = mobileMenu.classList.contains('inactive')
//si el mobile menu está abierto, hay que cerrarlo
if (!isMobileMenuClose) {
mobileMenu.classList.add('inactive')
}
const isProductDetailClose = productDetailContainer.classList.contains('inactive')
//si el mobile menu está abierto, hay que cerrarlo
if (!isProductDetailClose) {
productDetailContainer.classList.add('inactive')
}
shoppingCartContain.classList.toggle('inactive')
}
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: '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: 'Bike',
price: 120,
image: 'https://images.pexels.com/photos/276517/pexels-photo-276517.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'
})
function renderProducts(products) {
for (product of products) {
const productCard = document.createElement('div')
productCard.classList.add('product-card')
const productImg = document.createElement('img')
productImg.classList.add('img-art')
productImg.setAttribute('src', product.image)
productImg.addEventListener('click', openProductDetailAside)
const productInfo = document.createElement('div')
productInfo.classList.add('product-inf')
const productInfoDiv = document.createElement('div')
const productPrice = document.createElement('p')
productPrice.innerText = `$ ${product.price} COP`
const productName = document.createElement('p')
productName.innerText = product.name
productInfoDiv.appendChild(productPrice)
productInfoDiv.appendChild(productName)
const productFigure = document.createElement('figure')
productFigure.classList.add('icon-status')
const imgCart = document.createElement('img')
imgCart.setAttribute('src', './icons/bt_add_to_cart.svg')
productFigure.appendChild(imgCart)
productInfo.appendChild(productInfoDiv)
productInfo.appendChild(productFigure)
productCard.appendChild(productImg)
productCard.appendChild(productInfo)
cardsContainer.appendChild(productCard)
}
}
renderProducts(productList)
//abrir y cerrar el aside de compras
function openProductDetailAside() {
shoppingCartContain.classList.add('inactive')
productDetailContainer.classList.remove('inactive')
}
function closeProductDetailAside() {
productDetailContainer.classList.add('inactive')
}