feature/improved the getting of rates
This commit is contained in:
1 parent
93a5c45dd9
commit
17f11c85df
9 files changed
+44
-58
No files matched your search
@@ -1,43 +1,20 @@
|
||||
async function diffCountries() {
|
||||
const res = await fetch("/static/json/countries.json");
|
||||
const data = await res.json();
|
||||
|
||||
return data.countries;
|
||||
}
|
||||
|
||||
function getLocation() {
|
||||
if (navigator.geolocation) {
|
||||
navigator.geolocation.getCurrentPosition(success, error);
|
||||
navigator.geolocation.getCurrentPosition(getLatestExchange, error);
|
||||
}
|
||||
}
|
||||
|
||||
async function success(position) {
|
||||
const { latitude, longitude } = position.coords;
|
||||
let countryCurrency;
|
||||
|
||||
const countries = await diffCountries();
|
||||
|
||||
const res = await fetch("api/location" , {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({ latitude, longitude })
|
||||
});
|
||||
|
||||
async function getLatestExchange(position) {
|
||||
let lat = position.coords.latitude;
|
||||
let lon = position.coords.longitude;
|
||||
const res = await fetch(`/exRates/${lat}/${lon}`);
|
||||
const data = await res.json();
|
||||
|
||||
countries.forEach(country => {
|
||||
if (country.country === data.results[0].formatted_address) {
|
||||
countryCurrency = country.currency_abbreviation;
|
||||
|
||||
convert(countryCurrency);
|
||||
}
|
||||
});
|
||||
console.log(data);
|
||||
}
|
||||
|
||||
function error(err) {
|
||||
console.error("Geolocation error: ", err);
|
||||
}
|
||||
|
||||
//getLocation();
|
||||
getLocation();
|
||||
Reference in new issue
Block a user