diff --git a/services.py b/services.py index 1f9d826..51c4bd8 100644 --- a/services.py +++ b/services.py @@ -18,12 +18,29 @@ async def get_settings(user_id: str) -> CastleSettings: async def update_settings(user_id: str, data: CastleSettings) -> CastleSettings: + from loguru import logger + + from .fava_client import init_fava_client + settings = await get_castle_settings(user_id) if not settings: settings = await create_castle_settings(user_id, data) else: settings = await update_castle_settings(user_id, data) + # Reinitialize Fava client with new settings + try: + init_fava_client( + fava_url=settings.fava_url, + ledger_slug=settings.fava_ledger_slug, + timeout=settings.fava_timeout, + ) + logger.info( + f"Fava client reinitialized: {settings.fava_url}/{settings.fava_ledger_slug}" + ) + except Exception as e: + logger.error(f"Failed to reinitialize Fava client: {e}") + return settings diff --git a/static/js/index.js b/static/js/index.js index 3fc736e..3c8d9b7 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -31,6 +31,7 @@ window.app = Vue.createApp({ userInfo: null, // User information including equity eligibility isAdmin: false, isSuperUser: false, + settingsLoaded: false, // Flag to prevent race conditions on toolbar buttons castleWalletConfigured: false, userWalletConfigured: false, currentExchangeRate: null, // BTC/EUR rate (sats per EUR) @@ -57,6 +58,9 @@ window.app = Vue.createApp({ settingsDialog: { show: false, castleWalletId: '', + favaUrl: 'http://localhost:3333', + favaLedgerSlug: 'castle-ledger', + favaTimeout: 10.0, loading: false }, userWalletDialog: { @@ -517,6 +521,9 @@ window.app = Vue.createApp({ } catch (error) { // Settings not available this.castleWalletConfigured = false + } finally { + // Mark settings as loaded to enable toolbar buttons + this.settingsLoaded = true } }, async loadUserWallet() { @@ -534,6 +541,9 @@ window.app = Vue.createApp({ }, showSettingsDialog() { this.settingsDialog.castleWalletId = this.settings?.castle_wallet_id || '' + this.settingsDialog.favaUrl = this.settings?.fava_url || 'http://localhost:3333' + this.settingsDialog.favaLedgerSlug = this.settings?.fava_ledger_slug || 'castle-ledger' + this.settingsDialog.favaTimeout = this.settings?.fava_timeout || 10.0 this.settingsDialog.show = true }, showUserWalletDialog() { @@ -549,6 +559,14 @@ window.app = Vue.createApp({ return } + if (!this.settingsDialog.favaUrl) { + this.$q.notify({ + type: 'warning', + message: 'Fava URL is required' + }) + return + } + this.settingsDialog.loading = true try { await LNbits.api.request( @@ -556,7 +574,10 @@ window.app = Vue.createApp({ '/castle/api/v1/settings', this.g.user.wallets[0].adminkey, { - castle_wallet_id: this.settingsDialog.castleWalletId + castle_wallet_id: this.settingsDialog.castleWalletId, + fava_url: this.settingsDialog.favaUrl, + fava_ledger_slug: this.settingsDialog.favaLedgerSlug || 'castle-ledger', + fava_timeout: parseFloat(this.settingsDialog.favaTimeout) || 10.0 } ) this.$q.notify({ diff --git a/static/js/permissions.js b/static/js/permissions.js index 0de3569..4cc54f0 100644 --- a/static/js/permissions.js +++ b/static/js/permissions.js @@ -1118,5 +1118,3 @@ window.app = Vue.createApp({ } } }) - -window.app.mount('#vue') diff --git a/templates/castle/index.html b/templates/castle/index.html index 6648e6c..2a1e665 100644 --- a/templates/castle/index.html +++ b/templates/castle/index.html @@ -17,13 +17,14 @@
Track expenses, receivables, and balances for the collective