Skip to content

Instantly share code, notes, and snippets.

@DeaVenditama
Created November 6, 2020 19:05
Show Gist options
  • Save DeaVenditama/3940efc1458959577aadd46c9299c17a to your computer and use it in GitHub Desktop.
Save DeaVenditama/3940efc1458959577aadd46c9299c17a to your computer and use it in GitHub Desktop.
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