import * as React from 'react'; import ReactMarkdown from 'react-markdown'; import I18n from 'i18n-js'; import NewComment from './NewComment'; 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'; interface Props { id: number; body: string; isPostUpdate: boolean; attachmentUrls?: string[]; userFullName: string; userEmail: string; userAvatar?: string; userRole: number; createdAt: string; updatedAt: string; replyForm: ReplyFormState; handleToggleCommentReply(): void; handleCommentReplyBodyChange(e: React.FormEvent): void; handleSubmitComment(body: string, parentId: number, isPostUpdate: boolean, attachments: File[], onSuccess: Function): void; handleUpdateComment(commentId: number, body: string, isPostUpdate: boolean, attachmentsToDelete: number[], attachments: File[], onSuccess: Function): void; handleDeleteComment(id: number): void; tenantSetting: ITenantSetting; isLoggedIn: boolean; isPowerUser: boolean; currentUserEmail: string; currentUserAvatar?: string; } interface State { editMode: boolean; } class Comment extends React.Component { 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}); } _handleUpdateComment(body: string, isPostUpdate: boolean, attachmentsToDelete: number[], attachments: File[]) { this.props.handleUpdateComment( this.props.id, body, isPostUpdate, 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 (
{userFullName} { userRole > 0 && } { isPostUpdate && {I18n.t('post.comments.post_update_badge')} }
{ this.state.editMode ? : <> {body} { attachmentUrls && attachmentUrls.length > 0 && } } { replyForm.isOpen ? null} handleSubmit={handleSubmitComment} allowAttachmentUpload={tenantSetting.allow_attachment_upload} isLoggedIn={isLoggedIn} isPowerUser={isPowerUser} userEmail={currentUserEmail} userAvatar={currentUserAvatar} /> : null }
); } } export default Comment;