From 3be323bf98ddfe5de6bd2cdf0f09fa80168facf6 Mon Sep 17 00:00:00 2001 From: Nouman Tahir Date: Tue, 12 Sep 2023 16:37:54 +0500 Subject: [PATCH] unvote cache support --- .../upvotePopover/container/upvotePopover.tsx | 4 ++-- src/utils/postParser.tsx | 20 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/components/upvotePopover/container/upvotePopover.tsx b/src/components/upvotePopover/container/upvotePopover.tsx index 231c330fb..9a0a97455 100644 --- a/src/components/upvotePopover/container/upvotePopover.tsx +++ b/src/components/upvotePopover/container/upvotePopover.tsx @@ -204,7 +204,7 @@ const UpvotePopover = forwardRef(({ }: Props, ref) => { return; } setIsVoted(!!sliderValue); - _updateVoteCache(_author, _permlink, amount, false, !!sliderValue ? CacheStatus.DELETED : CacheStatus.PUBLISHED); + _updateVoteCache(_author, _permlink, amount, false, !!sliderValue ? CacheStatus.PUBLISHED : CacheStatus.DELETED); }) .catch((err) => { _updateVoteCache(_author, _permlink, amount, false, CacheStatus.FAILED); @@ -267,7 +267,7 @@ const UpvotePopover = forwardRef(({ }: Props, ref) => { transactionId: response.id, }); setIsVoted(!!sliderValue); - _updateVoteCache(_author, _permlink, amount, true, !!sliderValue ? CacheStatus.DELETED : CacheStatus.PUBLISHED); + _updateVoteCache(_author, _permlink, amount, true, !!sliderValue ? CacheStatus.PUBLISHED : CacheStatus.DELETED); }) .catch((err) => { dispatch( diff --git a/src/utils/postParser.tsx b/src/utils/postParser.tsx index c438d6876..cdeebacc4 100644 --- a/src/utils/postParser.tsx +++ b/src/utils/postParser.tsx @@ -318,12 +318,12 @@ export const injectPostCache = (commentsMap, cachedComments, cachedVotes, lastCa export const injectVoteCache = (post, voteCache) => { - if ( - voteCache && - (voteCache.status !== CacheStatus.FAILED || voteCache.status !== CacheStatus.DELETED) - ) { + if (voteCache && voteCache.status !== CacheStatus.FAILED) { + const _voteIndex = post.active_votes.findIndex((i) => i.voter === voteCache.voter); - if (_voteIndex < 0) { + + //if vote do not already exist + if (_voteIndex < 0 && voteCache.status !== CacheStatus.DELETED ) { post.total_payout += voteCache.amount * (voteCache.isDownvote ? -1 : 1); post.active_votes = [ ...post.active_votes, @@ -332,8 +332,14 @@ export const injectVoteCache = (post, voteCache) => { rshares: voteCache.isDownvote ? -1000 : 1000, }, ]; - } else { - post.active_votes[_voteIndex].rshares = voteCache.isDownvote ? -1000 : 1000; + } + + //if vote already exist + else { + post.active_votes[_voteIndex].rshares = voteCache.status === CacheStatus.DELETED + ? 0 + : voteCache.isDownvote + ? -1000 : 1000; post.active_votes = [...post.active_votes]; } }