refactor: replace promises with async/await

This commit is contained in:
Vlad Stan
2022-07-06 16:16:29 +03:00
parent 5a005e830c
commit 6cfa8e30bc

View File

@@ -246,7 +246,7 @@
filled filled
dense dense
emit-value emit-value
v-model="formDialogCharge.data.onchainwallet" v-model="onchainwallet"
:options="walletLinks" :options="walletLinks"
label="Onchain Wallet" label="Onchain Wallet"
/> />
@@ -401,6 +401,7 @@
show: false, show: false,
data: { data: {
onchain: false, onchain: false,
onchainwallet: '',
lnbits: false, lnbits: false,
description: '', description: '',
time: null, time: null,
@@ -426,24 +427,21 @@
self.formDialogCharge.show = false self.formDialogCharge.show = false
}, },
getWalletLinks: function () { getWalletLinks: async function () {
var self = this try {
const {data} = await LNbits.api.request(
LNbits.api
.request(
'GET', 'GET',
'/watchonly/api/v1/wallet', '/watchonly/api/v1/wallet',
this.g.user.wallets[0].inkey this.g.user.wallets[0].inkey
) )
.then(function (response) { this.walletLinks = data.map(w => ({
for (i = 0; i < response.data.length; i++) { id: w.id,
self.walletLinks.push(response.data[i].id) label: w.title
} }))
return
}) } catch (error) {
.catch(function (error) { LNbits.utils.notifyApiError(error)
LNbits.utils.notifyApiError(error) }
})
}, },
closeFormDialog: function () { closeFormDialog: function () {
this.formDialog.data = { this.formDialog.data = {
@@ -457,22 +455,17 @@
self.current = linkId self.current = linkId
self.Addresses.show = true self.Addresses.show = true
}, },
getCharges: function () { getCharges: async function () {
var self = this try {
var getAddressBalance = this.getAddressBalance const {data} = await LNbits.api.request(
LNbits.api
.request(
'GET', 'GET',
'/satspay/api/v1/charges', '/satspay/api/v1/charges',
this.g.user.wallets[0].inkey this.g.user.wallets[0].inkey
) )
.then(function (response) { this.ChargeLinks = data.map(mapCharge)
self.ChargeLinks = response.data.map(mapCharge) } catch (error) {
}) LNbits.utils.notifyApiError(error)
.catch(function (error) { }
LNbits.utils.notifyApiError(error)
})
}, },
sendFormDataCharge: function () { sendFormDataCharge: function () {
var self = this var self = this
@@ -480,6 +473,7 @@
var data = this.formDialogCharge.data var data = this.formDialogCharge.data
data.amount = parseInt(data.amount) data.amount = parseInt(data.amount)
data.time = parseInt(data.time) data.time = parseInt(data.time)
data.onchainwallet = this.onchainwallet?.id
this.createCharge(wallet, data) this.createCharge(wallet, data)
}, },
timerCount: function () { timerCount: function () {
@@ -487,40 +481,40 @@
var refreshIntervalId = setInterval(function () { var refreshIntervalId = setInterval(function () {
for (i = 0; i < self.ChargeLinks.length - 1; i++) { for (i = 0; i < self.ChargeLinks.length - 1; i++) {
if (self.ChargeLinks[i]['paid'] == 'True') { if (self.ChargeLinks[i]['paid'] == 'True') {
setTimeout(function () { setTimeout(async function () {
LNbits.api await LNbits.api.request(
.request( 'GET',
'GET', '/satspay/api/v1/charges/balance/' +
'/satspay/api/v1/charges/balance/' + self.ChargeLinks[i]['id'],
self.ChargeLinks[i]['id'], 'filla'
'filla' )
)
.then(function (response) {})
}, 2000) }, 2000)
} }
} }
self.getCharges() self.getCharges()
}, 20000) }, 20000)
}, },
createCharge: function (wallet, data) { createCharge: async function (wallet, data) {
var self = this try {
const resp = await LNbits.api.request(
'POST',
'/satspay/api/v1/charge',
wallet,
data
)
LNbits.api this.ChargeLinks.push(mapCharge(resp.data))
.request('POST', '/satspay/api/v1/charge', wallet, data) this.formDialogCharge.show = false
.then(function (response) { this.formDialogCharge.data = {
self.ChargeLinks.push(mapCharge(response.data)) onchain: false,
self.formDialogCharge.show = false lnbits: false,
self.formDialogCharge.data = { description: '',
onchain: false, time: null,
lnbits: false, amount: null
description: '', }
time: null, } catch (error) {
amount: null LNbits.utils.notifyApiError(error)
} }
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)
})
}, },
deleteChargeLink: function (chargeId) { deleteChargeLink: function (chargeId) {
@@ -528,26 +522,24 @@
var link = _.findWhere(this.ChargeLinks, {id: chargeId}) var link = _.findWhere(this.ChargeLinks, {id: chargeId})
LNbits.utils LNbits.utils
.confirmDialog('Are you sure you want to delete this pay link?') .confirmDialog('Are you sure you want to delete this pay link?')
.onOk(function () { .onOk(async function () {
LNbits.api try {
.request( const response = await LNbits.api.request(
'DELETE', 'DELETE',
'/satspay/api/v1/charge/' + chargeId, '/satspay/api/v1/charge/' + chargeId,
self.g.user.wallets[0].adminkey self.g.user.wallets[0].adminkey
) )
.then(function (response) {
self.ChargeLinks = _.reject(self.ChargeLinks, function (obj) { self.ChargeLinks = _.reject(self.ChargeLinks, function (obj) {
return obj.id === chargeId return obj.id === chargeId
})
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)
}) })
} catch (error) {
LNbits.utils.notifyApiError(error)
}
}) })
}, },
exportchargeCSV: function () { exportchargeCSV: function () {
var self = this LNbits.utils.exportCSV(this.ChargesTable.columns, this.ChargeLinks)
LNbits.utils.exportCSV(self.ChargesTable.columns, this.ChargeLinks)
} }
}, },
created: function () { created: function () {