splitpayments/static/js/index.js
Arc 81184a0a53
feat: adds a wallet select to picking wallet + uv (#37)
* Adds a wallet select to picking wallet

* added uv

* uv uv uv

---------

Co-authored-by: dni  <office@dnilabs.com>
2026-05-05 22:53:59 +01:00

127 lines
3 KiB
JavaScript

function hashTargets(targets) {
return targets
.filter(isTargetComplete)
.map(({wallet, percent, alias}) => `${wallet}${percent}${alias}`)
.join('')
}
function isTargetComplete(target) {
return (
target.wallet &&
target.wallet.trim() !== '' &&
(target.percent > 0 || target.tag != '')
)
}
window.app = Vue.createApp({
el: '#vue',
mixins: [windowMixin],
watch: {
selectedWallet() {
this.getTargets()
}
},
data() {
return {
selectedWallet: null,
currentHash: '', // a string that must match if the edit data is unchanged
targets: []
}
},
computed: {
isDirty() {
return hashTargets(this.targets) !== this.currentHash
}
},
methods: {
clearTarget(index) {
if (this.targets.length == 1) {
return this.deleteTargets()
}
this.targets.splice(index, 1)
Quasar.Notify.create({
message: 'Removed item. You must click to save manually.',
timeout: 500
})
},
getTargets() {
LNbits.api
.request(
'GET',
'/splitpayments/api/v1/targets',
this.selectedWallet.adminkey
)
.then(res => {
this.targets = res.data.map(t => ({
...t,
targetChoice: t.targetChoice || 'wallet'
}))
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
},
changedWallet(wallet) {
this.selectedWallet = wallet
this.getTargets()
},
addTarget() {
this.targets.push({
source: this.selectedWallet,
targetChoice: 'wallet'
})
},
saveTargets() {
const payload = this.targets
.filter(t => t.wallet && String(t.wallet).trim() !== '')
.map(({alias, percent, wallet}) => ({
alias,
percent: Number(percent) || 0,
wallet
}))
LNbits.api
.request(
'PUT',
'/splitpayments/api/v1/targets',
this.selectedWallet.adminkey,
{
targets: payload
}
)
.then(response => {
Quasar.Notify.create({
message: 'Split payments targets set.',
timeout: 700
})
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
},
deleteTargets() {
LNbits.utils
.confirmDialog('Are you sure you want to delete all targets?')
.onOk(() => {
this.targets = []
LNbits.api
.request(
'DELETE',
'/splitpayments/api/v1/targets',
this.selectedWallet.adminkey
)
.then(response => {
Quasar.Notify.create({
message: 'Split payments targets deleted.',
timeout: 700
})
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
})
}
},
created() {
this.selectedWallet = this.g.user.wallets[0]
}
})