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: () => ({ 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;
} }
}, }
}, }
}); });

File diff suppressed because it is too large Load Diff