Skip to content

Commit

Permalink
Refactor UseReferralsCount query and Types
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkidwarrior committed Oct 5, 2024
1 parent d721e33 commit 644b9fc
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/app/features/account/screen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jest.mock('app/routers/params', () => ({
}))

jest.mock('app/utils/useUserReferralsCount', () => ({
useUserReferralsCount: jest.fn().mockReturnValue({ referralsCount: 10 }),
useUserReferralsCount: jest.fn().mockReturnValue({ data: 10 }),
}))

describe('AccountScreen', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/features/account/screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function AccountScreen() {
const media = useMedia()
const toast = useToastController()
const { profile } = useUser()
const { referralsCount } = useUserReferralsCount()
const { data: referralsCount } = useUserReferralsCount()

const name = profile?.name
const send_id = profile?.send_id
Expand Down
2 changes: 1 addition & 1 deletion packages/app/features/home/screen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jest.mock('solito', () => {
})

jest.mock('app/utils/useUserReferralsCount', () => ({
useUserReferralsCount: jest.fn().mockReturnValue(123),
useUserReferralsCount: jest.fn().mockReturnValue({ data: 123 }),
}))

jest.mock('app/utils/useSendAccountBalances', () => ({
Expand Down
2 changes: 1 addition & 1 deletion packages/app/features/referrals/screen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { render, act, screen } from '@testing-library/react-native'
import { ReferralsScreen } from './screen'

jest.mock('app/utils/useUserReferralsCount', () => ({
useUserReferralsCount: jest.fn().mockReturnValue({ referralsCount: 123, error: null }),
useUserReferralsCount: jest.fn().mockReturnValue({ data: 123, error: null }),
}))

test('ReferralScreen', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/features/referrals/screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const RewardsHistoryHeader = () => (
)

const StatsCards = () => {
const { referralsCount, error: referralsCountError } = useUserReferralsCount()
const { data: referralsCount, error: referralsCountError } = useUserReferralsCount()

return (
<XStack flexWrap="wrap" ai="flex-start" jc="space-between" gap="$8" mb="$4">
Expand Down
29 changes: 8 additions & 21 deletions packages/app/utils/useUserReferralsCount.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { useQuery } from '@tanstack/react-query'
import type { Functions } from '@my/supabase/database.types'
import type { PostgrestError } from '@supabase/postgrest-js'
import { useQuery, type UseQueryResult } from '@tanstack/react-query'
import { useSupabase } from 'app/utils/supabase/useSupabase'

export type UserReferralsCount = {
referralsCount: number | undefined
error: Error | null
isLoading: boolean
refetch: () => void
}
export const useUserReferralsCount = () => {
export const useUserReferralsCount = (): UseQueryResult<
Functions<'user_referrals_count'>,
PostgrestError
> => {
const supabase = useSupabase()
const {
data: referralsCount,
isLoading,
refetch,
error,
} = useQuery({
return useQuery({
queryKey: ['user_referrals_count'],
queryFn: async () => {
const { data, error } = await supabase.rpc('user_referrals_count').select('*')
Expand All @@ -28,11 +22,4 @@ export const useUserReferralsCount = () => {
return data
},
})

return {
referralsCount,
isLoading,
error,
refetch,
} as UserReferralsCount
}

0 comments on commit 644b9fc

Please sign in to comment.