Merge pull request #10758 from cnohall/fix-refreshbtcprice-being-out-of-scope

Fix refreshBTCPrice being out of scope
This commit is contained in:
David Bomba 2025-03-11 15:43:18 +11:00 committed by GitHub
commit d02a02ddda
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 91 additions and 89 deletions

View File

@ -16,7 +16,8 @@ class Blockonomics {
// Bind the method to the instance
this.copyToClipboard = this.copyToClipboard.bind(this);
this.refreshBTCPrice = this.refreshBTCPrice.bind(this);
this.fetchAndDisplayQRCode = this.fetchAndDisplayQRCode.bind(this);
this.startTimer = this.startTimer.bind(this);
}
copyToClipboard(elementId, passedElement, shouldGrabNextElementSibling) {
@ -48,47 +49,25 @@ class Blockonomics {
}, 5000);
}
async refreshBTCPrice() {
const refreshIcon = document.querySelector('.icon-refresh');
refreshIcon.classList.add('rotating');
document.getElementsByClassName("btc-value")[0].innerHTML = "Refreshing...";
async fetchAndDisplayQRCode (newBtcAmount = null) {
try {
const newPrice = await getBTCPrice();
if (newPrice) {
// Update the text content of the countdown span to the new bitcoin price
const currency = document.querySelector('meta[name="currency"]').content;
document.getElementsByClassName("btc-value")[0].innerHTML = "1 BTC = " + (newPrice || "N/A") + " " + currency + ", updates in <span id='countdown'></span>";
const newBtcAmount = (document.querySelector('meta[name="amount"]').content / newPrice).toFixed(10);
// set the value of the input field and the text content of the span to the new bitcoin amount
document.querySelector('input[name="btc_price"]').value = newPrice;
document.querySelector('input[name="btc_amount"]').value = newBtcAmount;
document.getElementById('btc-amount').textContent = newBtcAmount;
const btcAddress = document.querySelector('meta[name="btc_address"]').content;
// set the href attribute of the link to the new bitcoin amount
const qrCodeLink = document.getElementById('qr-code-link');
const openInWalletLink = document.getElementById('open-in-wallet-link');
qrCodeLink.href = `bitcoin:${btcAddress}?amount=${newBtcAmount}`;
openInWalletLink.href = `bitcoin:${btcAddress}?amount=${newBtcAmount}`;
// fetch and display the new QR code
fetchAndDisplayQRCode(newBtcAmount);
startTimer(600); // Restart timer for 10 minutes (600 seconds)
}
} finally {
refreshIcon.classList.remove('rotating');
const btcAmount = newBtcAmount || '{{$btc_amount}}';
const qrString = encodeURIComponent(`bitcoin:${btcAddress}?amount=${btcAmount}`);
const response = await fetch(`/api/v1/get-blockonomics-qr-code?qr_string=${qrString}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const svgText = await response.text();
document.getElementById('qrcode-container').innerHTML = svgText;
} catch (error) {
console.error('Error fetching QR code:', error);
document.getElementById('qrcode-container').textContent = 'Error loading QR code';
}
};
handle() {
window.copyToClipboard = this.copyToClipboard;
window.refreshBTCPrice = this.refreshBTCPrice;
const startTimer = (seconds) => {
startTimer = (seconds) => {
const countDownDate = new Date().getTime() + seconds * 1000;
document.getElementById("countdown").innerHTML = "10" + ":" + "00" + " min";
@ -117,6 +96,12 @@ class Blockonomics {
window.countdownInterval = setInterval(updateCountdown, 1000);
}
async refreshBTCPrice() {
const refreshIcon = document.querySelector('.icon-refresh');
refreshIcon.classList.add('rotating');
document.getElementsByClassName("btc-value")[0].innerHTML = "Refreshing...";
const getBTCPrice = async () => {
try {
const currency = document.querySelector('meta[name="currency"]').content;
@ -132,6 +117,43 @@ class Blockonomics {
}
}
try {
const newPrice = await getBTCPrice();
if (newPrice) {
// Update the text content of the countdown span to the new bitcoin price
const currency = document.querySelector('meta[name="currency"]').content;
document.getElementsByClassName("btc-value")[0].innerHTML = "1 BTC = " + (newPrice || "N/A") + " " + currency + ", updates in <span id='countdown'></span>";
const newBtcAmount = (document.querySelector('meta[name="amount"]').content / newPrice).toFixed(10);
// set the value of the input field and the text content of the span to the new bitcoin amount
document.querySelector('input[name="btc_price"]').value = newPrice;
document.querySelector('input[name="btc_amount"]').value = newBtcAmount;
document.getElementById('btc-amount').textContent = newBtcAmount;
const btcAddress = document.querySelector('meta[name="btc_address"]').content;
// set the href attribute of the link to the new bitcoin amount
const qrCodeLink = document.getElementById('qr-code-link');
const openInWalletLink = document.getElementById('open-in-wallet-link');
qrCodeLink.href = `bitcoin:${btcAddress}?amount=${newBtcAmount}`;
openInWalletLink.href = `bitcoin:${btcAddress}?amount=${newBtcAmount}`;
// fetch and display the new QR code
await this.fetchAndDisplayQRCode(newBtcAmount);
this.startTimer(600); // Restart timer for 10 minutes (600 seconds)
}
} finally {
refreshIcon.classList.remove('rotating');
}
}
handle() {
window.copyToClipboard = this.copyToClipboard;
window.refreshBTCPrice = this.refreshBTCPrice;
window.fetchAndDisplayQRCode = this.fetchAndDisplayQRCode;
window.startTimer = this.startTimer;
const connectToWebsocket = () => {
const btcAddress = document.querySelector('meta[name="btc_address"]').content;
const webSocketUrl = `wss://www.blockonomics.co/payment/${btcAddress}`;
@ -150,29 +172,9 @@ class Blockonomics {
}
}
};
const fetchAndDisplayQRCode = async (newBtcAmount = null) => {
try {
const btcAddress = document.querySelector('meta[name="btc_address"]').content;
const btcAmount = newBtcAmount || '{{$btc_amount}}';
const qrString = encodeURIComponent(`bitcoin:${btcAddress}?amount=${btcAmount}`);
const response = await fetch(`/api/v1/get-blockonomics-qr-code?qr_string=${qrString}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const svgText = await response.text();
document.getElementById('qrcode-container').innerHTML = svgText;
} catch (error) {
console.error('Error fetching QR code:', error);
document.getElementById('qrcode-container').textContent = 'Error loading QR code';
}
};
startTimer(600); // Start timer for 10 minutes (600 seconds)
connectToWebsocket();
fetchAndDisplayQRCode();
}
}