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

79 lines
2.1 KiB
TypeScript
Raw Normal View History

2019-09-17 17:04:19 +02:00
import * as React from 'react';
import Comment from './Comment';
import IComment from '../../interfaces/IComment';
import { ReplyFormState } from '../../reducers/replyFormReducer';
2019-09-17 17:04:19 +02:00
interface Props {
comments: Array<IComment>;
replyForms: Array<ReplyFormState>;
2019-09-17 17:04:19 +02:00
parentId: number;
level: number;
2019-09-18 13:40:00 +02:00
toggleCommentReply(commentId: number): void;
setCommentReplyBody(commentId: number, body: string): void;
handleSubmitComment(body: string, parentId: number): void;
2019-09-20 18:43:24 +02:00
isLoggedIn: boolean;
2019-09-25 11:50:23 +02:00
isPowerUser: boolean;
2019-10-02 15:26:32 +02:00
userEmail: string;
2019-09-17 17:04:19 +02:00
}
const CommentList = ({
comments,
replyForms,
parentId,
level,
toggleCommentReply,
setCommentReplyBody,
2019-09-18 13:40:00 +02:00
handleSubmitComment,
2019-09-20 18:43:24 +02:00
isLoggedIn,
2019-09-25 11:50:23 +02:00
isPowerUser,
2019-10-02 15:26:32 +02:00
userEmail,
}: Props) => (
2019-09-17 17:04:19 +02:00
<React.Fragment>
{comments.map((comment, i) => {
if (comment.parentId === parentId) {
return (
2019-09-18 13:40:00 +02:00
<div className="commentList" key={i}>
<Comment
replyForm={replyForms.find(replyForm => replyForm.commentId === comment.id)}
handleToggleCommentReply={() => toggleCommentReply(comment.id)}
handleCommentReplyBodyChange={
(e: React.FormEvent) => (
setCommentReplyBody(comment.id, (e.target as HTMLTextAreaElement).value)
)
}
2019-09-18 13:40:00 +02:00
handleSubmitComment={handleSubmitComment}
{...comment}
2019-09-20 18:43:24 +02:00
isLoggedIn={isLoggedIn}
2019-09-25 11:50:23 +02:00
isPowerUser={isPowerUser}
2019-10-02 15:26:32 +02:00
currentUserEmail={userEmail}
/>
2019-09-17 17:04:19 +02:00
<CommentList
comments={comments}
replyForms={replyForms}
2019-09-17 17:04:19 +02:00
parentId={comment.id}
level={level+1}
toggleCommentReply={toggleCommentReply}
setCommentReplyBody={setCommentReplyBody}
2019-09-18 13:40:00 +02:00
handleSubmitComment={handleSubmitComment}
2019-09-20 18:43:24 +02:00
isLoggedIn={isLoggedIn}
2019-09-25 11:50:23 +02:00
isPowerUser={isPowerUser}
2019-10-02 15:26:32 +02:00
userEmail={userEmail}
2019-09-17 17:04:19 +02:00
/>
</div>
);
} else return null;
})}
</React.Fragment>
);
export default CommentList;