Now favorites and likes are stored also into the api
This commit is contained in:
+35
-8
@@ -97,7 +97,7 @@
|
|||||||
document.getElementById('main').onload = function () {
|
document.getElementById('main').onload = function () {
|
||||||
loadNewPokemon();
|
loadNewPokemon();
|
||||||
}
|
}
|
||||||
function loadNewPokemon(){
|
function loadNewPokemon() {
|
||||||
//We will need to load an image from the pokeAPI with his description and a random name/age
|
//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))
|
fetch("https://pokeapi.co/api/v2/pokemon/" + Math.floor(Math.random() * 905))
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
@@ -159,20 +159,22 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
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();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
document.getElementById('nopeButton').onclick = function () {
|
document.getElementById('nopeButton').onclick = function () {
|
||||||
if(LoadedSuccessfully){
|
if (LoadedSuccessfully) {
|
||||||
saveToIndexedDB("nopes",CurrentPokemon);
|
saveToIndexedDB("nopes", CurrentPokemon);
|
||||||
loadNewPokemon();
|
loadNewPokemon();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -199,7 +201,7 @@
|
|||||||
window.navigator.geolocation.getCurrentPosition(function (pos) {
|
window.navigator.geolocation.getCurrentPosition(function (pos) {
|
||||||
//console.log(pos);
|
//console.log(pos);
|
||||||
//console.log(
|
//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>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user