Merge pull request #65 from gerlofvanek/dev

ui: JS fix
This commit is contained in:
Gerlof van Ek 2024-03-09 19:50:39 +01:00 committed by GitHub
commit 31a1224311
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1092,11 +1092,29 @@ const chart = new Chart(ctx, {
const cryptoValue = parseFloat(cryptoCell.textContent);
const usdCell = cryptoCell.nextElementSibling;
if (usdCell) {
// Check if the exchange rate is in the cache and is not expired
if (exchangeRateCache[coinSymbol] && !isCacheExpired(coinSymbol)) {
console.log(`Using cached exchange rate for ${coinSymbol}`);
const exchangeRate = exchangeRateCache[coinSymbol].rate;
const cryptoValue = parseFloat(cryptoCell.textContent);
const usdValue = cryptoValue * exchangeRate;
usdCell.textContent = `${usdValue.toFixed(2)} USD`;
updateProfitLoss(cryptoCell.closest('tr'));
updateProfitValue(cryptoCell.closest('tr'));
}
} else {
}
if (isRate) {
const rateCell = usdCell.nextElementSibling;
if (rateCell) {
const usdValue = rateCell.previousElementSibling.textContent * cryptoValue;
usdCell.textContent = `${usdValue.toFixed(2)} USD`;
} else {
console.error("Rate cell does not exist.");
}
} else {
const apiUrl = `https://min-api.cryptocompare.com/data/price?fsym=${coinSymbol}&tsyms=USD`;
@ -1134,8 +1152,6 @@ const chart = new Chart(ctx, {
}
})
.catch(error => {
console.error('Error retrieving exchange rate:', error);
usdCell.textContent = 'Error retrieving exchange rate';
});
}
}
@ -1184,19 +1200,12 @@ function updateProfitLoss(row) {
document.addEventListener('DOMContentLoaded', () => {
const coinNameValues = document.querySelectorAll('.coinname-value');
for (let i = 0; i < coinNameValues.length; i++) {
const coinFullName = coinNameValues[i].getAttribute('data-coinname');
const isRate = coinNameValues[i].textContent.includes('rate');
updateUsdValue(coinNameValues[i], coinFullName, isRate);
if (!isRate) {
coinNameValues[i].addEventListener('input', () => {
console.log(`Clearing cache for ${coinNameToSymbol[coinFullName]}`);
exchangeRateCache[coinNameToSymbol[coinFullName]] = undefined;
updateUsdValue(coinNameValues[i], coinFullName);
coinNameValues.forEach(coinNameValue => {
const coinFullName = coinNameValue.getAttribute('data-coinname');
const isRateElement = coinNameValue.parentElement.querySelector('.ratetype');
const isRate = isRateElement ? isRateElement.textContent.includes('rate') : false;
updateUsdValue(coinNameValue, coinFullName, isRate);
});
}
}
});
</script>
{% include 'footer.html' %}