Refactor authentication and async operation handling in useAuth composable
- Introduce useMultiAsyncOperation to manage multiple async operations in useAuth, enhancing error handling and loading state management. - Replace manual loading and error state management with standardized async operation patterns for initialize, login, register, and logout functions. - Update related components to utilize the new async operation structure, improving code clarity and maintainability. - Add useAsyncOperation to other composables (useChat, useTicketPurchase, useMarket) for consistent async handling across the application.
This commit is contained in:
parent
e0443742c5
commit
7c439361b7
5 changed files with 298 additions and 133 deletions
|
|
@ -2,13 +2,15 @@ import { ref, computed, onUnmounted } from 'vue'
|
|||
import { purchaseTicket, checkPaymentStatus, payInvoiceWithWallet } from '@/lib/api/events'
|
||||
import { useAuth } from '@/composables/useAuth'
|
||||
import { toast } from 'vue-sonner'
|
||||
import { useAsyncOperation } from '@/core/composables/useAsyncOperation'
|
||||
|
||||
export function useTicketPurchase() {
|
||||
const { isAuthenticated, currentUser } = useAuth()
|
||||
|
||||
// Async operations
|
||||
const purchaseOperation = useAsyncOperation()
|
||||
|
||||
// State
|
||||
const isLoading = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
const paymentHash = ref<string | null>(null)
|
||||
const paymentRequest = ref<string | null>(null)
|
||||
const qrCode = ref<string | null>(null)
|
||||
|
|
@ -50,7 +52,7 @@ export function useTicketPurchase() {
|
|||
qrCode.value = dataUrl
|
||||
} catch (err) {
|
||||
console.error('Error generating QR code:', err)
|
||||
error.value = 'Failed to generate QR code'
|
||||
// Note: error handling is now managed by the purchaseOperation
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -98,16 +100,15 @@ export function useTicketPurchase() {
|
|||
throw new Error('User must be authenticated to purchase tickets')
|
||||
}
|
||||
|
||||
isLoading.value = true
|
||||
error.value = null
|
||||
paymentHash.value = null
|
||||
paymentRequest.value = null
|
||||
qrCode.value = null
|
||||
ticketQRCode.value = null
|
||||
purchasedTicketId.value = null
|
||||
showTicketQR.value = false
|
||||
return await purchaseOperation.execute(async () => {
|
||||
// Clear previous state
|
||||
paymentHash.value = null
|
||||
paymentRequest.value = null
|
||||
qrCode.value = null
|
||||
ticketQRCode.value = null
|
||||
purchasedTicketId.value = null
|
||||
showTicketQR.value = false
|
||||
|
||||
try {
|
||||
// Get the invoice
|
||||
const invoice = await purchaseTicket(eventId)
|
||||
paymentHash.value = invoice.payment_hash
|
||||
|
|
@ -133,12 +134,11 @@ export function useTicketPurchase() {
|
|||
// No wallet balance, proceed with manual payment
|
||||
await startPaymentStatusCheck(eventId, invoice.payment_hash)
|
||||
}
|
||||
} catch (err) {
|
||||
error.value = err instanceof Error ? err.message : 'Failed to purchase ticket'
|
||||
console.error('Error purchasing ticket:', err)
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
|
||||
return invoice
|
||||
}, {
|
||||
errorMessage: 'Failed to purchase ticket'
|
||||
})
|
||||
}
|
||||
|
||||
// Start payment status check
|
||||
|
|
@ -184,8 +184,7 @@ export function useTicketPurchase() {
|
|||
|
||||
// Reset payment state
|
||||
function resetPaymentState() {
|
||||
isLoading.value = false
|
||||
error.value = null
|
||||
purchaseOperation.clear()
|
||||
paymentHash.value = null
|
||||
paymentRequest.value = null
|
||||
qrCode.value = null
|
||||
|
|
@ -215,8 +214,8 @@ export function useTicketPurchase() {
|
|||
|
||||
return {
|
||||
// State
|
||||
isLoading,
|
||||
error,
|
||||
isLoading: purchaseOperation.isLoading,
|
||||
error: purchaseOperation.error,
|
||||
paymentHash,
|
||||
paymentRequest,
|
||||
qrCode,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue