File size: 3,187 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
import React from 'react';
import { Box } from '@chakra-ui/react';
import { useDispatch, useSelector } from 'react-redux';
import { getUserProfileData } from '../../helper/getUserProfileData';
import CommentItem from './CommentItem';
import { useEffect } from 'react';
import { setCurrentComments } from '../../store/comment/currentComments';
import { useAuth } from '../../context/auth';

const AllComment = ({ postDetail }) => {
   const dispatch = useDispatch();
   const user = useAuth();

   useEffect(() => {
      dispatch(setCurrentComments(postDetail.comments));
   }, [postDetail.comments, dispatch]);

   const profileData = useSelector((state) => state.profileData.profileData);
   const currentComments = useSelector(
      (state) => state.currentComments.currentComments
   );

   const repliedComment = (replies) =>
      Object.values(replies).sort((a, b) => a.createdAt - b.createdAt); //ascending ordered by created at;

   return (
      <Box mt='2rem'>
         {postDetail.comments.map((comment) => (
            <Box key={comment.commentId}>
               <CommentItem
                  avatarSize='30px'
                  footerPs='37px'
                  comments={currentComments}
                  authorId={postDetail.userId}
                  currentUserId={user?.userId}
                  likes={comment.likes}
                  text={comment.value}
                  createdAt={comment.createdAt}
                  currentUserProfile={getUserProfileData(
                     profileData,
                     comment.userId
                  )}
                  userId={comment.userId}
                  postId={postDetail.id}
                  commentId={comment.commentId}
                  edited={comment.edited}
                  editedAt={comment.editedAt}
               />
               {Object.values(comment.replies).length !== 0 &&
                  repliedComment(comment.replies).map((item) => (
                     <CommentItem
                        key={item.commentId}
                        comments={currentComments}
                        authorId={postDetail.userId}
                        currentUserId={user?.userId}
                        likes={item.likes}
                        ps='20px'
                        avatarSize='28px'
                        footerPs='35px'
                        text={item.value}
                        createdAt={item.createdAt}
                        currentUserProfile={getUserProfileData(
                           profileData,
                           item.userId
                        )}
                        repliedUserName={
                           getUserProfileData(profileData, item.repliedUserId)
                              .name
                        }
                        userId={item.userId}
                        postId={postDetail.id}
                        commentId={item.commentId}
                        edited={item.edited}
                        editedAt={item.editedAt}
                        reply={true}
                     />
                  ))}
            </Box>
         ))}
      </Box>
   );
};

export default AllComment;