Skip to content

Commit e972340

Browse files
[API Node] Fix credits fetch condition (#3575)
1 parent a01aa39 commit e972340

File tree

3 files changed

+57
-36
lines changed

3 files changed

+57
-36
lines changed

src/components/dialog/content/SettingDialogContent.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import { computed, defineAsyncComponent, watch } from 'vue'
8686
import SearchBox from '@/components/common/SearchBox.vue'
8787
import { useSettingSearch } from '@/composables/setting/useSettingSearch'
8888
import { useSettingUI } from '@/composables/setting/useSettingUI'
89+
import { useFirebaseAuthStore } from '@/stores/firebaseAuthStore'
8990
import { SettingTreeNode } from '@/stores/settingStore'
9091
import { ISettingGroup, SettingParams } from '@/types/settingTypes'
9192
import { flattenTree } from '@/utils/treeUtil'
@@ -135,6 +136,8 @@ const {
135136
getSearchResults
136137
} = useSettingSearch()
137138
139+
const authStore = useFirebaseAuthStore()
140+
138141
// Sort groups for a category
139142
const sortedGroups = (category: SettingTreeNode): ISettingGroup[] => {
140143
return [...(category.children ?? [])]
@@ -165,6 +168,9 @@ watch(activeCategory, (_, oldValue) => {
165168
if (!tabValue.value) {
166169
activeCategory.value = oldValue
167170
}
171+
if (activeCategory.value?.key === 'credits') {
172+
void authStore.fetchBalance()
173+
}
168174
})
169175
</script>
170176

src/components/dialog/content/setting/CreditsPanel.vue

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
{{ $t('credits.yourCreditBalance') }}
1313
</h3>
1414
<div class="flex justify-between items-center">
15-
<div class="flex items-center gap-1">
15+
<div v-if="balanceLoading" class="flex items-center gap-1">
16+
<div class="flex items-center gap-2">
17+
<Skeleton shape="circle" width="1.5rem" height="1.5rem" />
18+
</div>
19+
<div class="flex-1"></div>
20+
<Skeleton width="8rem" height="2rem" />
21+
</div>
22+
<div v-else class="flex items-center gap-1">
1623
<Tag
1724
severity="secondary"
1825
icon="pi pi-dollar"
@@ -21,11 +28,7 @@
2128
/>
2229
<div class="text-3xl font-bold">{{ formattedBalance }}</div>
2330
</div>
24-
<ProgressSpinner
25-
v-if="loading"
26-
class="w-12 h-12"
27-
style="--pc-spinner-color: #000"
28-
/>
31+
<Skeleton v-if="loading" width="2rem" height="2rem" />
2932
<Button
3033
v-else
3134
:label="$t('credits.purchaseCredits')"
@@ -34,7 +37,13 @@
3437
/>
3538
</div>
3639
<div class="flex flex-row items-center">
37-
<div v-if="formattedLastUpdateTime" class="text-xs text-muted">
40+
<Skeleton
41+
v-if="balanceLoading"
42+
width="12rem"
43+
height="1rem"
44+
class="text-xs"
45+
/>
46+
<div v-else-if="formattedLastUpdateTime" class="text-xs text-muted">
3847
{{ $t('credits.lastUpdated') }}: {{ formattedLastUpdateTime }}
3948
</div>
4049
<Button
@@ -112,10 +121,10 @@ import Button from 'primevue/button'
112121
import Column from 'primevue/column'
113122
import DataTable from 'primevue/datatable'
114123
import Divider from 'primevue/divider'
115-
import ProgressSpinner from 'primevue/progressspinner'
124+
import Skeleton from 'primevue/skeleton'
116125
import TabPanel from 'primevue/tabpanel'
117126
import Tag from 'primevue/tag'
118-
import { computed, onMounted, ref } from 'vue'
127+
import { computed, ref } from 'vue'
119128
import { useI18n } from 'vue-i18n'
120129
121130
import { useDialogService } from '@/services/dialogService'
@@ -133,7 +142,7 @@ const { t } = useI18n()
133142
const dialogService = useDialogService()
134143
const authStore = useFirebaseAuthStore()
135144
const loading = computed(() => authStore.loading)
136-
145+
const balanceLoading = computed(() => authStore.isFetchingBalance)
137146
const formattedBalance = computed(() => {
138147
if (!authStore.balance) return '0.00'
139148
return formatMetronomeCurrency(authStore.balance.amount_micros, 'usd')
@@ -174,10 +183,5 @@ const handleFaqClick = () => {
174183
window.open('https://drip-art.notion.site/api-nodes-faqs', '_blank')
175184
}
176185
177-
// Fetch initial balance when panel is mounted
178-
onMounted(() => {
179-
void authStore.fetchBalance()
180-
})
181-
182186
const creditHistory = ref<CreditHistoryItemData[]>([])
183187
</script>

src/stores/firebaseAuthStore.ts

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
4545
const currentUser = ref<User | null>(null)
4646
const isInitialized = ref(false)
4747
const customerCreated = ref(false)
48+
const isFetchingBalance = ref(false)
4849

4950
// Balance state
5051
const balance = ref<GetCustomerBalanceResponse | null>(null)
@@ -95,33 +96,42 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
9596
}
9697

9798
const fetchBalance = async (): Promise<GetCustomerBalanceResponse | null> => {
98-
const token = await getIdToken()
99-
if (!token) {
100-
error.value = 'Cannot fetch balance: User not authenticated'
101-
return null
102-
}
103-
104-
const response = await fetch(`${API_BASE_URL}/customers/balance`, {
105-
headers: {
106-
Authorization: `Bearer ${token}`
99+
isFetchingBalance.value = true
100+
try {
101+
const token = await getIdToken()
102+
if (!token) {
103+
error.value = 'Cannot fetch balance: User not authenticated'
104+
isFetchingBalance.value = false
105+
return null
107106
}
108-
})
109107

110-
if (!response.ok) {
111-
if (response.status === 404) {
112-
// Customer not found is expected for new users
108+
const response = await fetch(`${API_BASE_URL}/customers/balance`, {
109+
headers: {
110+
Authorization: `Bearer ${token}`
111+
}
112+
})
113+
114+
if (!response.ok) {
115+
if (response.status === 404) {
116+
// Customer not found is expected for new users
117+
return null
118+
}
119+
const errorData = await response.json()
120+
error.value = `Failed to fetch balance: ${errorData.message}`
113121
return null
114122
}
115-
const errorData = await response.json()
116-
error.value = `Failed to fetch balance: ${errorData.message}`
123+
124+
const balanceData = await response.json()
125+
// Update the last balance update time
126+
lastBalanceUpdateTime.value = new Date()
127+
balance.value = balanceData
128+
return balanceData
129+
} catch (e) {
130+
error.value = `Failed to fetch balance: ${e}`
117131
return null
132+
} finally {
133+
isFetchingBalance.value = false
118134
}
119-
120-
const balanceData = await response.json()
121-
// Update the last balance update time
122-
lastBalanceUpdateTime.value = new Date()
123-
balance.value = balanceData
124-
return balanceData
125135
}
126136

127137
const createCustomer = async (
@@ -303,6 +313,7 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
303313
isInitialized,
304314
balance,
305315
lastBalanceUpdateTime,
316+
isFetchingBalance,
306317

307318
// Getters
308319
isAuthenticated,

0 commit comments

Comments
 (0)