-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
66 lines (47 loc) · 1.7 KB
/
main.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
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 = 25;
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...';
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 {
pokemonName.innerHTML = 'Not Found :c';
pokemonNumber.innerHTML = '';
pokemonImage.style.display = 'none';
}
}
renderPokemon(searchPokemon);
form.addEventListener('submit', (event) => {
event.preventDefault();
renderPokemon(input.value.toLowerCase());
})
buttonPrev.addEventListener('click', () => {
if (searchPokemon > 1) {
searchPokemon--;
renderPokemon(searchPokemon);
}
})
buttonNext.addEventListener('click', () => {
searchPokemon++;
renderPokemon(searchPokemon);
})