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

105 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-09-17 17:04:19 +02:00
import * as React from 'react';
2019-09-30 23:28:52 +02:00
import Gravatar from 'react-gravatar';
2019-09-17 17:04:19 +02:00
2019-09-18 13:40:00 +02:00
import NewComment from './NewComment';
2019-09-25 11:50:23 +02:00
import Separator from '../shared/Separator';
2019-09-17 17:04:19 +02:00
import { MutedText } from '../shared/CustomTexts';
import { ReplyFormState } from '../../reducers/replyFormReducer';
import friendlyDate from '../../helpers/friendlyDate';
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;
2019-09-18 13:40:00 +02:00
handleSubmitComment(body: string, parentId: number): 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">
2019-09-30 23:28:52 +02:00
<Gravatar email={userEmail} size={24} className="gravatar" />
2019-09-17 17:04:19 +02:00
<span className="commentAuthor">{userFullName}</span>
{ isPostUpdate ? <span className="postUpdateBadge">Post update</span> : null }
2019-09-17 17:04:19 +02:00
</div>
<p className="commentBody">{body}</p>
<div className="commentFooter">
<a className="commentReplyButton commentLink" onClick={handleToggleCommentReply}>
{ replyForm.isOpen ? 'Cancel' : 'Reply' }
</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 />
2019-10-02 16:53:35 +02:00
<a href={`/admin/comments/${id}/edit`} data-turbolinks="false">Edit</a>
<Separator />
<a
href={`/admin/comments/${id}`}
data-method="delete"
data-confirm="Are you sure?"
data-turbolinks="false">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}
isSubmitting={replyForm.isSubmitting}
error={replyForm.error}
2019-09-18 13:40:00 +02:00
handleChange={handleCommentReplyBodyChange}
handleSubmit={handleSubmitComment}
2019-09-20 18:43:24 +02:00
isLoggedIn={isLoggedIn}
2019-10-02 15:26:32 +02:00
userEmail={currentUserEmail}
/>
:
null
}
2019-09-17 17:04:19 +02:00
</div>
);
export default Comment;