-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokedex.js
115 lines (105 loc) · 3.1 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
103
104
105
106
107
108
109
110
111
112
113
114
115
document.addEventListener("alpine:init", () => {
Alpine.data("pokedex", () => ({
// Persist the search using Alpine.js Persist Plugin
search: Alpine.$persist(""),
pokemonList: [],
loading: false,
types: [
"normal",
"fire",
"water",
"grass",
"electric",
"ice",
"fighting",
"poison",
"ground",
"flying",
"psychic",
"bug",
"rock",
"ghost",
"dragon",
"steel",
"fairy",
],
activeType: Alpine.$persist(""),
// Initialize by fetching pokemon automatically on page load
init() {
this.fetchPokemon();
},
// Fetch Pokémon data from pokeapi.co
fetchPokemon() {
this.loading = true;
fetch(`https://pokeapi.co/api/v2/pokemon?limit=151`)
.then((response) => response.json())
.then((data) => {
return Promise.all(
data.results.map((pokemon) =>
fetch(pokemon.url)
.then((res) => res.json())
.then(({ id, name, sprites, types }) => ({ id, name, sprites, types }))
)
);
})
.then((pokemons) => {
this.pokemonList.push(...pokemons);
})
.catch((error) => console.error(error))
.finally(() => {
this.loading = false;
});
},
// Search Pokémon based on the search
searchPokemon() {
this.pokemonList = [];
this.fetchPokemon();
},
// Set the active type for filtering
filterByType(type) {
this.activeType = type;
},
// Clear the search and filter
clearFilter() {
this.search = "";
this.activeType = "";
},
// Computed property to return filtered Pokemon list
get filteredPokemon() {
let filtered = this.pokemonList;
if (this.activeType) {
filtered = filtered.filter((pokemon) =>
pokemon.types.some((t) => t.type.name === this.activeType)
);
}
if (this.search !== "") {
filtered = filtered.filter((pokemon) => pokemon.name.includes(this.search.toLowerCase()));
}
return filtered;
},
// Get CSS classes depends on types
getTypeClass(type) {
const typeClasses = {
normal: "bg-gray-300 text-gray-800",
fire: "bg-red-300 text-red-800",
water: "bg-blue-300 text-blue-800",
grass: "bg-green-300 text-green-800",
electric: "bg-yellow-300 text-yellow-800",
ice: "bg-blue-200 text-blue-800",
fighting: "bg-pink-300 text-pink-800",
poison: "bg-purple-300 text-purple-800",
ground: "bg-emerald-300 text-emerald-800",
flying: "bg-indigo-300 text-indigo-800",
psychic: "bg-purple-200 text-purple-800",
bug: "bg-green-200 text-green-800",
rock: "bg-amber-200 text-amber-800",
ghost: "bg-indigo-200 text-indigo-800",
dragon: "bg-red-200 text-red-800",
dark: "bg-gray-600 text-gray-200",
steel: "bg-gray-400 text-gray-800",
fairy: "bg-pink-200 text-pink-800",
};
return typeClasses[type] || "bg-gray-300 text-gray-800";
},
}));
});