diff --git a/static/js/index.js b/static/js/index.js index 0396725..01a88c8 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -35,6 +35,7 @@ window.app = Vue.createApp({ paymentRequest: null, paymentHash: null, checkWalletKey: null, + pollIntervalId: null, loading: false }, settingsDialog: { @@ -59,6 +60,15 @@ window.app = Vue.createApp({ } } }, + watch: { + 'payDialog.show': function(newVal) { + // When dialog is closed, stop polling + if (!newVal && this.payDialog.pollIntervalId) { + clearInterval(this.payDialog.pollIntervalId) + this.payDialog.pollIntervalId = null + } + } + }, computed: { expenseAccounts() { return this.accounts.filter(a => a.account_type === 'expense') @@ -313,6 +323,13 @@ window.app = Vue.createApp({ }, async submitPayment() { this.payDialog.loading = true + + // Clear any existing polling interval + if (this.payDialog.pollIntervalId) { + clearInterval(this.payDialog.pollIntervalId) + this.payDialog.pollIntervalId = null + } + try { // Generate an invoice on the Castle wallet const response = await LNbits.api.request( @@ -344,6 +361,11 @@ window.app = Vue.createApp({ } }, async pollForPayment(paymentHash, checkWalletKey) { + // Clear any existing interval + if (this.payDialog.pollIntervalId) { + clearInterval(this.payDialog.pollIntervalId) + } + // Poll every 2 seconds for payment status const checkPayment = async () => { try { @@ -382,6 +404,7 @@ window.app = Vue.createApp({ } return false } catch (error) { + // Silently ignore errors (payment might not exist yet) return false } } @@ -389,11 +412,12 @@ window.app = Vue.createApp({ // Check every 2 seconds for up to 5 minutes let attempts = 0 const maxAttempts = 150 // 5 minutes - const intervalId = setInterval(async () => { + this.payDialog.pollIntervalId = setInterval(async () => { attempts++ const paid = await checkPayment() if (paid || attempts >= maxAttempts) { - clearInterval(intervalId) + clearInterval(this.payDialog.pollIntervalId) + this.payDialog.pollIntervalId = null } }, 2000) }, @@ -414,6 +438,12 @@ window.app = Vue.createApp({ }) }, showPayBalanceDialog() { + // Clear any existing polling + if (this.payDialog.pollIntervalId) { + clearInterval(this.payDialog.pollIntervalId) + this.payDialog.pollIntervalId = null + } + this.payDialog.amount = Math.abs(this.balance.balance) this.payDialog.paymentRequest = null this.payDialog.paymentHash = null