-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
64 lines (52 loc) · 1.67 KB
/
scripts.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
const pokemonName = document.querySelector('.pokemon_name');
const pokemonNumber = document.querySelector('.pokemon_number');
const pokemonImage = document.querySelector('.pokemon_image');
const pokemonForm = document.querySelector('.form');
const pokemonSearch = 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 = 'Loading...';
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'
];
pokemonSearch.value = '';
searchPokemon = data.id;
} else {
pokemonImage.style.display = 'none';
pokemonName.innerHTML = 'Not found :(';
pokemonNumber.innerHTML = '';
pokemonSearch.value = '';
}
};
pokemonForm.addEventListener('submit', (event) => {
event.preventDefault();
renderPokemon(pokemonSearch.value.toLowerCase());
});
buttonNext.addEventListener('click', () => {
searchPokemon += 1;
renderPokemon(searchPokemon);
});
buttonPrev.addEventListener('click', () => {
if (searchPokemon > 1) {
searchPokemon -= 1;
renderPokemon(searchPokemon);
}
});
renderPokemon(searchPokemon);