refactor: improve error handling and update fetchPokemons limit

This commit is contained in:
flavia-vic 2025-03-12 11:01:47 -04:00
parent 3284563325
commit 4ad2f3e04a
2 changed files with 796 additions and 355 deletions

View File

@ -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;
}
},
},
}
}
});

File diff suppressed because it is too large Load Diff