-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
145 lines (104 loc) · 3.69 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
141
142
143
144
145
/* DOM Objects */
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 leftButton = document.querySelector('.left-button');
const rightButton = document.querySelector('.right-button');
/* constant and variable */
const TYPES =[
'normal', 'fighting', 'flying',
'poison', 'ground', 'rock',
'bug', 'ghost', 'steel',
'fire', 'water', 'grass',
'electric', 'psychic', 'ice',
'dragon', 'dark', 'fairy'
];
let prevUrl = null;
let nextUrl = null;
/*function*/
const capitalize = (String) => String[0].toUpperCase() + String.substring(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; 1 < 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 = id => {
fetch(`https://pokeapi.co/api/v2/pokemon/${id}`)
.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 handleLeftButtonClick = () => {
if (prevUrl) {
fetchPokeList(prevUrl);
}
};
const handleRightButtonClick = () => {
if (nextUrl) {
fetchPokeList(nextUrl);
}
};
const handleListItemClick = (e) => {
if (!e.target) return;
const listItem = e.target;
if (!listItem.textContent) return;
const id = listItem.textContent.split('.')[0];
fetchPokeData(id);
};
/* getting the data for the right side of the screen*/
/* Adding Event Listeners */
leftButton.addEventListener('click', handleLeftButtonClick);
rightButton.addEventListener('click', handleRightButtonClick);
for (const pokeListItem of pokeListItems) {
pokeListItem.addEventListener('click', handleListItemClick);
}
/* Initializing The App */
fetchPokeList('https://pokeapi.co/api/v2/pokemon?offset=0&limit=20');