From b18a4d37e32dfb80aed6412c12181a0bd8801511 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Fri, 22 Jul 2022 18:01:47 +0300 Subject: [PATCH] refactor: extract wallet-config --- Makefile | 4 +- .../components/my-checkbox/my-checkbox.js | 30 ++++--- .../wallet-config/wallet-config.html | 40 +++++++++ .../components/wallet-config/wallet-config.js | 47 +++++++++++ .../extensions/watchonly/static/js/index.js | 43 +--------- .../extensions/watchonly/static/js/utils.js | 11 ++- .../watchonly/templates/watchonly/index.html | 81 ++----------------- 7 files changed, 124 insertions(+), 132 deletions(-) create mode 100644 lnbits/extensions/watchonly/static/components/wallet-config/wallet-config.html create mode 100644 lnbits/extensions/watchonly/static/components/wallet-config/wallet-config.js diff --git a/Makefile b/Makefile index 2873ae774..1e71b670f 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ format: prettier isort black check: mypy checkprettier checkblack prettier: $(shell find lnbits -name "*.js" -name ".html") - ./node_modules/.bin/prettier --write lnbits/static/js/*.js lnbits/core/static/js/*.js lnbits/extensions/*/templates/*/*.html ./lnbits/core/templates/core/*.html lnbits/templates/*.html lnbits/extensions/*/static/js/*.js + ./node_modules/.bin/prettier --write lnbits/static/js/*.js lnbits/core/static/js/*.js lnbits/extensions/*/templates/*/*.html ./lnbits/core/templates/core/*.html lnbits/templates/*.html lnbits/extensions/*/static/js/*.js lnbits/extensions/*/static/components/*/*.js black: $(shell find lnbits -name "*.py") ./venv/bin/black lnbits @@ -21,7 +21,7 @@ isort: $(shell find lnbits -name "*.py") ./venv/bin/isort --profile black lnbits checkprettier: $(shell find lnbits -name "*.js" -name ".html") - ./node_modules/.bin/prettier --check lnbits/static/js/*.js lnbits/core/static/js/*.js lnbits/extensions/*/templates/*/*.html ./lnbits/core/templates/core/*.html lnbits/templates/*.html lnbits/extensions/*/static/js/*.js + ./node_modules/.bin/prettier --check lnbits/static/js/*.js lnbits/core/static/js/*.js lnbits/extensions/*/templates/*/*.html ./lnbits/core/templates/core/*.html lnbits/templates/*.html lnbits/extensions/*/static/js/*.js lnbits/extensions/*/static/components/*/*.js checkblack: $(shell find lnbits -name "*.py") ./venv/bin/black --check lnbits diff --git a/lnbits/extensions/watchonly/static/components/my-checkbox/my-checkbox.js b/lnbits/extensions/watchonly/static/components/my-checkbox/my-checkbox.js index e24581d6b..3d22c3a05 100644 --- a/lnbits/extensions/watchonly/static/components/my-checkbox/my-checkbox.js +++ b/lnbits/extensions/watchonly/static/components/my-checkbox/my-checkbox.js @@ -1,18 +1,16 @@ async function initMyCheckbox(path) { - const t = await loadTemplateAsync(path) - Vue.component('my-checkbox', { - name:'my-checkbox', - template: t, - data() { - return { checked: false, title: 'Check me' } - }, - methods: { - check() { - this.checked = !this.checked; - console.log('### checked', this.checked) - } - } - }); + const t = await loadTemplateAsync(path) + Vue.component('my-checkbox', { + name: 'my-checkbox', + template: t, + data() { + return {checked: false, title: 'Check me'} + }, + methods: { + check() { + this.checked = !this.checked + console.log('### checked', this.checked) + } + } + }) } - - diff --git a/lnbits/extensions/watchonly/static/components/wallet-config/wallet-config.html b/lnbits/extensions/watchonly/static/components/wallet-config/wallet-config.html new file mode 100644 index 000000000..9de5aa908 --- /dev/null +++ b/lnbits/extensions/watchonly/static/components/wallet-config/wallet-config.html @@ -0,0 +1,40 @@ +
+ + +
+
+
+
{{satBtc(total)}}
+
+
+
+ + +
+
+
+ + + + + + + + + + + + + +
+ Update + Cancel +
+
+
+
+
\ No newline at end of file diff --git a/lnbits/extensions/watchonly/static/components/wallet-config/wallet-config.js b/lnbits/extensions/watchonly/static/components/wallet-config/wallet-config.js new file mode 100644 index 000000000..c822736d3 --- /dev/null +++ b/lnbits/extensions/watchonly/static/components/wallet-config/wallet-config.js @@ -0,0 +1,47 @@ +async function walletConfig(path) { + const t = await loadTemplateAsync(path) + Vue.component('wallet-config', { + name: 'wallet-config', + template: t, + + props: ['total', 'config', 'adminkey'], + data: function () { + return {} + }, + + methods: { + satBtc(val, showUnit = true) { + return satOrBtc(val, showUnit, this.config.data.sats_denominated) + }, + updateConfig: async function () { + // const wallet = this.g.user.wallets[0] + try { + await LNbits.api.request( + 'PUT', + '/watchonly/api/v1/config', + this.adminkey, + this.config.data + ) + this.config.show = false + } catch (error) { + LNbits.utils.notifyApiError(error) + } + }, + getConfig: async function () { + try { + const {data} = await LNbits.api.request( + 'GET', + '/watchonly/api/v1/config', + this.adminkey + ) + this.config.data = data + } catch (error) { + LNbits.utils.notifyApiError(error) + } + } + }, + created: async function () { + await this.getConfig() + } + }) +} diff --git a/lnbits/extensions/watchonly/static/js/index.js b/lnbits/extensions/watchonly/static/js/index.js index 4ed7fd7a4..a57d59e32 100644 --- a/lnbits/extensions/watchonly/static/js/index.js +++ b/lnbits/extensions/watchonly/static/js/index.js @@ -1,6 +1,7 @@ const watchOnly = async () => { Vue.component(VueQrcode.name, VueQrcode) - await initMyCheckbox('static/components/my-checkbox/my-checkbox.html') + + await walletConfig('static/components/wallet-config/wallet-config.html') Vue.filter('reverse', function (value) { // slice to make a copy of array, then reverse the copy @@ -10,9 +11,6 @@ const watchOnly = async () => { new Vue({ el: '#vue', mixins: [windowMixin], - mounted: function () { - console.log('### mounted') - }, data: function () { return { DUST_LIMIT: 546, @@ -81,32 +79,6 @@ const watchOnly = async () => { methods: { //################### CONFIG ################### - getConfig: async function () { - try { - const {data} = await LNbits.api.request( - 'GET', - '/watchonly/api/v1/config', - this.g.user.wallets[0].adminkey - ) - this.config.data = data - } catch (error) { - LNbits.utils.notifyApiError(error) - } - }, - updateConfig: async function () { - const wallet = this.g.user.wallets[0] - try { - await LNbits.api.request( - 'PUT', - '/watchonly/api/v1/config', - wallet.adminkey, - this.config.data - ) - this.config.show = false - } catch (error) { - LNbits.utils.notifyApiError(error) - } - }, //################### WALLETS ################### getWalletName: function (walletId) { @@ -1202,15 +1174,7 @@ const watchOnly = async () => { }, satBtc(val, showUnit = true) { - const value = this.config.data.sats_denominated - ? LNbits.utils.formatSat(val) - : val == 0 - ? 0.0 - : (val / 100000000).toFixed(8) - if (!showUnit) return value - return this.config.data.sats_denominated - ? value + ' sat' - : value + ' BTC' + return satOrBtc(val, showUnit, this.config.data.sats_denominated) }, getAccountDescription: function (accountType) { return getAccountDescription(accountType) @@ -1218,7 +1182,6 @@ const watchOnly = async () => { }, created: async function () { if (this.g.user.wallets.length) { - await this.getConfig() await this.refreshWalletAccounts() await this.refreshAddresses() await this.scanAddressWithAmount() diff --git a/lnbits/extensions/watchonly/static/js/utils.js b/lnbits/extensions/watchonly/static/js/utils.js index a4efc6076..0dbdee56d 100644 --- a/lnbits/extensions/watchonly/static/js/utils.js +++ b/lnbits/extensions/watchonly/static/js/utils.js @@ -140,10 +140,19 @@ const readFromSerialPort = serial => { return readStringUntil } +function satOrBtc(val, showUnit = true, showSats = false) { + const value = showSats + ? LNbits.utils.formatSat(val) + : val == 0 + ? 0.0 + : (val / 100000000).toFixed(8) + if (!showUnit) return value + return showSats ? value + ' sat' : value + ' BTC' +} + function loadTemplateAsync(path) { const result = new Promise(resolve => { const xhttp = new XMLHttpRequest() - console.log('### 300') xhttp.onreadystatechange = function () { if (this.readyState == 4) { diff --git a/lnbits/extensions/watchonly/templates/watchonly/index.html b/lnbits/extensions/watchonly/templates/watchonly/index.html index 91c97d443..7e3d8500f 100644 --- a/lnbits/extensions/watchonly/templates/watchonly/index.html +++ b/lnbits/extensions/watchonly/templates/watchonly/index.html @@ -2,25 +2,12 @@ %} {% block page %}
- - {% raw %} -
-
-
-
{{satBtc(utxos.total)}}
-
-
-
- - -
-
-
+ + {% raw %} @@ -31,9 +18,7 @@
-
- -
+
- - - - - - - - - - - -
- Update - Cancel -
-
-
-
@@ -1713,5 +1647,6 @@ + {% endblock %}