-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
104 lines (81 loc) · 3.16 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
let API_URL = "https://pokeapi.co/api/v2/pokemon/?limit=1302";
let container = document.querySelector(".AllCards");
fetch(API_URL)
.then((res) => res.json())
.then((data) => {
let pokearray = data.results;
pokearray.forEach((pokemon) => {
let PokeUrl = pokemon.url;
fetch(PokeUrl)
.then((res) => res.json())
.then((data) => {
let Pokname = data.name;
let PokId = data.id;
let PokImg = data.sprites.front_default;
let Poktype1 = data.types[0].type.name;
let Poktype2 = data.types[1] ? data.types[1].type.name : null;
let card = document.createElement("div");
card.classList.add("card", "Pokecard");
card.style.width = "15rem";
let img = document.createElement("img");
img.classList.add("card-img-top", "PokImg");
img.src = PokImg;
img.alt = "Pokemon Image";
let cardBody = document.createElement("div");
cardBody.classList.add("card-body");
let title = document.createElement("h5");
title.classList.add("PokeName");
title.textContent = Pokname;
let Pokecode = document.createElement("h6");
Pokecode.classList.add("card-title", "PokeCode");
Pokecode.textContent = "Code: #" + PokId;
let typeList = document.createElement("ul");
typeList.classList.add("PokeTypes");
let type1 = document.createElement("li");
type1.classList.add("type1");
type1.textContent = Poktype1;
let type2 = document.createElement("li");
type2.classList.add("type2");
type2.textContent = Poktype2 ? Poktype2 : "";
typeList.appendChild(type1);
if (Poktype2) typeList.appendChild(type2);
cardBody.append(title, Pokecode, typeList);
card.append(img, cardBody);
container.appendChild(card);
type1.style.backgroundColor = getComputedStyle(
document.documentElement
).getPropertyValue(`--type-${Poktype1}`);
if (Poktype2) {
type2.style.backgroundColor = getComputedStyle(
document.documentElement
).getPropertyValue(`--type-${Poktype2}`);
}
});
});
});
document.querySelectorAll('.btn-header').forEach(button => {
button.addEventListener('click', () => {
let type = button.id;
let allCards = document.querySelectorAll('.Pokecard');
allCards.forEach(card => {
let types = card.querySelectorAll('.PokeTypes li');
let match = false;
types.forEach(typeElement => {
if (typeElement.textContent.toLowerCase() === type.toLowerCase()) {
match = true;
}
});
if (match) {
card.style.display = 'block';
} else {
card.style.display = 'none';
}
});
});
});
document.querySelector('#ver-todos').addEventListener('click', () => {
let allCards = document.querySelectorAll('.Pokecard');
allCards.forEach(card => {
card.style.display = 'block';
});
});