Skip to content

Commit

Permalink
Satoshi denominated tpos (#63)
Browse files Browse the repository at this point in the history
* allow satoshi denomination
* use of formatSat
* apply suggestions from code review
* denomIsSats is possitive now
* prettier
* some refactoring + one missed line to hide
  • Loading branch information
iWarpBTC authored Feb 11, 2024
1 parent e5e97ad commit 30dfae3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 30 deletions.
19 changes: 14 additions & 5 deletions templates/tpos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ <h6 class="text-subtitle1 q-my-none">
return {
tposs: [],
currencyOptions: [
'sats',
'USD',
'EUR',
'GBP',
Expand Down Expand Up @@ -1009,7 +1010,8 @@ <h6 class="text-subtitle1 q-my-none">
itemFormatPrice(price, id) {
const tpos = id.split(':')[0]
const currency = _.findWhere(this.tposs, {id: tpos}).currency
return LNbits.utils.formatCurrency(Number(price).toFixed(2), currency)

return this.formatAmount(price, currency)
},
openItemDialog(id) {
const [tposId, itemId] = id.split(':')
Expand Down Expand Up @@ -1154,10 +1156,7 @@ <h6 class="text-subtitle1 q-my-none">
id: tpos.wallet
})
data.forEach(item => {
item.formattedPrice = LNbits.utils.formatCurrency(
Number(item.price).toFixed(2),
tpos.currency
)
item.formattedPrice = this.formatAmount(item.price, tpos.currency)
})
this.fileDataDialog.data = data
this.fileDataDialog.count = data.length
Expand All @@ -1174,6 +1173,16 @@ <h6 class="text-subtitle1 q-my-none">
openUrlDialog(id) {
this.urlDialog.data = _.findWhere(this.tposs, {id})
this.urlDialog.show = true
},
formatAmount: function (amount, currency) {
if (currency == 'sats') {
return LNbits.utils.formatSat(amount) + ' sat'
} else {
return LNbits.utils.formatCurrency(
Number(amount).toFixed(2),
currency
)
}
}
},
created: function () {
Expand Down
71 changes: 46 additions & 25 deletions templates/tpos/tpos.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@
<div class="row justify-center full-width">
<div class="col-12 col-sm-8 col-md-6 col-lg-4 text-center">
<h3 class="q-mb-md">${ amountFormatted }</h3>
<h5 class="q-mt-none q-mb-sm">${ fsat }<small> sat</small></h5>
<h5 v-show="!denomIsSats" class="q-mt-none q-mb-sm">
${ fsat }<small> sat</small>
</h5>
<div v-if="total > 0.0">
<h5>
<i>Total: ${totalFormatted}<br />${totalfsat} sat</i>
<i
>Total: ${totalFormatted}<span v-if="!denomIsSats"
><br />${totalfsat} sat</span
></i
>
</h5>
</div>
</div>
Expand Down Expand Up @@ -220,7 +226,9 @@ <h5>
<div class="row full-width q-pa-md">
<div class="col-12 text-center">
<h3 class="q-mb-md">${totalFormatted}</h3>
<h5 class="q-mt-none q-mb-sm">${totalfsat}<small> sat</small></h5>
<h5 v-show="!denomIsSats" class="q-mt-none q-mb-sm">
${totalfsat}<small> sat</small>
</h5>
</div>
<div class="col-12">
<table class="table full-width">
Expand Down Expand Up @@ -755,28 +763,26 @@ <h5 class="q-mt-none q-mb-sm">
cartDrawer: this.$q.screen.width > 1200,
searchTerm: '',
categoryFilter: '',
cart: new Map()
cart: new Map(),
denomIsSats: '{{ tpos.currency }}' == 'sats'
}
},
computed: {
amount: function () {
if (!this.stack.length) return 0.0
return this.stack.reduce((acc, dig) => acc * 10 + dig, 0) * 0.01
return (
this.stack.reduce((acc, dig) => acc * 10 + dig, 0) *
(this.currency == 'sats' ? 1 : 0.01)
)
},
amountFormatted: function () {
return LNbits.utils.formatCurrency(
this.amount.toFixed(2),
this.currency
)
return this.formatAmount(this.amount, this.currency)
},
totalFormatted() {
return LNbits.utils.formatCurrency(this.total.toFixed(2), this.currency)
return this.formatAmount(this.total, this.currency)
},
amountWithTipFormatted: function () {
return LNbits.utils.formatCurrency(
(this.amount + this.tipAmount).toFixed(2),
this.currency
)
return this.formatAmount(this.amount + this.tipAmount, this.currency)
},
sat: function () {
if (!this.exchangeRate) return 0
Expand Down Expand Up @@ -1000,7 +1006,11 @@ <h5 class="q-mt-none q-mb-sm">
},
submitForm: function () {
if (this.total != 0.0) {
this.stack = Array.from(String(Math.ceil(this.total * 100)), Number)
if (this.currency == 'sats') {
this.stack = Array.from(String(Math.ceil(this.total), Number))
} else {
this.stack = Array.from(String(Math.ceil(this.total * 100)), Number)
}
}
if (!this.exchangeRate || this.exchangeRate == 0 || this.sat == 0) {
this.$q.notify({
Expand Down Expand Up @@ -1224,12 +1234,16 @@ <h5 class="q-mt-none q-mb-sm">
})
},
getRates() {
LNbits.api
.request('GET', `/tpos/api/v1/rate/${this.currency}`)
.then(response => {
this.exchangeRate = response.data.rate
})
.catch(e => console.error(e))
if (this.currency == 'sats') {
this.exchangeRate = 1
} else {
LNbits.api
.request('GET', `/tpos/api/v1/rate/${this.currency}`)
.then(response => {
this.exchangeRate = response.data.rate
})
.catch(e => console.error(e))
}
},
getLastPayments() {
return axios
Expand Down Expand Up @@ -1285,6 +1299,16 @@ <h5 class="q-mt-none q-mb-sm">
} else {
this.categoryFilter = category == 'All' ? '' : category
}
},
formatAmount: function (amount, currency) {
if (currency == 'sats') {
return LNbits.utils.formatSat(amount) + ' sat'
} else {
return LNbits.utils.formatCurrency(
Number(amount).toFixed(2),
currency
)
}
}
},
created: function () {
Expand All @@ -1301,10 +1325,7 @@ <h5 class="q-mt-none q-mb-sm">

this.items = JSON.parse(`{{ tpos.items | safe }}`)
this.items.forEach((item, id) => {
item.formattedPrice = LNbits.utils.formatCurrency(
item.price,
this.currency
)
item.formattedPrice = this.formatAmount(item.price, this.currency)
item.id = id
return item
})
Expand Down

0 comments on commit 30dfae3

Please sign in to comment.