-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
61 lines (49 loc) · 1.75 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
const pokemonName = document.querySelector('.pokemon__name');
const pokemonNumber = document.querySelector('.pokemon__number');
const pokemonImage = document.querySelector('.pokemon__image');
const form = document.querySelector('.form');
const input = document.querySelector('.input__search');
const buttonPrev = document.querySelector('.btn-prev');
const buttonNext = document.querySelector('.btn-next');
let searchPokemon = 1;
const fetchPokemon = async (pokemon) => {
const APIResponse = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon}`);
if (APIResponse.status === 200) {
const data = await APIResponse.json();
return data;
}
}
const renderPokemon = async (pokemon) => {
pokemonName.innerHTML = 'Carregando...';
pokemonNumber.innerHTML = '';
const data = await fetchPokemon(pokemon);
if (data) {
pokemonImage.style.display = 'block'
pokemonName.innerHTML = data.name;
pokemonNumber.innerHTML = data.id;
pokemonImage.src = data['sprites']['versions']['generation-v']['black-white']['animated']['front_default'];
input.value = '';
searchPokemon = data.id;
} else {
pokemonImage.style.display = 'none';
pokemonName.innerHTML = 'Não encontrado 😢'
pokemonNumber.innerHTML = '';
}
}
form.addEventListener('submit',(event) => {
event.preventDefault();
renderPokemon(input.value.toLowerCase());
});
buttonPrev.addEventListener('click', () => {
if (searchPokemon > 1) {
searchPokemon -= 1;
renderPokemon(searchPokemon);
}
});
buttonNext.addEventListener('click', () => {
if (searchPokemon < 649) {
searchPokemon += 1;
renderPokemon(searchPokemon);
}
});
renderPokemon(searchPokemon);