unvote cache support

This commit is contained in:
Nouman Tahir 2023-09-12 16:37:54 +05:00
parent be96429431
commit 3be323bf98
2 changed files with 15 additions and 9 deletions

View File

@ -204,7 +204,7 @@ const UpvotePopover = forwardRef(({ }: Props, ref) => {
return; return;
} }
setIsVoted(!!sliderValue); setIsVoted(!!sliderValue);
_updateVoteCache(_author, _permlink, amount, false, !!sliderValue ? CacheStatus.DELETED : CacheStatus.PUBLISHED); _updateVoteCache(_author, _permlink, amount, false, !!sliderValue ? CacheStatus.PUBLISHED : CacheStatus.DELETED);
}) })
.catch((err) => { .catch((err) => {
_updateVoteCache(_author, _permlink, amount, false, CacheStatus.FAILED); _updateVoteCache(_author, _permlink, amount, false, CacheStatus.FAILED);
@ -267,7 +267,7 @@ const UpvotePopover = forwardRef(({ }: Props, ref) => {
transactionId: response.id, transactionId: response.id,
}); });
setIsVoted(!!sliderValue); setIsVoted(!!sliderValue);
_updateVoteCache(_author, _permlink, amount, true, !!sliderValue ? CacheStatus.DELETED : CacheStatus.PUBLISHED); _updateVoteCache(_author, _permlink, amount, true, !!sliderValue ? CacheStatus.PUBLISHED : CacheStatus.DELETED);
}) })
.catch((err) => { .catch((err) => {
dispatch( dispatch(

View File

@ -318,12 +318,12 @@ export const injectPostCache = (commentsMap, cachedComments, cachedVotes, lastCa
export const injectVoteCache = (post, voteCache) => { export const injectVoteCache = (post, voteCache) => {
if ( if (voteCache && voteCache.status !== CacheStatus.FAILED) {
voteCache &&
(voteCache.status !== CacheStatus.FAILED || voteCache.status !== CacheStatus.DELETED)
) {
const _voteIndex = post.active_votes.findIndex((i) => i.voter === voteCache.voter); 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.total_payout += voteCache.amount * (voteCache.isDownvote ? -1 : 1);
post.active_votes = [ post.active_votes = [
...post.active_votes, ...post.active_votes,
@ -332,8 +332,14 @@ export const injectVoteCache = (post, voteCache) => {
rshares: voteCache.isDownvote ? -1000 : 1000, 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]; post.active_votes = [...post.active_votes];
} }
} }