File size: 4,348 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import {
Menu,
MenuButton,
MenuList,
useColorModeValue,
} from '@chakra-ui/react';
import React from 'react';
import { RiMoreLine } from 'react-icons/ri';
import { useDispatch, useSelector } from 'react-redux';
import { useNavigate } from 'react-router-dom';
import { saveToLocalStorage } from '../../helper/localStorage';
import {
setcommentItem,
setTransformedComments,
} from '../../store/comment/currentComments';
import CustomMenuItem from '../../utils/CustomMenuItem';
// I save all transformedData in localStorage because it navigate next page, so when reload the next page, I take them back from localStorage
const ManageComment = ({ commentId, postId, comments }) => {
const navigate = useNavigate();
const dispatch = useDispatch();
const currentComments = useSelector(
(state) => state.currentComments.currentComments
);
//helper funcs
// to edit
const setCurrentCommentItemHandler = () => {
let commentItem;
currentComments.forEach((comment) => {
if (comment.commentId === commentId) {
commentItem = comment;
return;
}
const innerComments = Object.values(comment.replies);
if (innerComments.find((cmt) => cmt.commentId === commentId)) {
commentItem = innerComments.find(
(cmt) => cmt.commentId === commentId
);
}
});
dispatch(setcommentItem({ ...commentItem, postId })); // also put postId to refrence when edit || delete
saveToLocalStorage(
'commentItemToManage',
JSON.stringify({ ...commentItem, postId })
);
};
//transform comment for delete
const transformedCommentsHandler = () => {
// filter comments without deleteId
const filteredComments = comments.filter(
(comment) => comment.commentId !== commentId
);
const transformedCommennts = filteredComments.map((comment) => {
// filter repliedComments without deleteId
const repliedComments = Object.values(comment.replies)
.sort((a, b) => a.createdAt - b.createdAt)
.filter((cmt) => cmt.commentId !== commentId);
// make commentId array without deleteId
const commentIds = [
comment.commentId,
...repliedComments.map((cmt) => cmt.commentId),
];
// remove id when repliedCommentId doesn't include in commentId array
repliedComments.forEach((cmt) => {
if (!commentIds.includes(cmt.repliedCommentId)) {
commentIds.splice(commentIds.indexOf(cmt.commentId), 1);
}
});
// filtered repliedComments which actually exitst in commentId array
const finalRepliedComments = repliedComments.filter((cmt) =>
commentIds.includes(cmt.commentId)
);
return {
...comment,
replies: { ...finalRepliedComments },
};
});
dispatch(setTransformedComments(transformedCommennts));
saveToLocalStorage(
'transformedComments',
JSON.stringify(transformedCommennts)
);
};
const goToEdit = () => {
setCurrentCommentItemHandler();
navigate('/edit-comment');
};
const goToDelete = () => {
setCurrentCommentItemHandler();
transformedCommentsHandler();
navigate('/delete-comment');
};
const replyToColor = useColorModeValue('#8f8f8f', 'dark.colorTertiary');
return (
<Menu autoSelect={false} isLazy>
<MenuButton
bg='transparent'
p='0 3px'
h='24px'
borderRadius='5px'
_hover={{
bg: useColorModeValue('light.secondary', 'dark.secondary'),
color: useColorModeValue('light.primary', 'dark.primary'),
}}
color={replyToColor}
>
<RiMoreLine size={20} className='more-icon' />
</MenuButton>
<MenuList
minW='0'
w='170px'
p='.5rem'
bg={useColorModeValue('light.cardBg', 'dark.cardBg')}
>
<CustomMenuItem onClick={goToEdit}>Edit</CustomMenuItem>
<CustomMenuItem onClick={goToDelete}>Delete</CustomMenuItem>
</MenuList>
</Menu>
);
};
export default ManageComment;
|