From 4ad2f3e04ac428e774c96b4e5dd945f1960b1866 Mon Sep 17 00:00:00 2001 From: flavia-vic Date: Wed, 12 Mar 2025 11:01:47 -0400 Subject: [PATCH] refactor: improve error handling and update fetchPokemons limit --- src/stores/pokemonStore.js | 32 +- src/views/Home.vue | 1119 +++++++++++++++++++++++++----------- 2 files changed, 796 insertions(+), 355 deletions(-) diff --git a/src/stores/pokemonStore.js b/src/stores/pokemonStore.js index aa8bcd9..ae163a4 100644 --- a/src/stores/pokemonStore.js +++ b/src/stores/pokemonStore.js @@ -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; } - }, - }, -}); + } + } +}); \ No newline at end of file diff --git a/src/views/Home.vue b/src/views/Home.vue index 1e4280f..5cdb6b0 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -1,10 +1,12 @@ - - - - - - \ No newline at end of file +} + \ No newline at end of file