Skip to content

Commit

Permalink
Merge pull request #213 from dappforce/fix/reward
Browse files Browse the repository at this point in the history
Fix query cache issue
  • Loading branch information
teodorus-nathaniel authored Jan 30, 2024
2 parents 6ab3052 + 622dacb commit 0140180
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/components/activity/AccountActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
useGetReactionActivities,
useGetTweetActivities,
} from 'src/graphql/hooks'
import { useIsMyAddress } from '../auth/MyAccountsContext'
import WriteSomething from '../posts/WriteSomething'
import { Loading } from '../utils'
import { createLoadMorePosts, FeedActivities } from './FeedActivities'
import { createLoadMoreActivities, NotifActivities } from './Notifications'
Expand Down Expand Up @@ -115,9 +117,10 @@ const activityTabs = [
'spaces',
'all',
] as const
type ActivityTab = typeof activityTabs[number]
type ActivityTab = (typeof activityTabs)[number]
const getTab = (tab: ActivityTab) => tab
const OffchainAccountActivity = ({ address }: ActivitiesByAddressProps) => {
const isMyAddress = useIsMyAddress(address)
const getActivityCounts = useGetActivityCounts()
const router = useRouter()
const [activeTab, setActiveTab] = useState<ActivityTab>('posts')
Expand Down Expand Up @@ -172,7 +175,14 @@ const OffchainAccountActivity = ({ address }: ActivitiesByAddressProps) => {
return (
<Tabs activeKey={activeTab} onChange={onChangeTab}>
<TabPane tab={getTabTitle('Posts', postsCount)} key={getTab('posts')}>
<PostActivities address={address} totalCount={postsCount} />
{isMyAddress ? (
<div className='d-flex flex-column mt-3'>
<WriteSomething />
<PostActivities address={address} totalCount={postsCount} />
</div>
) : (
<PostActivities address={address} totalCount={postsCount} />
)}
</TabPane>
{(tweetsCount > 0 || haveDisplayedTweetsTab) && (
<TabPane tab={getTabTitle('Tweets', tweetsCount)} key={getTab('tweets')}>
Expand Down
7 changes: 5 additions & 2 deletions src/components/utils/datahub/active-staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { PostRewards } from 'src/rtk/features/activeStaking/postRewardSlice'
import { RewardHistory } from 'src/rtk/features/activeStaking/rewardHistorySlice'
import { fetchRewardReport, RewardReport } from 'src/rtk/features/activeStaking/rewardReportSlice'
import {
fetchSuperLikeCounts,
invalidateSuperLikeCounts,
SuperLikeCount,
} from 'src/rtk/features/activeStaking/superLikeCountsSlice'
import {
Expand Down Expand Up @@ -49,6 +49,7 @@ export async function getSuperLikeCounts(postIds: string[]): Promise<SuperLikeCo
>({
query: GET_SUPER_LIKE_COUNTS,
variables: { postIds },
fetchPolicy: 'network-only',
})

const resultMap = new Map<string, SuperLikeCount>()
Expand Down Expand Up @@ -124,6 +125,7 @@ export async function getAddressLikeCountToPosts(
>({
query: GET_ADDRESS_LIKE_COUNT_TO_POSTS,
variables: { postIds, address },
fetchPolicy: 'network-only',
})

const resultMap = new Map<string, AddressLikeCount>()
Expand Down Expand Up @@ -212,6 +214,7 @@ export async function getRewardReport(address: string): Promise<RewardReport> {
>({
query: GET_REWARD_REPORT,
variables: { address, ...getDayAndWeekTimestamp() },
fetchPolicy: 'network-only',
})
const weekReward = res.data.activeStakingRewardsByWeek?.[0]

Expand Down Expand Up @@ -392,7 +395,7 @@ async function processSubscriptionEvent(
const dispatch = getStoreDispatcher()
if (!dispatch) throw new Error('Dispatcher not exist')

dispatch(fetchSuperLikeCounts({ postIds: [post.persistentId], reload: true }))
dispatch(invalidateSuperLikeCounts({ postId: post.persistentId }))
if (staker.id === myAddress) {
dispatch(fetchRewardReport({ address: myAddress, reload: true }))
dispatch(
Expand Down
4 changes: 4 additions & 0 deletions src/components/utils/datahub/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import ws from 'isomorphic-ws'
import { datahubQueryUrl, datahubSubscriptionUrl } from 'src/config/env'
import { createApolloClient } from 'src/graphql/client'
import { wait } from 'src/utils/promise'
import { isServerSide } from '..'

dayjs.extend(utc)
dayjs.extend(isoWeek)
Expand Down Expand Up @@ -53,6 +54,9 @@ export function datahubQueryRequest<T, TVariables extends OperationVariables>(
config: QueryOptions<TVariables, T>,
) {
const client = getApolloClient()
if (isServerSide()) {
config.fetchPolicy = 'no-cache'
}
return client.query(config)
}

Expand Down
13 changes: 12 additions & 1 deletion src/rtk/features/activeStaking/superLikeCountsSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'
import { createAsyncThunk, createEntityAdapter, createSlice } from '@reduxjs/toolkit'
import { getSuperLikeCounts } from 'src/components/utils/datahub/active-staking'
import { ThunkApiConfig } from 'src/rtk/app/helpers'
import { RootState } from 'src/rtk/app/rootReducer'
import { createSimpleManyFetchWrapper } from 'src/rtk/app/wrappers'

Expand Down Expand Up @@ -35,6 +36,16 @@ export const fetchSuperLikeCounts = createSimpleManyFetchWrapper<
},
})

export const invalidateSuperLikeCounts = createAsyncThunk<void, { postId: string }, ThunkApiConfig>(
`${sliceName}/invalidate`,
async ({ postId }, { getState, dispatch }) => {
const state = selectPostSuperLikeCount(getState(), postId)
if (!state) return

await dispatch(fetchSuperLikeCounts({ postIds: [postId], reload: true }))
},
)

const slice = createSlice({
name: sliceName,
initialState: adapter.getInitialState(),
Expand Down

0 comments on commit 0140180

Please sign in to comment.