From 433e7e40e14ddc8282aa6a586be313305dfb9591 Mon Sep 17 00:00:00 2001 From: teodorus-nathaniel Date: Tue, 30 Jan 2024 00:51:22 +0700 Subject: [PATCH] refactor: only refetch super like count if its already fetched before --- src/components/utils/datahub/active-staking.ts | 4 ++-- .../features/activeStaking/superLikeCountsSlice.ts | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/components/utils/datahub/active-staking.ts b/src/components/utils/datahub/active-staking.ts index 0a9f1c2b7..b5a7a1b3c 100644 --- a/src/components/utils/datahub/active-staking.ts +++ b/src/components/utils/datahub/active-staking.ts @@ -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 { @@ -395,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( diff --git a/src/rtk/features/activeStaking/superLikeCountsSlice.ts b/src/rtk/features/activeStaking/superLikeCountsSlice.ts index c0c2d8910..ce1cd58e6 100644 --- a/src/rtk/features/activeStaking/superLikeCountsSlice.ts +++ b/src/rtk/features/activeStaking/superLikeCountsSlice.ts @@ -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' @@ -35,6 +36,16 @@ export const fetchSuperLikeCounts = createSimpleManyFetchWrapper< }, }) +export const invalidateSuperLikeCounts = createAsyncThunk( + `${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(),