From 31a020b149be5660560d0700f20fad9719f19594 Mon Sep 17 00:00:00 2001 From: noumantahir Date: Sun, 30 Jan 2022 22:30:26 +0500 Subject: [PATCH] added support for deleting cached comment and cleaning cache on delete --- .../comments/container/commentsContainer.js | 20 ++++++++++++++++--- src/redux/actions/cacheActions.ts | 8 +++++++- src/redux/constants/constants.js | 1 + src/redux/reducers/cacheReducer.ts | 9 ++++++++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/components/comments/container/commentsContainer.js b/src/components/comments/container/commentsContainer.js index 36b566b1a..0e5100736 100644 --- a/src/components/comments/container/commentsContainer.js +++ b/src/components/comments/container/commentsContainer.js @@ -19,6 +19,7 @@ import ROUTES from '../../../constants/routeNames'; // Component import CommentsView from '../view/commentsView'; import { useAppSelector } from '../../../hooks'; +import { deleteCommentCacheEntry } from '../../../redux/actions/cacheActions'; const CommentsContainer = ({ author, @@ -261,12 +262,25 @@ const CommentsContainer = ({ let filteredComments; deleteComment(currentAccount, pinCode, _permlink).then(() => { + let cachePath = null; + const _applyFilter = (item) => { + if (item.permlink === _permlink) { + cachePath = `${item.parent_author}/${item.parent_permlink}`; + return false; + } + return true; + }; + if (lcomments.length > 0) { - filteredComments = lcomments.filter((item) => item.permlink !== _permlink); + filteredComments = lcomments.filter(_applyFilter); + setLComments(filteredComments); } else { - filteredComments = comments.filter((item) => item.permlink !== _permlink); + filteredComments = replies.filter(_applyFilter); + setReplies(filteredComments); } - setLComments(filteredComments); + + // remove cached entry based on parent + dispatch(deleteCommentCacheEntry(cachePath)); }); }; diff --git a/src/redux/actions/cacheActions.ts b/src/redux/actions/cacheActions.ts index f9e9637e1..55f3b4db0 100644 --- a/src/redux/actions/cacheActions.ts +++ b/src/redux/actions/cacheActions.ts @@ -4,7 +4,8 @@ import { makeJsonMetadataReply } from '../../utils/editor'; import { UPDATE_VOTE_CACHE, PURGE_EXPIRED_CACHE, - UPDATE_COMMENT_CACHE + UPDATE_COMMENT_CACHE, + DELETE_COMMENT_CACHE_ENTRY } from '../constants/constants'; import { Comment, Vote } from '../reducers/cacheReducer'; @@ -64,6 +65,11 @@ import { Comment, Vote } from '../reducers/cacheReducer'; type: UPDATE_COMMENT_CACHE }) } + + export const deleteCommentCacheEntry = (commentPath:string) => ({ + payload:commentPath, + type: DELETE_COMMENT_CACHE_ENTRY + }) export const purgeExpiredCache = () => ({ type: PURGE_EXPIRED_CACHE diff --git a/src/redux/constants/constants.js b/src/redux/constants/constants.js index 704e6b153..5c25bdc07 100644 --- a/src/redux/constants/constants.js +++ b/src/redux/constants/constants.js @@ -108,3 +108,4 @@ export const TEMP_BENEFICIARIES_ID = 'temp-beneficiaries'; export const PURGE_EXPIRED_CACHE = 'PURGE_EXPIRED_CACHE'; export const UPDATE_VOTE_CACHE = 'UPDATE_VOTE_CACHE'; export const UPDATE_COMMENT_CACHE = 'UPDATE_COMMENT_CACHE'; +export const DELETE_COMMENT_CACHE_ENTRY = 'DELETE_COMMENT_CACHE_ENTRY'; diff --git a/src/redux/reducers/cacheReducer.ts b/src/redux/reducers/cacheReducer.ts index ac54231d1..368b48e13 100644 --- a/src/redux/reducers/cacheReducer.ts +++ b/src/redux/reducers/cacheReducer.ts @@ -1,4 +1,4 @@ -import { PURGE_EXPIRED_CACHE, UPDATE_VOTE_CACHE, UPDATE_COMMENT_CACHE } from "../constants/constants"; +import { PURGE_EXPIRED_CACHE, UPDATE_VOTE_CACHE, UPDATE_COMMENT_CACHE, DELETE_COMMENT_CACHE_ENTRY } from "../constants/constants"; export interface Vote { amount:number; @@ -71,6 +71,13 @@ const initialState:State = { type:'comment' } }; + + case DELETE_COMMENT_CACHE_ENTRY: + if(state.comments && state.comments.has(payload)){ + state.comments.delete(payload); + } + return { ...state } + case PURGE_EXPIRED_CACHE: const currentTime = new Date().getTime();