2019-09-17 11:33:18 +02:00
|
|
|
import * as React from 'react';
|
2022-06-05 11:40:43 +02:00
|
|
|
import I18n from 'i18n-js';
|
2019-09-17 11:33:18 +02:00
|
|
|
|
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';
|
2019-09-26 11:00:32 +02:00
|
|
|
import { DangerText } from '../shared/CustomTexts';
|
2019-09-17 17:04:19 +02:00
|
|
|
|
2019-09-17 11:33:18 +02:00
|
|
|
import IComment from '../../interfaces/IComment';
|
2019-09-26 18:22:18 +02:00
|
|
|
import { ReplyFormState } from '../../reducers/replyFormReducer';
|
2022-06-05 11:40:43 +02:00
|
|
|
import Separator from '../shared/Separator';
|
2019-09-17 11:33:18 +02:00
|
|
|
|
|
|
|
|
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;
|
2019-09-17 11:33:18 +02:00
|
|
|
|
|
|
|
|
comments: Array<IComment>;
|
2019-09-26 18:22:18 +02:00
|
|
|
replyForms: Array<ReplyFormState>;
|
2019-09-17 11:33:18 +02:00
|
|
|
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;
|
2022-05-28 11:03:36 +02:00
|
|
|
toggleCommentIsPostUpdateFlag(): void;
|
2019-10-02 16:43:13 +02:00
|
|
|
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,
|
2022-05-28 11:03:36 +02:00
|
|
|
isPostUpdate: boolean,
|
2019-09-18 13:40:00 +02:00
|
|
|
authenticityToken: string,
|
|
|
|
|
): void;
|
2019-09-17 11:33:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CommentsP extends React.Component<Props> {
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.props.requestComments(this.props.postId);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-02 16:43:13 +02:00
|
|
|
_handleToggleIsCommentUpdate = (commentId: number, currentIsPostUpdate: boolean) => {
|
|
|
|
|
this.props.toggleCommentIsPostUpdate(
|
|
|
|
|
this.props.postId,
|
|
|
|
|
commentId,
|
|
|
|
|
currentIsPostUpdate,
|
|
|
|
|
this.props.authenticityToken,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 11:03:36 +02:00
|
|
|
_handleSubmitComment = (body: string, parentId: number, isPostUpdate: boolean) => {
|
2019-09-18 13:40:00 +02:00
|
|
|
this.props.submitComment(
|
|
|
|
|
this.props.postId,
|
|
|
|
|
body,
|
|
|
|
|
parentId,
|
2022-05-28 11:03:36 +02:00
|
|
|
isPostUpdate,
|
2019-09-18 13:40:00 +02:00
|
|
|
this.props.authenticityToken,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 11:33:18 +02:00
|
|
|
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
|
|
|
|
2019-09-17 11:33:18 +02:00
|
|
|
comments,
|
2019-09-26 18:22:18 +02:00
|
|
|
replyForms,
|
2019-09-17 11:33:18 +02:00
|
|
|
areLoading,
|
|
|
|
|
error,
|
2019-09-17 19:09:38 +02:00
|
|
|
|
|
|
|
|
toggleCommentReply,
|
|
|
|
|
setCommentReplyBody,
|
2022-05-28 11:03:36 +02:00
|
|
|
toggleCommentIsPostUpdateFlag,
|
2019-09-17 11:33:18 +02:00
|
|
|
} = this.props;
|
|
|
|
|
|
2019-09-26 18:22:18 +02:00
|
|
|
const postReply = replyForms.find(replyForm => replyForm.commentId === null);
|
2019-09-20 18:43:24 +02:00
|
|
|
|
2019-09-17 11:33:18 +02:00
|
|
|
return (
|
2019-09-20 17:56:01 +02:00
|
|
|
<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}
|
2022-05-28 11:03:36 +02:00
|
|
|
postUpdateFlagValue={postReply && postReply.isPostUpdate}
|
2019-09-20 18:43:24 +02:00
|
|
|
isSubmitting={postReply && postReply.isSubmitting}
|
2019-09-26 18:22:18 +02:00
|
|
|
error={postReply && postReply.error}
|
2019-09-18 13:40:00 +02:00
|
|
|
handleChange={
|
2019-09-26 11:00:32 +02:00
|
|
|
(e: React.FormEvent) => (
|
2019-09-26 18:22:18 +02:00
|
|
|
setCommentReplyBody(null, (e.target as HTMLTextAreaElement).value)
|
2019-09-18 13:40:00 +02:00
|
|
|
)
|
|
|
|
|
}
|
2022-05-28 11:03:36 +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}
|
2022-05-28 11:03:36 +02:00
|
|
|
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 }
|
|
|
|
|
|
2019-09-26 18:22:18 +02:00
|
|
|
<div className="commentsTitle">
|
2022-06-05 11:40:43 +02:00
|
|
|
{I18n.t('post.comments.title')}
|
|
|
|
|
<Separator />
|
|
|
|
|
{I18n.t('common.comments_number', { count: comments.length })}
|
2019-09-26 18:22:18 +02:00
|
|
|
</div>
|
2019-09-20 17:56:01 +02:00
|
|
|
|
2019-09-17 17:04:19 +02:00
|
|
|
<CommentList
|
|
|
|
|
comments={comments}
|
2019-09-26 18:22:18 +02:00
|
|
|
replyForms={replyForms}
|
2019-09-17 19:09:38 +02:00
|
|
|
toggleCommentReply={toggleCommentReply}
|
|
|
|
|
setCommentReplyBody={setCommentReplyBody}
|
2019-10-02 16:43:13 +02:00
|
|
|
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
|
|
|
/>
|
2019-09-17 11:33:18 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default CommentsP;
|