-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokemon.js
98 lines (84 loc) · 2.87 KB
/
pokemon.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
const MAX_POKEMON = 350;
const listWrapper = document.querySelector(".list-wrapper");
const searchInput = document.querySelector("#search-input");
const numberFilter = document.querySelector("#number");
const nameFilter = document.querySelector("#name");
const notFoundMessage = document.querySelector("#not-found-message");
let allPokemons = [];
fetch(`https://pokeapi.co/api/v2/pokemon?limit=${MAX_POKEMON}`)
.then((response) => response.json())
.then((data) => {
allPokemons = data.results;
displayPokemons(allPokemons);
});
async function fetchPokemonDataBeforeRedirect(id) {
try {
const [pokemon, pokemonSpecies] = await Promise.all([
fetch(`https://pokeapi.co/api/v2/pokemon/${id}`).then((res) =>
res.json()
),
fetch(`https://pokeapi.co/api/v2/pokemon-species/${id}`).then((res) =>
res.json()
),
]);
return true;
} catch (error) {
console.error("Failed to fetch Pokemon data before redirect");
}
}
function displayPokemons(pokemon) {
listWrapper.innerHTML = "";
pokemon.forEach((pokemon) => {
const pokemonID = pokemon.url.split("/")[6];
const listItem = document.createElement("div");
listItem.className = "list-item";
listItem.innerHTML = `
<div class="number-wrap">
<p class="caption-fonts">#${pokemonID}</p>
</div>
<div class="img-wrap">
<img src="https://raw.githubusercontent.com/pokeapi/sprites/master/sprites/pokemon/other/dream-world/${pokemonID}.svg" alt="${pokemon.name}" />
</div>
<div class="name-wrap">
<p class="body3-fonts">${pokemon.name}</p>
</div>
`;
listItem.addEventListener("click", async () => {
const success = await fetchPokemonDataBeforeRedirect(pokemonID);
if (success) {
window.location.href = `./detail.html?id=${pokemonID}`;
}
});
listWrapper.appendChild(listItem);
});
}
searchInput.addEventListener("keyup", handleSearch);
function handleSearch() {
const searchTerm = searchInput.value.toLowerCase();
let filteredPokemons;
if (numberFilter.checked) {
filteredPokemons = allPokemons.filter((pokemon) => {
const pokemonID = pokemon.url.split("/")[6];
return pokemonID.startsWith(searchTerm);
});
} else if (nameFilter.checked) {
filteredPokemons = allPokemons.filter((pokemon) => {
return pokemon.name.toLowerCase().startsWith(searchTerm)
});
} else {
filteredPokemons = allPokemons;
}
displayPokemons(filteredPokemons);
if (filteredPokemons.length === 0) {
notFoundMessage.style.display = "block";
} else {
notFoundMessage.style.display = "none";
}
}
const closeButton = document.querySelector(".search-close-icon");
closeButton.addEventListener("click", clearSearch);
function clearSearch() {
searchInput.value = "";
displayPokemons(allPokemons);
notFoundMessage.style.display = "none";
}