refactor: improve error handling and update fetchPokemons limit
This commit is contained in:
parent
3284563325
commit
4ad2f3e04a
@ -5,35 +5,41 @@ export const usePokemonStore = defineStore('pokemon', {
|
|||||||
state: () => ({
|
state: () => ({
|
||||||
pokemons: [],
|
pokemons: [],
|
||||||
loading: false,
|
loading: false,
|
||||||
error: null,
|
error: null
|
||||||
}),
|
}),
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
async fetchPokemons(limit = 10) {
|
// Busca a lista inicial de Pokémons
|
||||||
|
async fetchPokemons() {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.error = null;
|
this.error = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(`https://pokeapi.co/api/v2/pokemon?limit=${limit}`);
|
const response = await axios.get('https://pokeapi.co/api/v2/pokemon?limit=20');
|
||||||
this.pokemons = response.data.results;
|
this.pokemons = response.data.results;
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
this.error = err.message;
|
console.error('Erro ao buscar Pokémons:', error);
|
||||||
|
this.error = error.message || 'Falha ao carregar Pokémons';
|
||||||
} finally {
|
} finally {
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Busca detalhes de um Pokémon específico por nome
|
||||||
async fetchPokemonDetails(name) {
|
async fetchPokemonDetails(name) {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.error = null;
|
this.error = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(`https://pokeapi.co/api/v2/pokemon/${name}`);
|
const response = await axios.get(`https://pokeapi.co/api/v2/pokemon/${name.toLowerCase()}`);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
this.error = err.message;
|
console.error('Erro ao buscar detalhes do Pokémon:', error);
|
||||||
|
this.error = `Não foi possível encontrar o Pokémon: ${name}`;
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
1105
src/views/Home.vue
1105
src/views/Home.vue
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user