-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokedex.js
102 lines (82 loc) · 3.03 KB
/
pokedex.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
const mainScreen = document.querySelector('.main-screen');
const pokeName = document.querySelector('.poke-name');
const pokeId = document.querySelector('.poke-id');
const pokeFrontImage = document.querySelector('.poke-front-image');
const pokeBackImage = document.querySelector('.poke-back-image');
const pokeTypeOne = document.querySelector('.poke-type-one');
const pokeTypeTwo = document.querySelector('.poke-type-two');
const pokeWeight = document.querySelector('.poke-weight');
const pokeHeight = document.querySelector('.poke-height');
const pokeListItems = document.querySelectorAll('.list-item');
const TYPES = [
'normal', 'fighting', 'flying',
'poison', 'ground', 'rock',
'bug', 'ghost', 'steel',
'fire', 'water', 'grass',
'electric', 'psychic', 'ice',
'dragon', 'dark', 'fairy'
]
const capitalize = (str) => str[0].toUpperCase() + str.substr(1);
const resetScreen = () => {
mainScreen.classList.remove('hide');
for (const type of TYPES) {
mainScreen.classList.remove(type);
}
}
const fetchPokeList = url => {
fetch(url)
.then(res => res.json())
.then(data => {
const { results, previous, next } = data;
prevUrl = previous;
nextUrl = next;
for (let i = 0; i < pokeListItems.length ; i++) {
const pokeListItem = pokeListItems[i];
const resultData = results[i];
if (resultData) {
const { name, url } = resultData;
const urlArray = url.split('/');
const id = urlArray[urlArray.length - 2];
pokeListItem.textContent = id + '. ' + capitalize(name);
} else {
pokeListItem.textContent = '';
}
}
});
}
const fetchPokeData = pokeInput => {
fetch(`https://pokeapi.co/api/v2/pokemon/${pokeInput}`)
.then(res => res.json())
.then(data => {
resetScreen();
const dataTypes = data['types'];
const dataFirstType = dataTypes[0];
const dataSecondType = dataTypes[1];
pokeTypeOne.textContent = capitalize(dataFirstType['type']['name']);
if (dataSecondType) {
pokeTypeTwo.classList.remove('hide');
pokeTypeTwo.textContent = capitalize(dataSecondType['type']['name']);
} else {
pokeTypeTwo.classList.add('hide');
pokeTypeTwo.textContent = '';
}
mainScreen.classList.add(dataFirstType['type']['name']);
pokeName.textContent = capitalize(data['name']);
pokeId.textContent = '#' + data['id'].toString().padStart(3, '0');
pokeWeight.textContent = data['weight'];
pokeHeight.textContent = data['height'];
pokeFrontImage.src = data['sprites']['front_default'] || '';
pokeBackImage.src = data['sprites']['back_default'] || '';
});
}
const handleListItemClick = (e) => {
if (!e.target) return;
const listItem = e.target;
if (!listItem.textContent) return;
const id = listItem.textContent.split('.')[0];
fetchPokeData(id);
}
for (const pokeListItem of pokeListItems) {
pokeListItem.addEventListener('click', handleListItemClick);
}
fetchPokeList('https://pokeapi.co/api/v2/pokemon?offset=0&limit=20');