Files
astuto/app/javascript/components/Comments/CommentList.tsx

61 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-09-17 17:04:19 +02:00
import * as React from 'react';
import { FormEvent } from 'react';
2019-09-17 17:04:19 +02:00
import Comment from './Comment';
import IComment from '../../interfaces/IComment';
import { CommentRepliesState } from '../../reducers/commentRepliesReducer';
2019-09-17 17:04:19 +02:00
interface Props {
comments: Array<IComment>;
replies: Array<CommentRepliesState>;
2019-09-17 17:04:19 +02:00
parentId: number;
level: number;
toggleCommentReply(commentId: number);
setCommentReplyBody(commentId: number, body: string);
2019-09-17 17:04:19 +02:00
}
const CommentList = ({
comments,
replies,
parentId,
level,
toggleCommentReply,
setCommentReplyBody,
}: Props) => (
2019-09-17 17:04:19 +02:00
<React.Fragment>
{comments.map((comment, i) => {
if (comment.parentId === parentId) {
return (
<div className="commentList">
<Comment
level={level}
reply={replies.find(reply => reply.commentId === comment.id)}
handleToggleCommentReply={() => toggleCommentReply(comment.id)}
handleCommentReplyBodyChange={
(e: FormEvent) => (
setCommentReplyBody(comment.id, (e.target as HTMLTextAreaElement).value)
)
}
{...comment}
/>
2019-09-17 17:04:19 +02:00
<CommentList
comments={comments}
replies={replies}
2019-09-17 17:04:19 +02:00
parentId={comment.id}
level={level+1}
toggleCommentReply={toggleCommentReply}
setCommentReplyBody={setCommentReplyBody}
2019-09-17 17:04:19 +02:00
/>
</div>
);
} else return null;
})}
</React.Fragment>
);
export default CommentList;