mirror of
https://github.com/basicswap/basicswap.git
synced 2024-11-17 00:07:56 +00:00
ui: JS fix
This commit is contained in:
parent
44f16bd28e
commit
4711d72bf5
1 changed files with 103 additions and 94 deletions
|
@ -1069,7 +1069,7 @@ const chart = new Chart(ctx, {
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
const coinNameToSymbol = {
|
const coinNameToSymbol = {
|
||||||
'Bitcoin': 'BTC',
|
'Bitcoin': 'BTC',
|
||||||
'Particl': 'PART',
|
'Particl': 'PART',
|
||||||
'Particl Blind': 'PART',
|
'Particl Blind': 'PART',
|
||||||
|
@ -1079,11 +1079,11 @@ const chart = new Chart(ctx, {
|
||||||
'Firo': 'FIRO',
|
'Firo': 'FIRO',
|
||||||
'Dash': 'DASH',
|
'Dash': 'DASH',
|
||||||
'PIVX': 'PIVX'
|
'PIVX': 'PIVX'
|
||||||
};
|
};
|
||||||
|
|
||||||
const exchangeRateCache = {};
|
const exchangeRateCache = {};
|
||||||
|
|
||||||
function updateUsdValue(cryptoCell, coinFullName, isRate = false) {
|
function updateUsdValue(cryptoCell, coinFullName, isRate = false) {
|
||||||
const coinSymbol = coinNameToSymbol[coinFullName];
|
const coinSymbol = coinNameToSymbol[coinFullName];
|
||||||
if (!coinSymbol) {
|
if (!coinSymbol) {
|
||||||
console.error(`Coin symbol not found for full name: ${coinFullName}`);
|
console.error(`Coin symbol not found for full name: ${coinFullName}`);
|
||||||
|
@ -1092,11 +1092,29 @@ const chart = new Chart(ctx, {
|
||||||
|
|
||||||
const cryptoValue = parseFloat(cryptoCell.textContent);
|
const cryptoValue = parseFloat(cryptoCell.textContent);
|
||||||
const usdCell = cryptoCell.nextElementSibling;
|
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) {
|
if (isRate) {
|
||||||
const rateCell = usdCell.nextElementSibling;
|
const rateCell = usdCell.nextElementSibling;
|
||||||
|
if (rateCell) {
|
||||||
const usdValue = rateCell.previousElementSibling.textContent * cryptoValue;
|
const usdValue = rateCell.previousElementSibling.textContent * cryptoValue;
|
||||||
usdCell.textContent = `${usdValue.toFixed(2)} USD`;
|
usdCell.textContent = `${usdValue.toFixed(2)} USD`;
|
||||||
|
} else {
|
||||||
|
console.error("Rate cell does not exist.");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const apiUrl = `https://min-api.cryptocompare.com/data/price?fsym=${coinSymbol}&tsyms=USD`;
|
const apiUrl = `https://min-api.cryptocompare.com/data/price?fsym=${coinSymbol}&tsyms=USD`;
|
||||||
|
|
||||||
|
@ -1134,17 +1152,15 @@ const chart = new Chart(ctx, {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error('Error retrieving exchange rate:', error);
|
|
||||||
usdCell.textContent = 'Error retrieving exchange rate';
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isCacheExpired(coinSymbol) {
|
function isCacheExpired(coinSymbol) {
|
||||||
const cacheEntry = exchangeRateCache[coinSymbol];
|
const cacheEntry = exchangeRateCache[coinSymbol];
|
||||||
return cacheEntry && (Date.now() - cacheEntry.timestamp > cacheEntry.ttl);
|
return cacheEntry && (Date.now() - cacheEntry.timestamp > cacheEntry.ttl);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateProfitLoss(row) {
|
function updateProfitLoss(row) {
|
||||||
const buyingUSD = parseFloat(row.querySelector('.usd-value').textContent);
|
const buyingUSD = parseFloat(row.querySelector('.usd-value').textContent);
|
||||||
|
@ -1171,7 +1187,7 @@ function updateProfitLoss(row) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateProfitValue(row) {
|
function updateProfitValue(row) {
|
||||||
const sellingUSD = parseFloat(row.querySelector('.usd-value').textContent);
|
const sellingUSD = parseFloat(row.querySelector('.usd-value').textContent);
|
||||||
const profitValueCell = row.querySelector('.profit-value');
|
const profitValueCell = row.querySelector('.profit-value');
|
||||||
|
|
||||||
|
@ -1180,24 +1196,17 @@ function updateProfitLoss(row) {
|
||||||
} else {
|
} else {
|
||||||
profitValueCell.textContent = 'Invalid USD value';
|
profitValueCell.textContent = 'Invalid USD value';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
const coinNameValues = document.querySelectorAll('.coinname-value');
|
const coinNameValues = document.querySelectorAll('.coinname-value');
|
||||||
for (let i = 0; i < coinNameValues.length; i++) {
|
coinNameValues.forEach(coinNameValue => {
|
||||||
const coinFullName = coinNameValues[i].getAttribute('data-coinname');
|
const coinFullName = coinNameValue.getAttribute('data-coinname');
|
||||||
const isRate = coinNameValues[i].textContent.includes('rate');
|
const isRateElement = coinNameValue.parentElement.querySelector('.ratetype');
|
||||||
updateUsdValue(coinNameValues[i], coinFullName, isRate);
|
const isRate = isRateElement ? isRateElement.textContent.includes('rate') : false;
|
||||||
|
updateUsdValue(coinNameValue, coinFullName, isRate);
|
||||||
if (!isRate) {
|
|
||||||
coinNameValues[i].addEventListener('input', () => {
|
|
||||||
console.log(`Clearing cache for ${coinNameToSymbol[coinFullName]}`);
|
|
||||||
exchangeRateCache[coinNameToSymbol[coinFullName]] = undefined;
|
|
||||||
updateUsdValue(coinNameValues[i], coinFullName);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
{% include 'footer.html' %}
|
{% include 'footer.html' %}
|
||||||
</body>
|
</body>
|
||||||
|
|
Loading…
Reference in a new issue