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

135 lines
3.5 KiB
TypeScript
Raw 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';
2019-09-30 23:28:52 +02:00
import Gravatar from 'react-gravatar';
2022-06-06 20:20:03 +02:00
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';
import Separator from '../common/Separator';
import { MutedText } from '../common/CustomTexts';
2019-09-17 17:04:19 +02:00
import { ReplyFormState } from '../../reducers/replyFormReducer';
import friendlyDate from '../../helpers/datetime';
2019-09-17 17:04:19 +02:00
interface Props {
id: number;
body: string;
isPostUpdate: boolean;
2019-09-17 17:04:19 +02:00
userFullName: string;
2019-09-30 23:28:52 +02:00
userEmail: string;
2019-09-17 17:04:19 +02:00
updatedAt: string;
replyForm: ReplyFormState;
handleToggleCommentReply(): void;
handleCommentReplyBodyChange(e: React.FormEvent): void;
handleToggleIsCommentUpdate(commentId: number, currentIsPostUpdate: boolean): void;
handleSubmitComment(body: string, parentId: number, isPostUpdate: boolean): 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
currentUserEmail: string;
2019-09-17 17:04:19 +02:00
}
const Comment = ({
id,
body,
isPostUpdate,
2019-09-17 17:04:19 +02:00
userFullName,
2019-09-30 23:28:52 +02:00
userEmail,
2019-09-17 17:04:19 +02:00
updatedAt,
replyForm,
handleToggleCommentReply,
handleCommentReplyBodyChange,
handleToggleIsCommentUpdate,
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
currentUserEmail,
2019-09-17 17:04:19 +02:00
}: Props) => (
<div className="comment">
<div className="commentHeader">
<Gravatar email={userEmail} size={28} className="gravatar" />
2019-09-17 17:04:19 +02:00
<span className="commentAuthor">{userFullName}</span>
{
isPostUpdate ?
<span className="postUpdateBadge">
{I18n.t('post.comments.post_update_badge')}
</span>
:
null
}
2019-09-17 17:04:19 +02:00
</div>
2022-06-06 20:20:03 +02:00
<ReactMarkdown
className="commentBody"
disallowedTypes={['heading', 'image', 'html']}
unwrapDisallowed
>
{body}
</ReactMarkdown>
2019-09-17 17:04:19 +02:00
<div className="commentFooter">
<a className="commentReplyButton commentLink" onClick={handleToggleCommentReply}>
{
replyForm.isOpen ?
I18n.t('common.buttons.cancel')
:
I18n.t('post.comments.reply_button')
}
</a>
2019-09-25 11:50:23 +02:00
{
isPowerUser ?
<React.Fragment>
<Separator />
<a
onClick={() => handleToggleIsCommentUpdate(id, isPostUpdate)}
className="commentLink"
>
2019-10-02 16:53:35 +02:00
{ 'Post update: ' + (isPostUpdate ? 'yes' : 'no') }
</a>
2019-09-25 11:50:23 +02:00
<Separator />
<a href={`/admin/comments/${id}/edit`} className="commentLink" data-turbolinks="false">
{I18n.t('common.buttons.edit')}
</a>
2019-10-02 16:53:35 +02:00
<Separator />
<a
href={`/admin/comments/${id}`}
className="commentLink"
2019-10-02 16:53:35 +02:00
data-method="delete"
data-confirm="Are you sure?"
data-turbolinks="false">
{I18n.t('common.buttons.delete')}
</a>
2019-09-25 11:50:23 +02:00
</React.Fragment>
:
null
}
<Separator />
<MutedText>{friendlyDate(updatedAt)}</MutedText>
2019-09-17 17:04:19 +02:00
</div>
{
replyForm.isOpen ?
2019-09-18 13:40:00 +02:00
<NewComment
body={replyForm.body}
2019-09-18 13:40:00 +02:00
parentId={id}
postUpdateFlagValue={replyForm.isPostUpdate}
isSubmitting={replyForm.isSubmitting}
error={replyForm.error}
2019-09-18 13:40:00 +02:00
handleChange={handleCommentReplyBodyChange}
handlePostUpdateFlag={() => null}
2019-09-18 13:40:00 +02:00
handleSubmit={handleSubmitComment}
2019-09-20 18:43:24 +02:00
isLoggedIn={isLoggedIn}
isPowerUser={isPowerUser}
2019-10-02 15:26:32 +02:00
userEmail={currentUserEmail}
/>
:
null
}
2019-09-17 17:04:19 +02:00
</div>
);
export default Comment;