-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
194 lines (169 loc) · 5.37 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const pokedex = document.getElementById('pokemon-display');
let currentPokemon = 1;
const fetchPokemon = async (pokemonId) => {
const url = `https://pokeapi.co/api/v2/pokemon/${pokemonId}/?limit=151`;
try {
const response = await fetch(url);
const result = await response.json();
const pokemon = {
name: result.name.charAt(0).toUpperCase() + result.name.slice(1),
image: result.sprites['front_default'],
type: result.types.map((type) => type.type.name).join(', '),
id: result.id
}
displayPokemon(pokemon);
} catch (error) {
console.log(error);
}
};
const displayPokemon = (pokemon) => {
console.log(pokemon);
const firstType = pokemon.type.split(',')[0].toLowerCase();
let subtitleClass;
switch (firstType) {
case 'normal':
subtitleClass = 'type-normal';
break;
case 'fire':
subtitleClass = 'type-fire';
break;
case 'water':
subtitleClass = 'type-water';
break;
case 'electric':
subtitleClass = 'type-electric';
break;
case 'grass':
subtitleClass = 'type-grass';
break;
case 'ice':
subtitleClass = 'type-ice';
break;
case 'fighting':
subtitleClass = 'type-fighting';
break;
case 'poison':
subtitleClass = 'type-poison';
break;
case 'ground':
subtitleClass = 'type-ground';
break;
case 'flying':
subtitleClass = 'type-flying';
break;
case 'psychic':
subtitleClass = 'type-psychic';
break;
case 'bug':
subtitleClass = 'type-bug';
break;
case 'rock':
subtitleClass = 'type-rock';
break;
case 'ghost':
subtitleClass = 'type-ghost';
break;
case 'dragon':
subtitleClass = 'type-dragon';
break;
case 'dark':
subtitleClass = 'type-dark';
break;
case 'steel':
subtitleClass = 'type-steel';
break;
case 'fairy':
subtitleClass = 'type-fairy';
break;
default:
subtitleClass = '';
break;
}
const pokemonHTMLString = `
<img class="card-image" src="${pokemon.image}"/>
<h2 class="card-title">#${pokemon.id} ${pokemon.name}</h2>
<p class="card-subtitle ${subtitleClass}">${pokemon.type}</p>
`;
pokedex.innerHTML = pokemonHTMLString;
};
const searchPokemon = () => {
const searchInput = document.getElementById('search-input').value.toLowerCase().trim();
let pokemonId;
if (!isNaN(searchInput)) {
pokemonId = parseInt(searchInput);
} else {
// Search for Pokemon by name
const url = `https://pokeapi.co/api/v2/pokemon/${searchInput}/?limit=151`;
fetch(url)
.then((res) => res.json())
.then((result) => {
pokemonId = result.id;
currentPokemon = pokemonId;
fetchPokemon(currentPokemon);
})
.catch((err) => console.error(err));
return;
}
if (pokemonId >= 1 && pokemonId <= 151) {
currentPokemon = pokemonId;
fetchPokemon(currentPokemon);
} else {
alert('Please enter a valid Pokemon ID (1-151)');
}
};
document.getElementById('search-btn').addEventListener('click', searchPokemon);
fetchPokemon(currentPokemon);
//search bar display
const endpoint = 'https://pokeapi.co/api/v2/pokemon/?limit=151';
const pokemons = [];
fetch(endpoint)
.then(blob => blob.json())
.then(data => pokemons.push(...data.results));
console.log(pokemons)
function findMatches(wordToMatch, pokemons) {
return pokemons.filter(pokemon => {
const regex = new RegExp(wordToMatch, 'gi');
return pokemon.name.match(regex)
});
}
function displayMatches() {
if (this.value === '') {
suggestions.innerHTML = '';
return;
}
const matchArray = findMatches(this.value, pokemons);
const html = matchArray.map(pokemon => {
const regex = new RegExp(this.value, 'gi');
const pokemonName = pokemon.name.replace(regex, `<span class="hl">${this.value}</span>`);
return `
<li>
<span class="name">${pokemonName}</span>
</li>
`;
}).join('');
suggestions.innerHTML = html;
const suggestionItems = suggestions.querySelectorAll('li')
suggestionItems.forEach(item =>{
item.addEventListener('click',()=>{
searchInput.value = item.querySelector('.name').textContent
suggestions.innerHTML = '';
})
})
}
const searchInput = document.querySelector('#search-input');
const suggestions = document.querySelector('.suggestions');
searchInput.addEventListener('change', displayMatches);
searchInput.addEventListener('keyup', displayMatches);
// Add event listeners to display the previous and next Pokemon when the corresponding buttons are clicked.
document.getElementById('prev-btn').addEventListener('click', () => {
if (currentPokemon > 1) {
currentPokemon--;
}
fetchPokemon(currentPokemon);
});
document.getElementById('next-btn').addEventListener('click', () => {
if (currentPokemon < 151) {
currentPokemon++;
}
fetchPokemon(currentPokemon);
});