Created
November 6, 2020 19:05
-
-
Save DeaVenditama/3940efc1458959577aadd46c9299c17a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { reactive, toRefs } from 'vue'; | |
const state = reactive({ | |
error: null, | |
pokemons: null, | |
loaded: false, | |
loading: false, | |
temp: null, | |
}); | |
export default function usePokemons(){ | |
const load = async () =>{ | |
if(!state.loaded){ | |
try{ | |
const pokemonsResponse = await fetch( | |
"http://localhost/pokemon.json" | |
); | |
state.pokemons = await pokemonsResponse.json(); | |
state.temp = state.pokemons | |
} | |
catch(e) | |
{ | |
state.error = e; | |
} | |
} | |
}; | |
const search = (searchKey) => { | |
if(searchKey) | |
{ | |
state.temp = [ | |
state.pokemons.find(function(item){ | |
return item.Name == searchKey.toLowerCase(); | |
}) | |
]; | |
}else{ | |
state.temp = state.pokemons; | |
} | |
} | |
return { ...toRefs(state), load, search }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment