-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
190 lines (156 loc) · 5.09 KB
/
main.js
File metadata and controls
190 lines (156 loc) · 5.09 KB
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
// Inicio
const contenedorProductos = document.querySelector("#contenedor-productos");
const botonesCategorias = document.querySelectorAll(".boton-categoria");
const tituloPrincipal = document.querySelector("#titulo-principal");
let botonesAgregar = document.querySelectorAll(".producto-agregar");
const numerito = document.querySelector("#numerito");
let logo_img = document.getElementById("logo_img");
function cargarProductos(productosElegidos) {
return new Promise((resolve) => {
contenedorProductos.innerHTML = "";
productosElegidos.forEach((producto) => {
const div = document.createElement("div");
div.classList.add("producto");
div.innerHTML = `
<img class="producto-imagen" src="${producto.imagen}" alt="${producto.titulo}">
<div class="producto-detalles">
<h3 class="producto-titulo">${producto.titulo}</h3>
<p class="producto-precio">$${producto.precio}</p>
<button class="producto-agregar" id="${producto.id}">Enviar al Hangar</button>
</div>
`;
contenedorProductos.append(div);
});
actualizarBotonesAgregar();
});
}
cargarProductos(productos);
botonesCategorias.forEach((boton) => {
boton.addEventListener("click", (e) => {
botonesCategorias.forEach((boton) => boton.classList.remove("active"));
e.currentTarget.classList.add("active");
if (e.currentTarget.id != "todos") {
const productoCategoria = productos.find(
(producto) => producto.categoria.id === e.currentTarget.id
);
tituloPrincipal.innerText = productoCategoria.categoria.nombre;
const productosBoton = productos.filter(
(producto) => producto.categoria.id === e.currentTarget.id
);
cargarProductos(productosBoton);
} else {
tituloPrincipal.innerText = "Todas las Aeronaves";
cargarProductos(productos);
}
});
});
function actualizarBotonesAgregar() {
botonesAgregar = document.querySelectorAll(".producto-agregar");
botonesAgregar.forEach((boton) => {
boton.addEventListener("click", agregarAlCarrito);
});
}
let productosEnCarrito;
let productosEnCarritoLS = localStorage.getItem("productos-en-carrito");
if (productosEnCarritoLS) {
productosEnCarrito = JSON.parse(productosEnCarritoLS);
actualizarContadorCarrito();
} else {
productosEnCarrito = [];
}
function agregarAlCarrito(e) {
const idBoton = e.currentTarget.id;
const productoAgregado = productos.find(
(producto) => producto.id === idBoton
);
if (productosEnCarrito.some((producto) => producto.id === idBoton)) {
const index = productosEnCarrito.findIndex(
(producto) => producto.id === idBoton
);
productosEnCarrito[index].cantidad++;
} else {
productoAgregado.cantidad = 1;
productosEnCarrito.push(productoAgregado);
}
actualizarContadorCarrito();
localStorage.setItem(
"productos-en-carrito",
JSON.stringify(productosEnCarrito)
);
const Toast = Swal.mixin({
toast: true,
position: "top-end",
showConfirmButton: false,
timer: 1000,
timerProgressBar: true,
didOpen: (toast) => {
toast.addEventListener("mouseenter", Swal.stopTimer);
toast.addEventListener("mouseleave", Swal.resumeTimer);
},
});
Toast.fire({
icon: "success",
title: "Enviado al hangar",
});
}
function actualizarContadorCarrito() {
let nuevoNumerito = productosEnCarrito.reduce(
(acc, producto) => acc + producto.cantidad,
0
);
numerito.innerText = nuevoNumerito;
}
// Efecto de cambio de color en el logo
window.addEventListener("DOMContentLoaded", () => {
logo_img.onmouseenter = () => {
logo_img.src = "./media/logo_.jpg";
};
logo_img.onmouseleave = () => {
logo_img.src = "./media/logo.jpg";
};
});
// Efectos visuales en scrollbar y en cursor
const progressBarContainer = document.querySelector("#progressBarContainer");
const progressBar = document.querySelector("#progressBar");
let totalPageHeight = document.body.scrollHeight - window.innerHeight;
let debounceResize;
window.addEventListener(
"scroll",
() => {
let newProgressHeight = window.pageYOffset / totalPageHeight;
progressBar.style.transform = `scale(1,${newProgressHeight})`;
progressBar.style.opacity = `${newProgressHeight}`;
},
{
capture: true,
passive: true,
}
);
progressBarContainer.addEventListener("click", (e) => {
let newPageScroll =
(e.clientY / progressBarContainer.offsetHeight) * totalPageHeight;
window.scrollTo({
top: newPageScroll,
behavior: "smooth",
});
});
window.addEventListener("resize", () => {
clearTimeout(debounceResize);
debounceResize = setTimeout(() => {
totalPageHeight = document.body.scrollHeight - window.innerHeight;
}, 250);
});
function cambiarCursor() {
document.getElementById("body").style.cursor = "crosshair";
}
// Fin
/*
PROTIPS DE TONY EN LA ULTIMA CLASE SE DEBE ENTREGAR.
Objetos y Arrays. Métodos de Arrays.
Funciones y condicionales.
Generación del DOM de forma dinámica. Eventos.
Sintaxis avanzada.
Al menos una librería de uso relevante para el proyecto.
Manejo de promesas con fetch.
Carga de datos desde un JSON local o desde una API externa.
*/