function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getLatestExchange, error);
}
}
function update(data) {
document.getElementById("loading").style = "display: none";
document.getElementById("currencyExchange").style = "display: flex";
document.getElementById("yourFlag").src = `/static/svgs/flags/${data.data.country}.svg`
document.getElementById("yourFlag").alt = data.data.country;
document.getElementById("flagTag").innerHTML = `(${data.data.country})`;
document.getElementById("exchangeFlag").src = `/static/svgs/flags/USD.svg`;
document.getElementById("exchangeFlag").alt = "USD";
document.getElementById("exFlagTag").innerHTML = "(USD)";
document.getElementById("dropdown-country-button").value = data.data.toCurrencyRates["USD"];
updateExchange(document.getElementById("dropdown-country-button").value);
const dropdown = document.getElementById("dropdown");
let countries = Object.keys(data.data.toCurrencyRates);
countries.forEach(item => {
if (dropdown.querySelector(`button[value="${data.data.toCurrencyRates[item]}"]`)) {
return;
}
const listItem = document.createElement("li");
listItem.innerHTML = ``;
dropdown.appendChild(listItem);
});
console.log(data);
}
function switchButton(clickedButton) {
document.getElementById("dropdown-country-button").value = clickedButton.value;
updateExchange(document.getElementById("dropdown-country-button").value);
document.getElementById("dropdown-country-button").innerHTML = clickedButton.innerHTML +
`
`;
}
function updateExchange(exRate) {
document.getElementById("exchange1").innerHTML = "$1.00 = $" +`${(1 * exRate).toFixed(2)}`;
}
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();
update(data);
}
function error(err) {
console.error("Geolocation error: ", err);
}