-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
536 lines (483 loc) · 15.2 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import { appState, productsData } from './src/js/data.js'
// Elementos relacionados con el menú hamburguesa
const menuBtn = document.querySelector('#menu-btn')
const menuNav = document.querySelector('#nav-menu')
// Elementos relacionados con el carrito
const shoppingCart = document.querySelector('.shopping-cart')
const cartMenu = document.querySelector('.cart-menu')
const productsCart = document.querySelector('.cart-container')
const cartTotalAmount = document.querySelector('.cart-total-amount')
const cartCount = document.querySelector('.cart-count')
const buyCartBtn = document.querySelector('#buy-cart-btn')
const emptyCartBtn = document.querySelector('#empty-cart-btn')
const successMessage = document.querySelector('#show-msg')
// Elementos relacionados con los productos
const productsContainer = document.querySelector('.cards-container')
const showMoreCardsBtn = document.querySelector('.btn-load')
const categoriesContainer = document.querySelector('.categories-container')
const categoriesList = document.querySelectorAll('.category')
// Elementos relacionados con el formulario de contacto
const contactForm = document.querySelector('#contact-form')
const inputName = document.querySelector('#nombre')
const inputLastName = document.querySelector('#apellido')
const inputEmail = document.querySelector('#email')
const inputMensaje = document.querySelector('#mensaje')
const btnSubmit = document.querySelector('.button-group button[type="submit"]')
const btnReset = document.querySelector('.button-group button[type="reset"]')
const spinner = document.querySelector('#spinner')
// Variables
const emailObj = {
nombre: '',
apellido: '',
email: '',
mensaje: ''
}
// -> Funciones:
//
// -> LocalStorage
// Inicializar el carrito desde localStorage o como un array vacío
let cart = JSON.parse(localStorage.getItem('cart')) || []
// Funcion para guardar carrito en el localstorage
const saveCartToLocalStorage = () => {
localStorage.setItem('cart', JSON.stringify(cart))
}
// localStorage.clear()
//
// -> Menu hamburguesa
//
// Función para manejar el menú hamburguesa
const handleMenuToggle = () => {
menuNav.classList.toggle('menu-open')
menuBtn.classList.toggle('menu-btn-active')
if (cartMenu.style.display === 'flex') {
cartMenu.style.display = 'none'
}
}
// Función para manejar el cierre del menú al hacer clic en un enlace
const handleMenuItemClick = () => {
menuNav.classList.remove('menu-open')
menuBtn.classList.toggle('menu-btn-active')
}
//
// -> Productos
//
// Funcion crear template producto
const generateProductCardHTML = (product) => {
const { id, title, price, offer, img } = product
const finalPrice = offer
? `Comprar ahora $${offer}`
: `Comprar ahora $${price}`
return `<div class="card">
<img src="${img}" alt="${title}" />
<h3>${title}</h3>
<p>$${price}</p>
<button class="card-btn" data-id="${id}" data-title="${title}" data-price="${price}"
data-img="${img}" ${offer ? `data-offer="${offer}"` : ''}>
${finalPrice}
</button>
</div>`
}
// Funcion renderizar productos en carrito
const renderProductsToDOM = (productsList) => {
productsContainer.innerHTML += productsList
.map(generateProductCardHTML)
.join('')
}
// Funcion para saber el ultimo indice de los productos
const getLastProductIndexInState = () => {
let { products } = appState
return products.length - 1
}
// Funcion para manejar el paginado
const handleShowMoreProducts = () => {
appState.currentProductsIndex += 1
let { products, currentProductsIndex } = appState
renderProductsToDOM(products[currentProductsIndex])
// Verificar si ya no hay mas productos
if (currentProductsIndex === getLastProductIndexInState()) {
showMoreCardsBtn.style.display = 'none'
}
}
// Funcion para no mostrar el boton de ver mas cuando hay filtro activado
const setShowMoreVisibility = () => {
if (appState.activeFilter) {
showMoreCardsBtn.style.display = 'none'
} else {
// Si no hay un filtro activo y hay más productos por mostrar, mostrar el botón
const isLastIndex =
appState.currentProductsIndex === getLastProductIndexInState()
showMoreCardsBtn.style.display = isLastIndex ? 'none' : 'block'
}
}
// Funcion para cambiar el estado de los botones
const changeBtnAvtiveState = (selectedCategory) => {
const categories = [...categoriesList]
categories.forEach((categoryBtn) => {
if (categoryBtn.dataset.category !== selectedCategory) {
categoryBtn.classList.remove('active')
return
}
categoryBtn.classList.add('active')
})
}
// Funcion para cambiar el estado del filtro
const changeFilterState = (btn) => {
appState.activeFilter = btn.dataset.category
changeBtnAvtiveState(appState.activeFilter)
setShowMoreVisibility(appState.activeFilter)
}
// Funcion para saber si el boton de filtro es inactivo o no
const isInactiveFilterBtn = (element) => {
return (
element.classList.contains('category') &&
!element.classList.contains('active')
)
}
// Funcion que aplica un filtro
const applyFilter = ({ target }) => {
if (!isInactiveFilterBtn(target)) {
return
}
changeFilterState(target)
productsContainer.innerHTML = ''
if (appState.activeFilter) {
renderFilteredProducts()
appState.currentProductsIndex = 0
} else {
renderProductsToDOM(appState.products[0])
}
setShowMoreVisibility() // Llamar después de renderizar los productos
}
// Funcion para renderizar los productos filtrados
const renderFilteredProducts = () => {
const filteredProducts = productsData.filter((product) => {
return product.category.includes(appState.activeFilter)
})
renderProductsToDOM(filteredProducts)
}
//
// -> Formulario de contacto
//
// Funcion para validar el email
const validar = (e) => {
const campo = e.target.id.toUpperCase()
if (e.target.value.trim() === '') {
if (e.target.id !== 'apellido') {
mostrarAlerta(`El campo ${campo} es obligatorio`, e.target.parentElement)
emailObj[e.target.name] = ''
comprobarEmail()
}
return
}
if (e.target.id === 'email' && !validarEmail(e.target.value)) {
mostrarAlerta('El email no es valido', e.target.parentElement)
emailObj[e.target.name] = ''
comprobarEmail()
return
}
limpiarAlerta(e.target.parentElement)
emailObj[e.target.name] = e.target.value.trim().toLowerCase()
comprobarEmail()
}
// Funcion para mostrar alerta
const mostrarAlerta = (mensaje, referencia) => {
limpiarAlerta(referencia)
const alerta_vacio_parrafo = document.createElement('P')
alerta_vacio_parrafo.textContent = mensaje
alerta_vacio_parrafo.classList.add('empty-field-alert')
referencia.appendChild(alerta_vacio_parrafo)
}
// Funcion para limpiar alerta
const limpiarAlerta = (referencia) => {
const alerta = referencia.querySelector('.empty-field-alert')
if (alerta) {
alerta.remove()
}
}
// Funcion para validar email
const validarEmail = (email) => {
const regex = /^\w+([.-_+]?\w+)*@\w+([.-]?\w+)*(\.\w{2,10})+$/
return regex.test(email)
}
// Funcion para comprobar email
const comprobarEmail = () => {
if (
emailObj.nombre === '' ||
emailObj.email === '' ||
emailObj.mensaje === ''
) {
btnSubmit.style.opacity = '0.5'
btnSubmit.style.cursor = 'not-allowed'
btnSubmit.disabled = true
return
}
btnSubmit.style.opacity = '1'
btnSubmit.style.cursor = 'pointer'
btnSubmit.disabled = false
}
// Funcion para resetear el formulario
const resetForm = () => {
emailObj.nombre = ''
emailObj.apellido = ''
emailObj.email = ''
emailObj.mensaje = ''
contactForm.reset()
comprobarEmail()
}
// Funcion para enviar email
const enviarEmail = (e) => {
e.preventDefault()
spinner.style.display = 'flex'
setTimeout(() => {
spinner.style.display = 'none'
resetForm()
const alertaExito = document.createElement('P')
alertaExito.classList.add('alerta-exito-enviar')
alertaExito.textContent = 'Email enviado correctamente'
contactForm.appendChild(alertaExito)
setTimeout(() => {
alertaExito.remove()
}, 3000)
}, 3000)
}
//
// -> Shopping cart
//
// Funcion para manejar el menú Carrito
const toggleShoppingCart = () => {
if (cartMenu.style.display === 'none' || cartMenu.style.display === '') {
cartMenu.style.display = 'flex'
} else {
cartMenu.style.display = 'none'
}
if (menuNav.classList.contains('menu-open')) {
menuNav.classList.remove('menu-open')
}
}
// Funcion para cerrar los Menus
const closeAnyMenu = (e) => {
// Verifica si el clic no es dentro del header
if (!header.contains(e.target)) {
// Oculta el cartMenu si está visible
if (cartMenu.style.display === 'flex') {
cartMenu.style.display = 'none'
}
// Cierra el menú de navegación si está abierto
if (menuNav.classList.contains('menu-open')) {
menuNav.classList.remove('menu-open')
}
}
}
// Funcion que devuelve un template de los items del carrito
const createCartProductsTemplate = (cartProduct) => {
const { id, title, price, offer, quantity, img } = cartProduct
const finalPrice = offer !== null && offer !== undefined ? offer : price
return `
<div class="cart-item" data-id="${id}">
<img class="cart-item-img" src="${img}" alt="${title}">
<div class="cart-item-info">
<h3 class="cart-item-title">${title}</h3>
<p class="cart-item-price">Precio: $${finalPrice}</p>
<p class="cart-item-quantity">Cantidad: ${quantity}</p>
<button class="remove-btn" data-id="${id}">Eliminar</button>
<div class="cart-handler">
<span class="quantity-handler-down less-btn" data-id="${id}">-</span>
<span class="item-quantity">${quantity}</span>
<span class="quantity-handler-up plus-btn" data-id="${id}">+</span>
</div>
</div>
</div>`
}
// Funcion que renderiza los item del carrito
const renderCart = () => {
if (!cart.length) {
const emptyMessage = document.createElement('P')
emptyMessage.classList.add('empty-cart-message')
emptyMessage.textContent = 'Tu carrito está vacío'
productsCart.appendChild(emptyMessage)
return
}
productsCart.innerHTML = cart.map(createCartProductsTemplate).join('')
}
// Funcion que obtiene el total de la compra
const getCartTotalAmount = () => {
return cart.reduce(
(acc, curr) => acc + (curr.offer ? curr.offer : curr.price) * curr.quantity,
0
)
}
// Funcion que renderiza la cantidad total del carrito
const renderCartTotalAmount = () => {
cartTotalAmount.innerHTML = `$${getCartTotalAmount().toFixed(2)}`
}
// Funcion que renderiza el contador del carrito
const renderCartCount = () => {
cartCount.textContent = cart.reduce(
(acc, curr) => acc + Number(curr.quantity),
0
)
}
// Funcion que chequea el estado de los botones
const disableBtn = (btn) => {
if (!cart.length) {
btn.classList.add('disabled')
} else {
btn.classList.remove('disabled')
}
}
// Funcion que actualiza el estado del carrito
const updateCartState = () => {
saveCartToLocalStorage()
renderCart()
renderCartTotalAmount()
disableBtn(buyCartBtn)
disableBtn(emptyCartBtn)
renderCartCount()
}
// Funcion para agregar al carrito
const addProductToCart = (e) => {
if (!e.target.classList.contains('card-btn')) {
return
}
const product = createProductData(e.target.dataset)
if (isExistingCartProduct(product)) {
addUnitToProduct(product) // Agrega una unidad
showModalSuccess(`Se agregó una unidad del producto al carrito`)
} else {
createCartProduct(product) // Crea el producto
showModalSuccess(`El producto se agregó al carrito`)
}
updateCartState() // Actualiza el estado del carrito
}
// Funcion para mostrar el modal
const showModalSuccess = (message) => {
successMessage.classList.add('active-modal')
successMessage.textContent = message
setTimeout(() => {
successMessage.classList.remove('active-modal')
}, 2000)
}
// Funcion para crear el producto
const createCartProduct = (product) => {
cart = [...cart, { ...product, quantity: 1 }]
}
// Funcion para agregar una unidad al producto
const addUnitToProduct = (product) => {
cart = cart.map((cartProduct) => {
if (cartProduct.id === product.id) {
return { ...cartProduct, quantity: cartProduct.quantity + 1 }
} else {
return cartProduct
}
})
}
// Funcion para validar si el producto ya existe en el carrito
const isExistingCartProduct = (product) => {
return cart.find((item) => item.id === product.id)
}
//
const createProductData = (product) => {
const { id, title, price, offer, img } = product
return {
id,
title,
price: Number(price),
offer: offer ? Number(offer) : null,
img
}
}
// Funcion para cambiar la cantidad del item
const handleQuantity = (e) => {
if (e.target.classList.contains('less-btn')) {
handleLessBtnEvent(e.target.dataset.id)
} else if (e.target.classList.contains('plus-btn')) {
handlePLusBtnEvent(e.target.dataset.id)
}
updateCartState()
}
// Funcion para manejar el boton del menos
const handleLessBtnEvent = (id) => {
const existingCartProduct = cart.find((item) => item.id === id)
if (existingCartProduct.quantity === 1) {
if (window.confirm('¿Deseas eliminar este item del carrito?')) {
removeProductFromCart(existingCartProduct)
if (!cart.length) {
setTimeout(() => {
location.reload()
}, 1000)
}
}
return
}
substractProductUnit(existingCartProduct)
}
const substractProductUnit = (existingProduct) => {
cart = cart.map((product) => {
return product.id === existingProduct.id
? { ...product, quantity: product.quantity - 1 }
: product
})
}
const removeProductFromCart = (existingProduct) => {
cart = cart.filter((product) => product.id !== existingProduct.id)
updateCartState()
}
const handlePLusBtnEvent = (id) => {
const existingCartProduct = cart.find((item) => item.id === id)
addUnitToProduct(existingCartProduct)
}
const resetCartItems = () => {
cart = []
updateCartState()
setTimeout(() => {
location.reload()
}, 1000)
}
// Funcion para completa una accion en el carrito
const completeCartAction = (confirmMsg, successMsg) => {
if (!cart.length) return
if (window.confirm(confirmMsg)) {
resetCartItems()
alert(successMsg)
}
}
const completeBuy = () => {
completeCartAction('¿Deseas efectuar su compra?', 'Gracias por tu compra!')
}
const deleteCart = () => {
completeCartAction('¿Deseas vaciar el carrito?', 'Carrito vaciado')
}
// Funcion Inicial
const initializeApp = () => {
// document.addEventListener('click', closeAnyMenu)
// Menú hamburguesa
menuBtn.addEventListener('click', handleMenuToggle)
menuNav.querySelectorAll('a').forEach((link) => {
link.addEventListener('click', handleMenuItemClick)
})
// Productos
renderProductsToDOM(appState.products[0])
showMoreCardsBtn.addEventListener('click', handleShowMoreProducts)
categoriesContainer.addEventListener('click', applyFilter)
// Validacion del formulario
if (contactForm) {
inputName.addEventListener('blur', validar)
inputLastName.addEventListener('blur', validar)
inputEmail.addEventListener('blur', validar)
inputMensaje.addEventListener('input', validar)
contactForm.addEventListener('submit', enviarEmail)
btnReset.addEventListener('click', resetForm)
}
// Carrito de compras
shoppingCart.addEventListener('click', toggleShoppingCart)
document.addEventListener('DOMContentLoaded', renderCart)
document.addEventListener('DOMContentLoaded', renderCartTotalAmount)
disableBtn(buyCartBtn)
disableBtn(emptyCartBtn)
renderCartCount()
productsContainer.addEventListener('click', addProductToCart)
productsCart.addEventListener('click', handleQuantity)
buyCartBtn.addEventListener('click', completeBuy)
emptyCartBtn.addEventListener('click', deleteCart)
}
initializeApp()