Skip to content

Commit

Permalink
Optimistically update daily claim reward
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Jul 1, 2024
1 parent 17b2232 commit 86647b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/services/datahub/content-staking/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { getMyMainAddress } from '@/stores/my-account'
import mutationWrapper from '@/subsocial-query/base'
import { allowWindowUnload, preventWindowUnload } from '@/utils/window'
import { SocialCallDataArgs, socialCallName } from '@subsocial/data-hub-sdk'
import { subscribeBalance } from '../balances/subscription'
import {
DatahubParams,
createSignedSocialDataEvent,
Expand Down Expand Up @@ -140,9 +139,16 @@ export const useClaimDailyReward = mutationWrapper(
onSuccess: () => {
const myAddress = getMyMainAddress()
if (!myAddress || !queryClient) return
subscribeBalance(queryClient, myAddress, true, () => {
if (!queryClient) return
getDailyRewardQuery.invalidate(queryClient, myAddress)
getDailyRewardQuery.setQueryData(queryClient, myAddress, (oldData) => {
if (!oldData) return oldData
const newClaims = [...oldData.claims]
const todayClaim = newClaims.find((claim) => claim.openToClaim)
if (todayClaim) todayClaim.openToClaim = false
return {
...oldData,
claims: newClaims,
claimsCount: oldData.claimsCount + 1,
}
})
},
}
Expand Down
10 changes: 9 additions & 1 deletion src/services/datahub/events/subscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { gql } from 'graphql-request'
import { useEffect, useRef } from 'react'
import { toast } from 'sonner'
import sortKeysRecursive from 'sort-keys-recursive'
import { getTodaySuperLikeCountQuery } from '../content-staking/query'
import {
getDailyRewardQuery,
getTodaySuperLikeCountQuery,
} from '../content-staking/query'
import {
ServiceMessageStatusCode,
SocialCallName,
Expand Down Expand Up @@ -217,6 +220,11 @@ async function processSubscriptionEvent(
ServiceMessageStatusCode.DailySuperLikesMaxLimitReached
) {
getTodaySuperLikeCountQuery.invalidate(client, mainAddress)
} else if (
eventData.meta.callName ===
SocialCallName.SynthGamificationClaimEntranceDailyReward
) {
getDailyRewardQuery.invalidate(client, mainAddress)
}

toast.custom((t) => (
Expand Down

0 comments on commit 86647b2

Please sign in to comment.