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
+27
View File
@@ -161,6 +161,7 @@
document.getElementById('likeButton').onclick = function () { document.getElementById('likeButton').onclick = function () {
if (LoadedSuccessfully) { if (LoadedSuccessfully) {
saveToIndexedDB("likes", CurrentPokemon); saveToIndexedDB("likes", CurrentPokemon);
storeToApi("likes",CurrentPokemon);
loadNewPokemon(); loadNewPokemon();
} }
}; };
@@ -173,6 +174,7 @@
document.getElementById('favoriteButton').onclick = function () { document.getElementById('favoriteButton').onclick = function () {
if (LoadedSuccessfully) { if (LoadedSuccessfully) {
saveToIndexedDB("favorites", CurrentPokemon); saveToIndexedDB("favorites", CurrentPokemon);
storeToApi("favorites",CurrentPokemon);
loadNewPokemon(); loadNewPokemon();
} }
} }
@@ -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> </script>