Merge pull request #10758 from cnohall/fix-refreshbtcprice-being-out-of-scope
Fix refreshBTCPrice being out of scope
This commit is contained in:
commit
d02a02ddda
|
|
@ -16,7 +16,8 @@ class Blockonomics {
|
||||||
// Bind the method to the instance
|
// Bind the method to the instance
|
||||||
this.copyToClipboard = this.copyToClipboard.bind(this);
|
this.copyToClipboard = this.copyToClipboard.bind(this);
|
||||||
this.refreshBTCPrice = this.refreshBTCPrice.bind(this);
|
this.refreshBTCPrice = this.refreshBTCPrice.bind(this);
|
||||||
|
this.fetchAndDisplayQRCode = this.fetchAndDisplayQRCode.bind(this);
|
||||||
|
this.startTimer = this.startTimer.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
copyToClipboard(elementId, passedElement, shouldGrabNextElementSibling) {
|
copyToClipboard(elementId, passedElement, shouldGrabNextElementSibling) {
|
||||||
|
|
@ -48,47 +49,25 @@ class Blockonomics {
|
||||||
}, 5000);
|
}, 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 {
|
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;
|
const btcAddress = document.querySelector('meta[name="btc_address"]').content;
|
||||||
|
const btcAmount = newBtcAmount || '{{$btc_amount}}';
|
||||||
// set the href attribute of the link to the new bitcoin amount
|
const qrString = encodeURIComponent(`bitcoin:${btcAddress}?amount=${btcAmount}`);
|
||||||
const qrCodeLink = document.getElementById('qr-code-link');
|
const response = await fetch(`/api/v1/get-blockonomics-qr-code?qr_string=${qrString}`);
|
||||||
const openInWalletLink = document.getElementById('open-in-wallet-link');
|
if (!response.ok) {
|
||||||
qrCodeLink.href = `bitcoin:${btcAddress}?amount=${newBtcAmount}`;
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
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 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 = (seconds) => {
|
||||||
handle() {
|
|
||||||
window.copyToClipboard = this.copyToClipboard;
|
|
||||||
window.refreshBTCPrice = this.refreshBTCPrice;
|
|
||||||
|
|
||||||
const startTimer = (seconds) => {
|
|
||||||
const countDownDate = new Date().getTime() + seconds * 1000;
|
const countDownDate = new Date().getTime() + seconds * 1000;
|
||||||
document.getElementById("countdown").innerHTML = "10" + ":" + "00" + " min";
|
document.getElementById("countdown").innerHTML = "10" + ":" + "00" + " min";
|
||||||
|
|
||||||
|
|
@ -117,6 +96,12 @@ class Blockonomics {
|
||||||
window.countdownInterval = setInterval(updateCountdown, 1000);
|
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 () => {
|
const getBTCPrice = async () => {
|
||||||
try {
|
try {
|
||||||
const currency = document.querySelector('meta[name="currency"]').content;
|
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 connectToWebsocket = () => {
|
||||||
const btcAddress = document.querySelector('meta[name="btc_address"]').content;
|
const btcAddress = document.querySelector('meta[name="btc_address"]').content;
|
||||||
const webSocketUrl = `wss://www.blockonomics.co/payment/${btcAddress}`;
|
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)
|
startTimer(600); // Start timer for 10 minutes (600 seconds)
|
||||||
connectToWebsocket();
|
connectToWebsocket();
|
||||||
fetchAndDisplayQRCode();
|
fetchAndDisplayQRCode();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue