Fetches and sets default currency for expenses

Ensures the expense form defaults to the LNbits instance's configured currency.

This change retrieves the default currency from the LNbits API and sets it as the initial value in the expense form. If no default is configured, it falls back to the first available currency or EUR.
This commit is contained in:
padreug 2025-11-07 21:51:10 +01:00
parent 8dad92f0e5
commit f6ecbc8faf
2 changed files with 34 additions and 2 deletions

View file

@ -274,7 +274,7 @@ const form = useForm({
initialValues: {
description: '',
amount: 0,
currency: 'EUR',
currency: '', // Will be set dynamically from LNbits default currency
reference: '',
isEquity: false
}
@ -284,14 +284,24 @@ const { resetForm, meta } = form
const isFormValid = computed(() => meta.value.valid)
/**
* Fetch available currencies on component mount
* Fetch available currencies and default currency on component mount
*/
onMounted(async () => {
try {
loadingCurrencies.value = true
// Fetch available currencies
const currencies = await expensesAPI.getCurrencies()
availableCurrencies.value = currencies
console.log('[AddExpense] Loaded currencies:', currencies)
// Fetch default currency
const defaultCurrency = await expensesAPI.getDefaultCurrency()
// Set default currency: use configured default, or first available currency, or 'EUR' as final fallback
const initialCurrency = defaultCurrency || currencies[0] || 'EUR'
form.setFieldValue('currency', initialCurrency)
console.log('[AddExpense] Default currency set to:', initialCurrency)
} catch (error) {
console.error('[AddExpense] Failed to load currencies:', error)
toast.error('Failed to load currencies', { description: 'Please check your connection and try again' })

View file

@ -228,4 +228,26 @@ export class ExpensesAPI extends BaseService {
throw error
}
}
/**
* Get default currency from LNbits instance
*/
async getDefaultCurrency(): Promise<string | null> {
try {
const response = await fetch(`${this.baseUrl}/api/v1/default-currency`, {
method: 'GET',
signal: AbortSignal.timeout(this.config?.apiConfig?.timeout || 30000)
})
if (!response.ok) {
throw new Error(`Failed to fetch default currency: ${response.statusText}`)
}
const data = await response.json()
return data.default_currency
} catch (error) {
console.error('[ExpensesAPI] Error fetching default currency:', error)
throw error
}
}
}