Now favorites and likes are stored also into the api

This commit is contained in:
maxluli
2022-10-04 09:39:49 +02:00
parent be666ac326
commit c475b6323d

View File

@@ -97,7 +97,7 @@
document.getElementById('main').onload = function () {
loadNewPokemon();
}
function loadNewPokemon(){
function loadNewPokemon() {
//We will need to load an image from the pokeAPI with his description and a random name/age
fetch("https://pokeapi.co/api/v2/pokemon/" + Math.floor(Math.random() * 905))
.then((response) => {
@@ -159,20 +159,22 @@
})
}
document.getElementById('likeButton').onclick = function () {
if(LoadedSuccessfully){
saveToIndexedDB("likes",CurrentPokemon);
if (LoadedSuccessfully) {
saveToIndexedDB("likes", CurrentPokemon);
storeToApi("likes",CurrentPokemon);
loadNewPokemon();
}
};
document.getElementById('nopeButton').onclick = function () {
if(LoadedSuccessfully){
saveToIndexedDB("nopes",CurrentPokemon);
if (LoadedSuccessfully) {
saveToIndexedDB("nopes", CurrentPokemon);
loadNewPokemon();
}
}
document.getElementById('favoriteButton').onclick = function () {
if(LoadedSuccessfully){
saveToIndexedDB("favorites",CurrentPokemon);
if (LoadedSuccessfully) {
saveToIndexedDB("favorites", CurrentPokemon);
storeToApi("favorites",CurrentPokemon);
loadNewPokemon();
}
}
@@ -199,7 +201,7 @@
window.navigator.geolocation.getCurrentPosition(function (pos) {
//console.log(pos);
//console.log(
//distance(pos.coords.longitude, pos.coords.latitude, 42.37, 71.03)
//distance(pos.coords.longitude, pos.coords.latitude, 42.37, 71.03)
//);
});
@@ -239,6 +241,31 @@
}
);
}
//This code... you wont believe it... its still not mine HAHAHAHAHA but now its from postman
function storeToApi(storeName,pokemon) {
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");
var urlencoded = new URLSearchParams();
urlencoded.append("name", pokemon.name);
urlencoded.append("pokemonId", pokemon.pokemonId);
urlencoded.append("pp", pokemon.pp);
urlencoded.append("age", pokemon.age);
urlencoded.append("description", pokemon.description);
urlencoded.append("indexDbId", pokemon.id);
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: urlencoded,
redirect: 'follow'
};
fetch("https://633bde0df11701a65f69abe8.mockapi.io/api/v1/"+storeName+"/", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
}
</script>