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

134 lines
3.6 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import I18n from 'i18n-js';
2019-09-18 13:40:00 +02:00
import NewComment from './NewComment';
2019-09-17 17:04:19 +02:00
import CommentList from './CommentList';
import Spinner from '../shared/Spinner';
import { DangerText } from '../shared/CustomTexts';
2019-09-17 17:04:19 +02:00
import IComment from '../../interfaces/IComment';
import { ReplyFormState } from '../../reducers/replyFormReducer';
import Separator from '../shared/Separator';
interface Props {
postId: number;
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-18 13:40:00 +02:00
authenticityToken: string;
comments: Array<IComment>;
replyForms: Array<ReplyFormState>;
areLoading: boolean;
error: string;
2019-09-18 13:40:00 +02:00
requestComments(postId: number, page?: number): void;
toggleCommentReply(commentId: number): void;
setCommentReplyBody(commentId: number, body: string): void;
toggleCommentIsPostUpdateFlag(): void;
toggleCommentIsPostUpdate(
postId: number,
commentId: number,
currentIsPostUpdate: boolean,
authenticityToken: string,
): void;
2019-09-18 13:40:00 +02:00
submitComment(
postId: number,
body: string,
parentId: number,
isPostUpdate: boolean,
2019-09-18 13:40:00 +02:00
authenticityToken: string,
): void;
}
class CommentsP extends React.Component<Props> {
componentDidMount() {
this.props.requestComments(this.props.postId);
}
_handleToggleIsCommentUpdate = (commentId: number, currentIsPostUpdate: boolean) => {
this.props.toggleCommentIsPostUpdate(
this.props.postId,
commentId,
currentIsPostUpdate,
this.props.authenticityToken,
);
}
_handleSubmitComment = (body: string, parentId: number, isPostUpdate: boolean) => {
2019-09-18 13:40:00 +02:00
this.props.submitComment(
this.props.postId,
body,
parentId,
isPostUpdate,
2019-09-18 13:40:00 +02:00
this.props.authenticityToken,
);
}
render() {
const {
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,
2019-09-20 18:43:24 +02:00
comments,
replyForms,
areLoading,
error,
toggleCommentReply,
setCommentReplyBody,
toggleCommentIsPostUpdateFlag,
} = this.props;
const postReply = replyForms.find(replyForm => replyForm.commentId === null);
2019-09-20 18:43:24 +02:00
return (
<div className="commentsContainer">
2019-09-18 13:40:00 +02:00
<NewComment
2019-09-20 18:43:24 +02:00
body={postReply && postReply.body}
2019-09-18 13:40:00 +02:00
parentId={null}
postUpdateFlagValue={postReply && postReply.isPostUpdate}
2019-09-20 18:43:24 +02:00
isSubmitting={postReply && postReply.isSubmitting}
error={postReply && postReply.error}
2019-09-18 13:40:00 +02:00
handleChange={
(e: React.FormEvent) => (
setCommentReplyBody(null, (e.target as HTMLTextAreaElement).value)
2019-09-18 13:40:00 +02:00
)
}
handlePostUpdateFlag={toggleCommentIsPostUpdateFlag}
2019-09-18 13:40:00 +02:00
handleSubmit={this._handleSubmitComment}
2019-09-20 18:43:24 +02:00
isLoggedIn={isLoggedIn}
isPowerUser={isPowerUser}
2019-10-02 15:26:32 +02:00
userEmail={userEmail}
2019-09-18 13:40:00 +02:00
/>
2019-09-17 17:04:19 +02:00
{ areLoading ? <Spinner /> : null }
{ error ? <DangerText>{error}</DangerText> : null }
<div className="commentsTitle">
{I18n.t('post.comments.title')}
<Separator />
{I18n.t('common.comments_number', { count: comments.length })}
</div>
2019-09-17 17:04:19 +02:00
<CommentList
comments={comments}
replyForms={replyForms}
toggleCommentReply={toggleCommentReply}
setCommentReplyBody={setCommentReplyBody}
handleToggleIsCommentUpdate={this._handleToggleIsCommentUpdate}
2019-09-18 13:40:00 +02:00
handleSubmitComment={this._handleSubmitComment}
2019-09-17 17:04:19 +02:00
parentId={null}
level={1}
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>
);
}
}
export default CommentsP;