From c6f69c64e2019428ef1422ea87c2bf61357c22a8 Mon Sep 17 00:00:00 2001 From: noumantahir Date: Fri, 28 Jan 2022 23:33:48 +0500 Subject: [PATCH] added basic comments cache store model --- .../upvote/container/upvoteContainer.js | 3 +- src/redux/actions/cacheActions.ts | 11 ++++- src/redux/constants/constants.js | 1 + src/redux/reducers/cacheReducer.ts | 42 +++++++++++++++++-- 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/components/upvote/container/upvoteContainer.js b/src/components/upvote/container/upvoteContainer.js index 7103ad48c..99ea87ec1 100644 --- a/src/components/upvote/container/upvoteContainer.js +++ b/src/components/upvote/container/upvoteContainer.js @@ -73,7 +73,8 @@ const UpvoteContainer = (props) => { if ( lastCacheUpdate && lastCacheUpdate.postPath === postPath && - content.post_fetched_at < lastCacheUpdate.updatedAt + content.post_fetched_at < lastCacheUpdate.updatedAt && + lastCacheUpdate.type === 'vote' ) { _handleCachedVote(); } diff --git a/src/redux/actions/cacheActions.ts b/src/redux/actions/cacheActions.ts index 2ab0da214..b84141902 100644 --- a/src/redux/actions/cacheActions.ts +++ b/src/redux/actions/cacheActions.ts @@ -1,6 +1,7 @@ import { UPDATE_VOTE_CACHE, - PURGE_EXPIRED_CACHE + PURGE_EXPIRED_CACHE, + UPDATE_COMMENT_CACHE } from '../constants/constants'; import { Vote } from '../reducers/cacheReducer'; @@ -12,6 +13,14 @@ import { Vote } from '../reducers/cacheReducer'; }, type: UPDATE_VOTE_CACHE }) + + export const updateCommentCache = (commentPath:string, comment:Comment) => ({ + payload:{ + commentPath, + comment + }, + type: UPDATE_COMMENT_CACHE + }) export const purgeExpiredCache = () => ({ type: PURGE_EXPIRED_CACHE diff --git a/src/redux/constants/constants.js b/src/redux/constants/constants.js index 1356ede25..704e6b153 100644 --- a/src/redux/constants/constants.js +++ b/src/redux/constants/constants.js @@ -107,3 +107,4 @@ export const TEMP_BENEFICIARIES_ID = 'temp-beneficiaries'; //CACHE export const PURGE_EXPIRED_CACHE = 'PURGE_EXPIRED_CACHE'; export const UPDATE_VOTE_CACHE = 'UPDATE_VOTE_CACHE'; +export const UPDATE_COMMENT_CACHE = 'UPDATE_COMMENT_CACHE'; diff --git a/src/redux/reducers/cacheReducer.ts b/src/redux/reducers/cacheReducer.ts index 891001548..83eb8e583 100644 --- a/src/redux/reducers/cacheReducer.ts +++ b/src/redux/reducers/cacheReducer.ts @@ -1,4 +1,4 @@ -import { PURGE_EXPIRED_CACHE, UPDATE_VOTE_CACHE } from "../constants/constants"; +import { PURGE_EXPIRED_CACHE, UPDATE_VOTE_CACHE, UPDATE_COMMENT_CACHE } from "../constants/constants"; export interface Vote { amount:number; @@ -8,16 +8,29 @@ export interface Vote { expiresAt:number; } +export interface Comment { + author:string, + permlink:string, + parentAuthor:string, + parentPermlink:string, + content:string, + createdAt:number, + expiresAt:number, +} + interface State { votes:Map + comments:Map lastUpdate:{ postPath:string, updatedAt:number, + type:'vote'|'comment', } } const initialState:State = { votes:new Map(), + comments:new Map(), lastUpdate:null, }; @@ -33,19 +46,42 @@ const initialState:State = { ...state, //spread operator in requried here, otherwise persist do not register change lastUpdate:{ postPath:payload.postPath, - updatedAt: new Date().getTime() + updatedAt: new Date().getTime(), + type:'vote', + } + }; + + case UPDATE_COMMENT_CACHE: + if(!state.comments){ + state.comments = new Map(); + } + state.comments.set(payload.commentPath, payload.comment); + return { + ...state, //spread operator in requried here, otherwise persist do not register change + lastUpdate:{ + postPath:payload.commentPath, + updatedAt: new Date().getTime(), + type:'comment' } }; case PURGE_EXPIRED_CACHE: const currentTime = new Date().getTime(); - if(state.votes && state.votes.entries){ + if(state.votes && state.votes.size){ Array.from(state.votes).forEach((entry)=>{ if(entry[1].expiresAt < currentTime){ state.votes.delete(entry[0]); } }) } + + if(state.comments && state.comments.size){ + Array.from(state.comments).forEach((entry)=>{ + if(entry[1].expiresAt < currentTime){ + state.comments.delete(entry[0]); + } + }) + } return { ...state