Skip to content

Commit

Permalink
Use functions instead of views in affiliate hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkidwarrior committed Nov 23, 2024
1 parent 928d7f2 commit 191be15
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions apps/next/pages/account/affiliate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const Page: NextPageWithLayout = () => {
<title>Send | Affiliates</title>
<meta
name="description"
content="View your network, track your score, and see your referrals activity."
content="View your network and track referral activity."
key="desc"
/>
</Head>
Expand All @@ -21,7 +21,7 @@ export const Page: NextPageWithLayout = () => {
)
}

const subheader = 'View your network, track your score, and see your referrals activity.'
const subheader = 'View your network and track referral activity.'

export const getServerSideProps = userProtectedGetSSP()

Expand Down
5 changes: 1 addition & 4 deletions packages/app/features/affiliate/screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import {
} from '@my/ui'

import { useAffiliateReferrals } from './utils/useAffiliateReferrals'

import { Fragment } from 'react'

import formatAmount from 'app/utils/formatAmount'
import { useAffiliateStats } from './utils/useAffiliateStats'

export const AffiliateScreen = () => {
Expand Down Expand Up @@ -136,7 +133,7 @@ const ReferralsList = () => {
</Paragraph>
)}
{pages?.map((referrals) => {
return referrals?.map(({ referral }) => {
return referrals?.map((referral) => {
if (!referral) return null
return (
<Fragment key={`${referral.referred_id}-${referral.tag}`}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function useAffiliateReferrals({ pageSize = 10 }: { pageSize?: number } =
async function fetchAffiliateReferrals({ pageParam }: { pageParam: number }) {
const from = pageParam * pageSize
const to = (pageParam + 1) * pageSize - 1
const request = supabase.from('affiliate_referrals').select('*').range(from, to)
const request = supabase.rpc('get_affiliate_referrals').select('*').range(from, to)
const { data, error } = await request
throwIf(error)
return data
Expand Down
7 changes: 5 additions & 2 deletions packages/app/features/affiliate/utils/useAffiliateStats.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { Functions } from '@my/supabase/database.types'
import { useQuery } from '@tanstack/react-query'
import { useSupabase } from 'app/utils/supabase/useSupabase'

export function useAffiliateStats() {
const supabase = useSupabase()
async function fetchAffiliateStats() {
const request = supabase.from('affiliate_stats_summary').select('*').single()
async function fetchAffiliateStats(): Promise<
Functions<'get_affiliate_stats_summary'>[number] | null
> {
const request = supabase.rpc('get_affiliate_stats_summary').select('*').single()

const { data, error } = await request
if (error) {
Expand Down
22 changes: 22 additions & 0 deletions supabase/database-generated.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,28 @@ export type Database = {
Args: Record<PropertyKey, never>
Returns: string
}
get_affiliate_referrals: {
Args: Record<PropertyKey, never>
Returns: {
referred_id: string
send_plus_minus: number
avatar_url: string
tag: string
created_at: string
}[]
}
get_affiliate_stats_summary: {
Args: Record<PropertyKey, never>
Returns: {
id: string
created_at: string
user_id: string
send_plus_minus: number
referral_count: number
network_plus_minus: number
affiliate_send_score: number
}[]
}
insert_challenge: {
Args: Record<PropertyKey, never>
Returns: {
Expand Down

0 comments on commit 191be15

Please sign in to comment.