Skip to content

Commit 9d603a1

Browse files
committed
fix: #774 update cards in parallel with proper error handling
1 parent a957ccd commit 9d603a1

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

frontend/src/services/collection/collectionService.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,6 @@ export const updateCards = async (email: string, rowsToUpdate: CardAmountUpdate[
6969
const now = new Date()
7070
const nowString = now.toISOString()
7171

72-
const { data: account, error: accountError } = await supabase
73-
.from('accounts')
74-
.update({ collection_last_updated: now })
75-
.eq('email', email)
76-
.select('*, trade_rarity_settings:trade_rarity_settings!email(*)')
77-
.single()
78-
79-
if (accountError) {
80-
throw new Error(`Error fetching account: ${accountError.message}`)
81-
}
82-
8372
// Update collection records
8473
const collectionRows: CollectionRowUpdate[] = rowsToUpdate.map((row) => ({
8574
email,
@@ -97,15 +86,36 @@ export const updateCards = async (email: string, rowsToUpdate: CardAmountUpdate[
9786
//deduplicate amountRows on internal_id, needed for card csv import feature
9887
.filter((row, index, self) => index === self.findIndex((r) => r.internal_id === row.internal_id))
9988

100-
//then update the card amounts
101-
const { error: cardAmountsError } = await supabase.from('card_amounts').upsert(amountRows)
102-
if (cardAmountsError) {
103-
throw new Error(`Error bulk updating collection: ${cardAmountsError?.message}`)
104-
}
89+
// Execute all three database calls in parallel
90+
let account: AccountRow
91+
try {
92+
const [accountResult, cardAmountsResult, collectionResult] = await Promise.all([
93+
supabase
94+
.from('accounts')
95+
.update({ collection_last_updated: now })
96+
.eq('email', email)
97+
.select('*, trade_rarity_settings:trade_rarity_settings!email(*)')
98+
.single(),
99+
supabase.from('card_amounts').upsert(amountRows),
100+
supabase.from('collection').upsert(collectionRows),
101+
])
102+
103+
if (accountResult.error) {
104+
throw new Error(`Error fetching account: ${accountResult.error.message}`)
105+
}
106+
if (cardAmountsResult.error) {
107+
throw new Error(`Error bulk updating card amounts: ${cardAmountsResult.error.message}`)
108+
}
109+
if (collectionResult.error) {
110+
throw new Error(`Error bulk updating collection: ${collectionResult.error.message}`)
111+
}
105112

106-
const { error: collectionError } = await supabase.from('collection').upsert(collectionRows)
107-
if (collectionError) {
108-
throw new Error(`Error bulk updating collection: ${collectionError?.message}`)
113+
account = accountResult.data as AccountRow
114+
} catch (error) {
115+
// Invalidate local cache by removing records from localStorage
116+
localStorage.removeItem(`${COLLECTION_CACHE_KEY}_${email}`)
117+
localStorage.removeItem(`${COLLECTION_TIMESTAMP_KEY}_${email}`)
118+
throw error
109119
}
110120

111121
// Update cache with the changes

frontend/src/services/collection/useCollection.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
22
import { useContext } from 'react'
33
import { DialogContext } from '@/context/DialogContext.ts'
4+
import { useToast } from '@/hooks/use-toast.ts'
45
import { useAccount } from '@/services/account/useAccount.ts'
56
import { useUser } from '@/services/auth/useAuth.ts'
67
import { deleteCard, getCollection, getPublicCollection, updateCards } from '@/services/collection/collectionService.ts'
@@ -30,6 +31,7 @@ export function usePublicCollection(friendId: string | undefined) {
3031
}
3132

3233
export function useUpdateCards() {
34+
const { toast } = useToast()
3335
const { data: user } = useUser()
3436
const email = user?.user.email
3537

@@ -47,6 +49,11 @@ export function useUpdateCards() {
4749
// Update account data in cache (for collection_last_updated timestamp)
4850
queryClient.setQueryData(['account', email], result.account)
4951
},
52+
onError: async (error) => {
53+
toast({ title: 'Error updating cards', description: error.message, variant: 'destructive' })
54+
await queryClient.invalidateQueries({ queryKey: ['collection'] })
55+
await queryClient.invalidateQueries({ queryKey: ['account'] })
56+
},
5057
})
5158
}
5259

0 commit comments

Comments
 (0)