-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
164 lines (141 loc) · 4.94 KB
/
script.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
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
const cardContainer = document.getElementById("cardcontainer");
const apiUrl = "https://pokeapi.co/api/v2/pokemon?limit=151";
let lastPokemon = 0
let search = false
function makeCard(id, cardImg, pokemon, types)
{
document.body.style.overflow = "scroll"
const cardEl = document.createElement('div');
cardEl.classList.add('card');
id = (id + 1).toString();
let finalId = "";
for(let i = 0; i < (4 - id.length); i++)
{
finalId += "0";
}
finalId += id
cardEl.innerHTML = `
<div class="image-container">
<img class="card-image" src=${cardImg}>
</div>
<p class="number"><img class="pokeball" src="favicon.svg" style="height: 0.8rem"> #${finalId}</p>
<div class="card-info">
<h3 class="poke-name">${pokemon}</p>
<div class="poke-types">
${formatTypes(types)}
</div>
</div>
`;
cardContainer.appendChild(cardEl);;
}
function clearCards()
{
lastPokemon = 0
cardContainer.innerHTML = ""
document.body.style.overflow = "hidden"
}
async function formatData() {
try {
pokemen = await fetchData();
let pokemonList = [];
for (let i = 0; i < pokemen.length; i++) {
pokedata = await fetchPokemon(pokemen[i].url);
let pokename = capitalize(pokemen[i].name);
let sprite = pokedata.sprites.other.dream_world.front_default;
let types = pokedata.types.map(t => t.type.name);
pokemonList.push({ id: i, name: pokename, sprite, types });
}
clearCards();
const sortOrder = document.getElementById('sort-order').value;
const searchCriteria = document.getElementById('search-criteria').value;
pokemonList.sort((a, b) => {
if (searchCriteria === 'id') {
return sortOrder === 'asc' ? a.id - b.id : b.id - a.id;
} else if (searchCriteria === 'name') {
return sortOrder === 'asc' ? a.name.localeCompare(b.name) : b.name.localeCompare(a.name);
} else if (searchCriteria === 'type') {
return sortOrder === 'asc' ? a.types[0].localeCompare(b.types[0]) : b.types[0].localeCompare(a.types[0]);
}
});
for (let pokemon of pokemonList) {
if (testPokemon(pokemon.id, pokemon.name, pokemon.types)) {
makeCard(pokemon.id, pokemon.sprite, pokemon.name, pokemon.types);
}
}
document.getElementById("loadspinner").classList.add("invisible")
} catch(error) {
console.error("Error in data formatting: ", error);
}
}
function testPokemon(id, name, types) {
const searchTerm = document.getElementById('search-input').value.toLowerCase();
const searchCriteria = document.getElementById('search-criteria').value;
if (searchTerm === '') return true;
if ((id + 1).toString().includes(searchTerm)) return true;
if (name.toLowerCase().includes(searchTerm)) return true;
if (types.some(type => type.toLowerCase().includes(searchTerm))) return true;
return false;
}
function capitalize(str)
{
str = str[0].toUpperCase().concat(str.slice(1,(str.length)));
return str
}
async function fetchData()
{
try
{
const response = await fetch(apiUrl);
if (!response.ok)
{
throw new Error("Network response was not ok")
}
const data = await response.json();
return data.results;
} catch(error)
{
console.error('There was a problem with the fetch operation:', error);
return [];
}
}
async function fetchPokemon(url)
{
document.getElementById("loadspinner").classList.remove("invisible")
try
{
const response = await fetch(url);
if (!response.ok)
{
throw new Error("Network response was not ok")
}
const pokedata = await response.json();
return pokedata;
} catch(error)
{
console.error('There was a problem with the fetch operation:', error);
return [];
}
}
function formatTypes(types)
{
divs = ""
for(let i = 0; i < types.length; i++)
{
let type = types[i];
divs += `<div class="typediv ${type}">${capitalize(type)}</div>`
}
return divs
}
formatData()
document.getElementById('search-input').addEventListener('input', debounce(formatData, 100));
document.getElementById('search-criteria').addEventListener('change', formatData);
document.getElementById('sort-order').addEventListener('change', formatData);
function debounce(func, delay) {
let debounceTimer;
return function() {
const context = this;
const args = arguments;
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => func.apply(context, args), delay);
}
}