-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproductoIndividual.js
57 lines (51 loc) · 1.88 KB
/
productoIndividual.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
import { clientServices } from "../service/client-service.js";
const Contenido = document.querySelector("[data-div]");
const Similares = document.querySelector("[data-similares]");
let producto = null;
const obtenerInformacion = async () => {
const url = new URL(window.location);
const id = url.searchParams.get("id");
if (id === null) {
window.location.href = "../screens/error.html";
}
try {
producto = await clientServices.detalleCliente(id);
if (producto.imagen && producto.categoria && producto.nombre && producto.precio && producto.descripcion) {
const contenido = `
<img src="${producto.imagen}" alt="${producto.nombre}">
<div class="contenidoIndividual">
<h2>${producto.nombre}</h2>
<p class="precio">${producto.precio}</p>
<p>${producto.descripcion}</p>
</div>
`;
Contenido.innerHTML = contenido;
} else {
throw new Error();
}
} catch (err) {
window.location.href = "../screens/error.html";
}
};
const recomendarProductos = (imagen, nombre, precio, id) => {
const similar = `
<div class="productos__caja">
<img src="${imagen}" alt="${nombre}" class="imagen__producto">
<p>${nombre}</p>
<p>${precio}</p>
<a href="productoIndividual.html?id=${id}">Ver producto</a>
</div>
`;
Similares.innerHTML += similar;
}
obtenerInformacion().then(() => {
clientServices.listaClientes()
.then((data) => {
data.forEach(({ imagen, categoria, nombre, precio, id }) => {
if (producto && producto.categoria === categoria) {
recomendarProductos(imagen, nombre, precio, id);
}
});
})
.catch((error) => alert(`Ocurrió un error: ${error.message}`));
});