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: () => ({
|
||||
pokemons: [],
|
||||
loading: false,
|
||||
error: null,
|
||||
error: null
|
||||
}),
|
||||
|
||||
|
||||
actions: {
|
||||
async fetchPokemons(limit = 10) {
|
||||
// Busca a lista inicial de Pokémons
|
||||
async fetchPokemons() {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
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;
|
||||
} catch (err) {
|
||||
this.error = err.message;
|
||||
} catch (error) {
|
||||
console.error('Erro ao buscar Pokémons:', error);
|
||||
this.error = error.message || 'Falha ao carregar Pokémons';
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
// Busca detalhes de um Pokémon específico por nome
|
||||
async fetchPokemonDetails(name) {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
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;
|
||||
} catch (err) {
|
||||
this.error = err.message;
|
||||
} catch (error) {
|
||||
console.error('Erro ao buscar detalhes do Pokémon:', error);
|
||||
this.error = `Não foi possível encontrar o Pokémon: ${name}`;
|
||||
return null;
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
1119
src/views/Home.vue
1119
src/views/Home.vue
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user