-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
140 lines (123 loc) · 4.33 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
const baseURL = 'https://pokeapi.co/api/v2/pokemon/';
const container = document.querySelector('.container');
const search = document.getElementById('search');
const filter = document.getElementById('filter');
const prev = document.getElementById('prev');
const next = document.getElementById('next');
const firstPage = document.getElementById('firstPage');
const lastPage = document.getElementById('lastPage');
const currentPageSpan = document.getElementById('currentPage');
let currentPage = 1;
const itemsPerPage = 200;
const totalPages = Math.ceil(1302 / itemsPerPage);
const colors = {
fire: '#f9c0b7',
grass: '#DEFDE0',
electric: '#FCF7DE',
water: '#DEF3FD',
ground: '#f4e7da',
rock: '#d5d5d4',
fairy: '#fceaff',
poison: '#c8a3d5',
bug: '#f8d5a3',
dragon: '#97b3e6',
psychic: '#eaeda1',
flying: '#F5F5F5',
fighting: '#E6E0D4',
normal: '#F5F5F5'
};
const fetchPokemons = async (page) => {
container.innerHTML = '';
const offset = (page - 1) * itemsPerPage;
const url = `${baseURL}?offset=${offset}&limit=${itemsPerPage}`;
const res = await fetch(url);
const data = await res.json();
data.results.forEach(async (pokemon) => {
const pokeData = await fetchPokemonData(pokemon.url);
createPokemonCard(pokeData);
});
updatePaginationState();
};
const updatePaginationState = () => {
prev.disabled = currentPage === 1;
firstPage.disabled = currentPage === 1;
next.disabled = currentPage === totalPages;
lastPage.disabled = currentPage === totalPages;
currentPageSpan.textContent = `Page ${currentPage} of ${totalPages}`;
};
const fetchPokemonData = async (url) => {
const res = await fetch(url);
const data = await res.json();
return data;
};
function createPokemonCard(pokemon) {
const pokeEl = document.createElement('div');
pokeEl.classList.add('pokemon');
const name = pokemon.name[0].toUpperCase() + pokemon.name.slice(1);
const id = pokemon.id.toString().padStart(3, '0');
const poke_types = pokemon.types.map(type => type.type.name);
const type = mainType(poke_types);
const color = colors[type];
pokeEl.style.backgroundColor = color;
const pokemonInnerHTML = `
<div class="img-container">
<img src="https://assets.pokemon.com/assets/cms2/img/pokedex/full/${id}.png" alt="${name}">
</div>
<div class="info">
<span class="number">#${id}</span>
<h3 class="name">${name}</h3>
<small>Type: <span class="type">${type}</span></small>
<p>
<small class="abilities"><span>Abilities:</span> ${pokemon.abilities.map(ability => ability.ability.name).join(', ')}</small><br>
<small class="moves"><span>Moves:</span> ${pokemon.moves.slice(0, 6).map(move => move.move.name).join(', ')}</small>
</p>
</div>
`;
pokeEl.innerHTML = pokemonInnerHTML;
container.appendChild(pokeEl);
}
function mainType(types) {
const type = types.find(type => Object.keys(colors).includes(type));
return type || 'normal';
}
search.addEventListener('input', (e) => {
const term = e.target.value.toLowerCase();
const pokemons = document.querySelectorAll('.pokemon');
pokemons.forEach(pokemon => {
const name = pokemon.querySelector('.name').innerText.toLowerCase();
pokemon.style.display = name.includes(term) ? '' : 'none';
});
});
filter.addEventListener('change', (e) => {
const type = e.target.value;
const pokemons = document.querySelectorAll('.pokemon');
pokemons.forEach(pokemon => {
const pokemonType = pokemon.querySelector('.type').innerText;
pokemon.style.display = (type === 'all' || pokemonType === type) ? '' : 'none';
});
});
firstPage.addEventListener('click', () => {
if (currentPage !== 1) {
currentPage = 1;
fetchPokemons(currentPage);
}
});
lastPage.addEventListener('click', () => {
if (currentPage !== totalPages) {
currentPage = totalPages;
fetchPokemons(currentPage);
}
});
prev.addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
fetchPokemons(currentPage);
}
});
next.addEventListener('click', () => {
if (currentPage < totalPages) {
currentPage++;
fetchPokemons(currentPage);
}
});
fetchPokemons(currentPage);