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

187 lines
5.0 KiB
TypeScript
Raw Permalink Normal View History

2019-09-17 17:04:19 +02:00
import * as React from 'react';
2022-06-06 20:20:03 +02:00
import ReactMarkdown from 'react-markdown';
import I18n from 'i18n-js';
2019-09-17 17:04:19 +02:00
2019-09-18 13:40:00 +02:00
import NewComment from './NewComment';
2019-09-17 17:04:19 +02:00
import { ReplyFormState } from '../../reducers/replyFormReducer';
import CommentEditForm from './CommentEditForm';
import CommentFooter from './CommentFooter';
import { StaffIcon } from '../common/Icons';
import Avatar from '../common/Avatar';
import CommentAttachments from './CommentAttachments';
import ITenantSetting from '../../interfaces/ITenantSetting';
2019-09-17 17:04:19 +02:00
interface Props {
id: number;
body: string;
isPostUpdate: boolean;
attachmentUrls?: string[];
2019-09-17 17:04:19 +02:00
userFullName: string;
2019-09-30 23:28:52 +02:00
userEmail: string;
userAvatar?: string;
userRole: number;
createdAt: string;
2019-09-17 17:04:19 +02:00
updatedAt: string;
replyForm: ReplyFormState;
handleToggleCommentReply(): void;
handleCommentReplyBodyChange(e: React.FormEvent): void;
handleSubmitComment(body: string, parentId: number, isPostUpdate: boolean, attachments: File[], onSuccess: Function): void;
2025-02-05 22:53:22 +01:00
handleUpdateComment(commentId: number, body: string, isPostUpdate: boolean, attachmentsToDelete: number[], attachments: File[], onSuccess: Function): void;
handleDeleteComment(id: number): void;
2019-09-20 18:43:24 +02:00
tenantSetting: ITenantSetting;
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
currentUserEmail: string;
currentUserAvatar?: string;
2019-09-17 17:04:19 +02:00
}
interface State {
editMode: boolean;
}
class Comment extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
editMode: false,
};
this.toggleEditMode = this.toggleEditMode.bind(this);
this._handleUpdateComment = this._handleUpdateComment.bind(this);
}
toggleEditMode() {
this.setState({editMode: !this.state.editMode});
}
2025-02-05 22:53:22 +01:00
_handleUpdateComment(body: string, isPostUpdate: boolean, attachmentsToDelete: number[], attachments: File[]) {
this.props.handleUpdateComment(
this.props.id,
body,
isPostUpdate,
2025-02-05 22:53:22 +01:00
attachmentsToDelete,
attachments,
this.toggleEditMode,
);
}
render() {
const {
id,
body,
isPostUpdate,
attachmentUrls,
userFullName,
userEmail,
userAvatar,
userRole,
createdAt,
updatedAt,
replyForm,
handleToggleCommentReply,
handleCommentReplyBodyChange,
handleSubmitComment,
handleDeleteComment,
tenantSetting,
isLoggedIn,
isPowerUser,
currentUserEmail,
currentUserAvatar,
} = this.props;
return (
<div className="comment">
<div className="commentHeader">
<Avatar avatarUrl={userAvatar} email={userEmail} size={28} />
<span className="commentAuthor">{userFullName}</span>
{ userRole > 0 && <StaffIcon /> }
{
isPostUpdate &&
<span className="postUpdateBadge">
{I18n.t('post.comments.post_update_badge')}
</span>
}
</div>
{
this.state.editMode ?
<CommentEditForm
id={id}
initialBody={body}
initialIsPostUpdate={isPostUpdate}
2025-02-05 22:53:22 +01:00
attachmentUrls={attachmentUrls}
isPowerUser={isPowerUser}
2025-02-05 22:53:22 +01:00
tenantSetting={tenantSetting}
handleUpdateComment={this._handleUpdateComment}
toggleEditMode={this.toggleEditMode}
/>
:
<>
<ReactMarkdown
className="commentBody"
disallowedTypes={['heading', 'image', 'html']}
unwrapDisallowed
>
{body}
</ReactMarkdown>
{
attachmentUrls && attachmentUrls.length > 0 &&
<CommentAttachments attachmentUrls={attachmentUrls} />
}
<CommentFooter
id={id}
createdAt={createdAt}
updatedAt={updatedAt}
replyForm={replyForm}
isPowerUser={isPowerUser}
currentUserEmail={currentUserEmail}
commentAuthorEmail={userEmail}
handleDeleteComment={handleDeleteComment}
handleToggleCommentReply={handleToggleCommentReply}
toggleEditMode={this.toggleEditMode}
/>
</>
}
{
replyForm.isOpen ?
<NewComment
body={replyForm.body}
parentId={id}
postUpdateFlagValue={replyForm.isPostUpdate}
isSubmitting={replyForm.isSubmitting}
error={replyForm.error}
handleChange={handleCommentReplyBodyChange}
handlePostUpdateFlag={() => null}
handleSubmit={handleSubmitComment}
allowAttachmentUpload={tenantSetting.allow_attachment_upload}
isLoggedIn={isLoggedIn}
isPowerUser={isPowerUser}
userEmail={currentUserEmail}
userAvatar={currentUserAvatar}
/>
:
null
}
</div>
);
}
}
2019-09-17 17:04:19 +02:00
export default Comment;