feature/improved the getting of rates

This commit is contained in:
Joaquin committed 2025-05-06 21:26:45 -07:00
1 parent 93a5c45dd9
commit 17f11c85df
9 files changed
+44 -58

No files matched your search

-132
View File
@@ -1,132 +0,0 @@
{
"countries": [
{
"country": "United States",
"currency_abbreviation": "USD"
},
{
"country": "Eurozone",
"currency_abbreviation": "EUR"
},
{
"country": "United Kingdom",
"currency_abbreviation": "GBP"
},
{
"country": "Japan",
"currency_abbreviation": "JPY"
},
{
"country": "Switzerland",
"currency_abbreviation": "CHF"
},
{
"country": "Canada",
"currency_abbreviation": "CAD"
},
{
"country": "Australia",
"currency_abbreviation": "AUD"
},
{
"country": "China",
"currency_abbreviation": "CNY"
},
{
"country": "India",
"currency_abbreviation": "INR"
},
{
"country": "Russia",
"currency_abbreviation": "RUB"
},
{
"country": "Brazil",
"currency_abbreviation": "BRL"
},
{
"country": "Mexico",
"currency_abbreviation": "MXN"
},
{
"country": "South Korea",
"currency_abbreviation": "KRW"
},
{
"country": "Indonesia",
"currency_abbreviation": "IDR"
},
{
"country": "Bulgaria",
"currency_abbreviation": "BGN"
},
{
"country": "Czech Republic",
"currency_abbreviation": "CZK"
},
{
"country": "Denmark",
"currency_abbreviation": "DKK"
},
{
"country": "Hong Kong",
"currency_abbreviation": "HKD"
},
{
"country": "Hungary",
"currency_abbreviation": "HUF"
},
{
"country": "Israel",
"currency_abbreviation": "ILS"
},
{
"country": "Iceland",
"currency_abbreviation": "ISK"
},
{
"country": "Malaysia",
"currency_abbreviation": "MYR"
},
{
"country": "Norway",
"currency_abbreviation": "NOK"
},
{
"country": "New Zealand",
"currency_abbreviation": "NZD"
},
{
"country": "Philippines",
"currency_abbreviation": "PHP"
},
{
"country": "Poland",
"currency_abbreviation": "PLN"
},
{
"country": "Romania",
"currency_abbreviation": "RON"
},
{
"country": "Sweden",
"currency_abbreviation": "SEK"
},
{
"country": "Singapore",
"currency_abbreviation": "SGD"
},
{
"country": "Thailand",
"currency_abbreviation": "THB"
},
{
"country": "Turkey",
"currency_abbreviation": "TRY"
},
{
"country": "South Africa",
"currency_abbreviation": "ZAR"
}
]
}
-6
View File
@@ -1,6 +0,0 @@
async function convert(from) {
const res = await fetch(`https://api.frankfurter.dev/v1/latest?base=${from}`);
const data = await res.json();
return data;
}
+7 -30
View File
@@ -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();