-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (79 loc) · 2.78 KB
/
Copy pathindex.js
File metadata and controls
89 lines (79 loc) · 2.78 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
let dataFetch;
const cards = document.querySelector("#card-dinamica");
const templateCard = document.querySelector(".card-template").content;
const formulario = document.getElementById("formulario");
const btnClasificar = document.querySelector("main .btn");
const url = "https://rickandmortyapi.com/api/character";
//Segundo metodo de seguridad para cargar js despues del dom
document.addEventListener("DOMContentLoaded", () => {
loadDataFetch(true);
});
const loadDataFetch = async () => {
try {
loadingData(true);
const respuestaAPI = await fetch(url);
dataFetch = (await respuestaAPI.json()).results;
} catch (error) {
console.log(error);
} finally {
loadingData(false);
}
};
formulario.addEventListener("submit", (e) => {
e.preventDefault();
});
const filtrar = (data) => {
const ALIVE = 1;
const DEAD = 2;
const UNKNOWN = 3;
const filtroType = parseInt(
document.querySelector("form .form-select").value
);
if (!Number.isInteger(filtroType)) console.log("El filtro no es válido");
try {
switch (filtroType) {
case ALIVE:
document.querySelector(".title").classList.remove("d-none");
document.querySelector(".title").textContent = "Personajes vivos";
return data.filter((item) => item.status === "Alive");
case DEAD:
document.querySelector(".title").classList.remove("d-none");
document.querySelector(".title").textContent = "Personajes Muertos";
return data.filter((item) => item.status === "Dead");
case UNKNOWN:
document.querySelector(".title").classList.remove("d-none");
document.querySelector(".title").textContent = "Personajes Unknown";
return data.filter((item) => item.status === "unknown");
}
} catch (error) {
console.log(error);
}
};
const eliminarCard = () => {
while (document.querySelector("#card-dinamica .card")) {
cards.removeChild(document.querySelector("#card-dinamica .card"));
}
};
const pintarCard = (data) => {
eliminarCard();
const fragment = document.createDocumentFragment();
const filtrado = filtrar(data);
filtrado.forEach((item) => {
const clone = templateCard.cloneNode(true);
clone.querySelector(".name").textContent = item.name;
clone.querySelector(".species").textContent = item.species;
clone.querySelector(".status").textContent = item.status;
clone.querySelector(".location").textContent = item.location.name;
clone.querySelector("img").src = item.image;
fragment.appendChild(clone);
});
cards.appendChild(fragment);
};
const loadingData = (estado) => {
const loading = document.querySelector(".spinner-border");
if (estado) loading.classList.remove("d-none");
else loading.classList.add("d-none");
};
btnClasificar.addEventListener("click", () => {
pintarCard(dataFetch);
});