updated comment body state based on cache event

This commit is contained in:
noumantahir 2022-01-30 02:19:15 +05:00
parent 1a3ced39de
commit 2343083b34
3 changed files with 37 additions and 15 deletions

View File

@ -33,10 +33,8 @@ const CommentView = ({
handleOnVotersPress, handleOnVotersPress,
isLoggedIn, isLoggedIn,
isShowComments, isShowComments,
isShowMoreButton,
mainAuthor = { mainAuthor }, mainAuthor = { mainAuthor },
isHideImage, isHideImage,
showAllComments,
isShowSubComments, isShowSubComments,
hideManyCommentsButton, hideManyCommentsButton,
openReplyThread, openReplyThread,
@ -46,12 +44,17 @@ const CommentView = ({
const actionSheet = useRef(null); const actionSheet = useRef(null);
const isMuted = useAppSelector(state => state.account.currentAccount.mutes?.indexOf(comment.author) > -1); const isMuted = useAppSelector(state => state.account.currentAccount.mutes?.indexOf(comment.author) > -1);
const lastCacheUpdate = useAppSelector((state) => state.cache.lastUpdate);
const cachedComments = useAppSelector((state) => state.cache.comments);
const [_isShowSubComments, setIsShowSubComments] = useState(isShowSubComments || false); const [_isShowSubComments, setIsShowSubComments] = useState(isShowSubComments || false);
const [isPressedShowButton, setIsPressedShowButton] = useState(false); const [isPressedShowButton, setIsPressedShowButton] = useState(false);
const [activeVotes, setActiveVotes] = useState([]); const [activeVotes, setActiveVotes] = useState([]);
const [cacheVoteIcrement, setCacheVoteIcrement] = useState(0); const [cacheVoteIcrement, setCacheVoteIcrement] = useState(0);
const [childCount, setChildCount] = useState(comment.children);
const [replies, setReplies] = useState(comment.replies);
useEffect(() => { useEffect(() => {
@ -60,8 +63,32 @@ const CommentView = ({
} }
}, [comment]); }, [comment]);
const _showSubCommentsToggle = () => {
if(comment.replies && comment.replies.length > 0){ useEffect(() => {
const postPath = `${comment.author || ''}/${comment.permlink || ''}`;
//this conditional makes sure on targetted already fetched post is updated
//with new cache status, this is to avoid duplicate cache merging
if (
lastCacheUpdate &&
lastCacheUpdate.postPath === postPath &&
lastCacheUpdate.type === 'comment' &&
lastCacheUpdate.updatedAt > fetchedAt
) {
//TODO: update comment count and show sub comment if required;
const cachedComment = cachedComments.get(postPath);
if(cachedComment.updated === cachedComment.created){
setChildCount(childCount + 1);
setReplies(replies ? [...replies, cachedComment] : [cachedComment]);
}
if(!_isShowSubComments){
_showSubCommentsToggle(true);
}
}
}, [lastCacheUpdate]);
const _showSubCommentsToggle = (force) => {
if(((replies && replies.length > 0) || force)){
setIsShowSubComments(!_isShowSubComments); setIsShowSubComments(!_isShowSubComments);
setIsPressedShowButton(true); setIsPressedShowButton(true);
} else if(openReplyThread) { } else if(openReplyThread) {
@ -105,9 +132,8 @@ const CommentView = ({
avatarSize={avatarSize || 24} avatarSize={avatarSize || 24}
author={comment.author} author={comment.author}
permlink={comment.permlink} permlink={comment.permlink}
commentCount={comment.children} commentCount={childCount}
comments={comment.replies} comments={comment.replies}
isShowMoreButton={false}
hasManyComments={commentNumber === 5 && get(comment, 'children') > 0} hasManyComments={commentNumber === 5 && get(comment, 'children') > 0}
fetchPost={fetchPost} fetchPost={fetchPost}
hideManyCommentsButton={hideManyCommentsButton} hideManyCommentsButton={hideManyCommentsButton}
@ -139,8 +165,8 @@ const CommentView = ({
{_renderActionPanel()} {_renderActionPanel()}
</View> </View>
{ commentNumber > 1 && { commentNumber > 1 &&
comment.children > 0 && childCount > 0 &&
!comment.replies?.length && !replies?.length &&
_renderReadMoreButton() _renderReadMoreButton()
} }
</Fragment> </Fragment>
@ -195,7 +221,7 @@ const CommentView = ({
onPress={() => handleOnEditPress && handleOnEditPress(comment)} onPress={() => handleOnEditPress && handleOnEditPress(comment)}
iconType="MaterialIcons" iconType="MaterialIcons"
/> />
{!comment.children && !activeVotes.length && ( {!childCount && !activeVotes.length && (
<Fragment> <Fragment>
<IconButton <IconButton
size={20} size={20}
@ -224,7 +250,7 @@ const CommentView = ({
)} )}
{isShowMoreButton && ( {commentNumber === 1 && childCount > 0 && (
<View style={styles.rightButtonWrapper}> <View style={styles.rightButtonWrapper}>
<TextWithIcon <TextWithIcon
wrapperStyle={styles.rightButton} wrapperStyle={styles.rightButton}
@ -235,7 +261,7 @@ const CommentView = ({
iconStyle={styles.iconStyle} iconStyle={styles.iconStyle}
iconSize={16} iconSize={16}
onPress={() => _showSubCommentsToggle()} onPress={() => _showSubCommentsToggle()}
text={`${comment.children} ${intl.formatMessage({ id: 'comments.more_replies' })}`} text={`${childCount} ${intl.formatMessage({ id: 'comments.more_replies' })}`}
/> />
</View> </View>
)} )}

View File

@ -36,7 +36,6 @@ const CommentsContainer = ({
commentCount, commentCount,
isLoggedIn, isLoggedIn,
commentNumber, commentNumber,
isShowMoreButton,
mainAuthor, mainAuthor,
selectedPermlink: _selectedPermlink, selectedPermlink: _selectedPermlink,
isHideImage, isHideImage,
@ -313,7 +312,6 @@ const CommentsContainer = ({
selectedPermlink={_selectedPermlink || selectedPermlink} selectedPermlink={_selectedPermlink || selectedPermlink}
author={author} author={author}
mainAuthor={mainAuthor} mainAuthor={mainAuthor}
isShowMoreButton={isShowMoreButton}
commentNumber={commentNumber || 1} commentNumber={commentNumber || 1}
commentCount={commentCount} commentCount={commentCount}
comments={lcomments.length > 0 ? lcomments : replies} comments={lcomments.length > 0 ? lcomments : replies}

View File

@ -31,7 +31,6 @@ const CommentsView = ({
isShowSubComments, isShowSubComments,
mainAuthor, mainAuthor,
marginLeft, marginLeft,
isShowMoreButton,
showAllComments, showAllComments,
hideManyCommentsButton, hideManyCommentsButton,
flatListProps, flatListProps,
@ -106,7 +105,6 @@ const CommentsView = ({
handleOnVotersPress={handleOnVotersPress} handleOnVotersPress={handleOnVotersPress}
isHideImage={isHideImage} isHideImage={isHideImage}
isLoggedIn={isLoggedIn} isLoggedIn={isLoggedIn}
isShowMoreButton={isShowMoreButton || (commentNumber === 1 && get(item, 'children') > 0)}
showAllComments={showAllComments} showAllComments={showAllComments}
isShowSubComments={isShowSubComments} isShowSubComments={isShowSubComments}
key={get(item, 'permlink')} key={get(item, 'permlink')}