Fetch available currencies from LNbits core API
- Call /api/v1/currencies endpoint on LNbits core (not nostrmarket extension) - Ensure 'sat' is always first in the currency list and used as default - Add loading state for currency dropdown - Add key prop to Select component for proper reactivity 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
2ac584b1d6
commit
b7db9bba5a
2 changed files with 45 additions and 19 deletions
|
|
@ -47,10 +47,14 @@
|
||||||
<FormField v-slot="{ componentField }" name="currency">
|
<FormField v-slot="{ componentField }" name="currency">
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Currency *</FormLabel>
|
<FormLabel>Currency *</FormLabel>
|
||||||
<Select :disabled="isCreating" v-bind="componentField">
|
<Select
|
||||||
|
:key="`currency-select-${availableCurrencies.length}`"
|
||||||
|
:disabled="isCreating || isLoadingCurrencies"
|
||||||
|
v-bind="componentField"
|
||||||
|
>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<SelectTrigger class="w-full">
|
<SelectTrigger class="w-full">
|
||||||
<SelectValue placeholder="Select currency" />
|
<SelectValue :placeholder="isLoadingCurrencies ? 'Loading...' : 'Select currency'" />
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
|
|
@ -275,6 +279,7 @@ const toast = useToast()
|
||||||
// Local state
|
// Local state
|
||||||
const isCreating = ref(false)
|
const isCreating = ref(false)
|
||||||
const createError = ref<string | null>(null)
|
const createError = ref<string | null>(null)
|
||||||
|
const isLoadingCurrencies = ref(false)
|
||||||
const availableCurrencies = ref<string[]>(['sat'])
|
const availableCurrencies = ref<string[]>(['sat'])
|
||||||
const availableZones = ref<Zone[]>([])
|
const availableZones = ref<Zone[]>([])
|
||||||
const showNewZoneForm = ref(false)
|
const showNewZoneForm = ref(false)
|
||||||
|
|
@ -331,11 +336,27 @@ const onSubmit = form.handleSubmit(async (values) => {
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
const loadAvailableCurrencies = async () => {
|
const loadAvailableCurrencies = async () => {
|
||||||
|
isLoadingCurrencies.value = true
|
||||||
try {
|
try {
|
||||||
const currencies = await nostrmarketAPI.getCurrencies()
|
const currencies = await nostrmarketAPI.getCurrencies()
|
||||||
availableCurrencies.value = currencies
|
if (currencies.length > 0) {
|
||||||
|
// Ensure 'sat' is always first in the list
|
||||||
|
const satIndex = currencies.indexOf('sat')
|
||||||
|
if (satIndex === -1) {
|
||||||
|
// Add 'sat' at the beginning if not present
|
||||||
|
availableCurrencies.value = ['sat', ...currencies]
|
||||||
|
} else if (satIndex > 0) {
|
||||||
|
// Move 'sat' to the beginning if present but not first
|
||||||
|
const withoutSat = currencies.filter(c => c !== 'sat')
|
||||||
|
availableCurrencies.value = ['sat', ...withoutSat]
|
||||||
|
} else {
|
||||||
|
availableCurrencies.value = currencies
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to load currencies:', error)
|
console.error('Failed to load currencies:', error)
|
||||||
|
} finally {
|
||||||
|
isLoadingCurrencies.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -371,30 +371,35 @@ export class NostrmarketAPI extends BaseService {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get available currencies
|
* Get available currencies from the LNbits core API
|
||||||
|
* This endpoint returns currencies allowed by the server configuration
|
||||||
*/
|
*/
|
||||||
async getCurrencies(): Promise<string[]> {
|
async getCurrencies(): Promise<string[]> {
|
||||||
const baseCurrencies = ['sat']
|
// Call the LNbits core API directly (not under /nostrmarket)
|
||||||
|
const url = `${this.baseUrl}/api/v1/currencies`
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const apiCurrencies = await this.request<string[]>(
|
const response = await fetch(url, {
|
||||||
'/api/v1/currencies',
|
method: 'GET',
|
||||||
'', // No authentication needed for currencies endpoint
|
headers: { 'Content-Type': 'application/json' }
|
||||||
{ method: 'GET' }
|
})
|
||||||
)
|
|
||||||
|
|
||||||
if (apiCurrencies && Array.isArray(apiCurrencies)) {
|
if (!response.ok) {
|
||||||
// Combine base currencies with API currencies, removing duplicates
|
throw new Error(`Failed to fetch currencies: ${response.status}`)
|
||||||
const allCurrencies = [...baseCurrencies, ...apiCurrencies.filter(currency => !baseCurrencies.includes(currency))]
|
|
||||||
this.debug('Retrieved currencies:', { count: allCurrencies.length, currencies: allCurrencies })
|
|
||||||
return allCurrencies
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.debug('No API currencies returned, using base currencies only')
|
const apiCurrencies = await response.json()
|
||||||
return baseCurrencies
|
|
||||||
|
if (apiCurrencies && Array.isArray(apiCurrencies)) {
|
||||||
|
this.debug('Retrieved currencies from LNbits core:', { count: apiCurrencies.length, currencies: apiCurrencies })
|
||||||
|
return apiCurrencies
|
||||||
|
}
|
||||||
|
|
||||||
|
this.debug('No currencies returned from server, using default')
|
||||||
|
return ['sat']
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.debug('Failed to get currencies, falling back to base currencies:', error)
|
this.debug('Failed to get currencies, using default:', error)
|
||||||
return baseCurrencies
|
return ['sat']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue