Improve comments component

This commit is contained in:
riggraz
2019-09-20 18:43:24 +02:00
parent 0b88d58094
commit 38345f9c42
9 changed files with 64 additions and 14 deletions

View File

@@ -20,6 +20,8 @@ interface Props {
handleToggleCommentReply(): void; handleToggleCommentReply(): void;
handleCommentReplyBodyChange(e: FormEvent): void; handleCommentReplyBodyChange(e: FormEvent): void;
handleSubmitComment(body: string, parentId: number): void; handleSubmitComment(body: string, parentId: number): void;
isLoggedIn: boolean;
} }
const Comment = ({ const Comment = ({
@@ -34,6 +36,8 @@ const Comment = ({
handleToggleCommentReply, handleToggleCommentReply,
handleCommentReplyBodyChange, handleCommentReplyBodyChange,
handleSubmitComment, handleSubmitComment,
isLoggedIn,
}: Props) => ( }: Props) => (
<div className="comment"> <div className="comment">
<div className="commentHeader"> <div className="commentHeader">
@@ -52,8 +56,11 @@ const Comment = ({
<NewComment <NewComment
body={reply.body} body={reply.body}
parentId={id} parentId={id}
isSubmitting={reply.isSubmitting}
handleChange={handleCommentReplyBodyChange} handleChange={handleCommentReplyBodyChange}
handleSubmit={handleSubmitComment} handleSubmit={handleSubmitComment}
isLoggedIn={isLoggedIn}
/> />
: :
null null

View File

@@ -15,6 +15,8 @@ interface Props {
toggleCommentReply(commentId: number): void; toggleCommentReply(commentId: number): void;
setCommentReplyBody(commentId: number, body: string): void; setCommentReplyBody(commentId: number, body: string): void;
handleSubmitComment(body: string, parentId: number): void; handleSubmitComment(body: string, parentId: number): void;
isLoggedIn: boolean;
} }
const CommentList = ({ const CommentList = ({
@@ -26,6 +28,8 @@ const CommentList = ({
toggleCommentReply, toggleCommentReply,
setCommentReplyBody, setCommentReplyBody,
handleSubmitComment, handleSubmitComment,
isLoggedIn,
}: Props) => ( }: Props) => (
<React.Fragment> <React.Fragment>
{comments.map((comment, i) => { {comments.map((comment, i) => {
@@ -43,6 +47,8 @@ const CommentList = ({
} }
handleSubmitComment={handleSubmitComment} handleSubmitComment={handleSubmitComment}
{...comment} {...comment}
isLoggedIn={isLoggedIn}
/> />
<CommentList <CommentList
@@ -54,6 +60,8 @@ const CommentList = ({
toggleCommentReply={toggleCommentReply} toggleCommentReply={toggleCommentReply}
setCommentReplyBody={setCommentReplyBody} setCommentReplyBody={setCommentReplyBody}
handleSubmitComment={handleSubmitComment} handleSubmitComment={handleSubmitComment}
isLoggedIn={isLoggedIn}
/> />
</div> </div>
); );

View File

@@ -11,6 +11,7 @@ import { CommentRepliesState } from '../../reducers/commentRepliesReducer';
interface Props { interface Props {
postId: number; postId: number;
isLoggedIn: boolean;
authenticityToken: string; authenticityToken: string;
comments: Array<IComment>; comments: Array<IComment>;
@@ -45,6 +46,8 @@ class CommentsP extends React.Component<Props> {
render() { render() {
const { const {
isLoggedIn,
comments, comments,
replies, replies,
areLoading, areLoading,
@@ -55,17 +58,22 @@ class CommentsP extends React.Component<Props> {
submitComment, submitComment,
} = this.props; } = this.props;
const postReply = replies.find(reply => reply.commentId === -1);
return ( return (
<div className="commentsContainer"> <div className="commentsContainer">
<NewComment <NewComment
body={replies.find(reply => reply.commentId === -1) && replies.find(reply => reply.commentId === -1).body} body={postReply && postReply.body}
parentId={null} parentId={null}
isSubmitting={postReply && postReply.isSubmitting}
handleChange={ handleChange={
(e: FormEvent) => ( (e: FormEvent) => (
setCommentReplyBody(-1, (e.target as HTMLTextAreaElement).value) setCommentReplyBody(-1, (e.target as HTMLTextAreaElement).value)
) )
} }
handleSubmit={this._handleSubmitComment} handleSubmit={this._handleSubmitComment}
isLoggedIn={isLoggedIn}
/> />
{ areLoading ? <Spinner /> : null } { areLoading ? <Spinner /> : null }
@@ -83,6 +91,7 @@ class CommentsP extends React.Component<Props> {
handleSubmitComment={this._handleSubmitComment} handleSubmitComment={this._handleSubmitComment}
parentId={null} parentId={null}
level={1} level={1}
isLoggedIn={isLoggedIn}
/> />
</div> </div>
); );

View File

@@ -2,32 +2,46 @@ import * as React from 'react';
import { FormEvent } from 'react'; import { FormEvent } from 'react';
import Button from '../shared/Button'; import Button from '../shared/Button';
import Spinner from '../shared/Spinner';
interface Props { interface Props {
body: string; body: string;
parentId: number; parentId: number;
isSubmitting: boolean;
handleChange(e: FormEvent): void; handleChange(e: FormEvent): void;
handleSubmit(body: string, parentId: number): void; handleSubmit(body: string, parentId: number): void;
isLoggedIn: boolean;
} }
const NewComment = ({ const NewComment = ({
body, body,
parentId, parentId,
isSubmitting,
handleChange, handleChange,
handleSubmit, handleSubmit,
isLoggedIn,
}: Props) => ( }: Props) => (
<div className="newCommentForm"> <div className="newCommentForm">
<textarea {
value={body} isLoggedIn ?
onChange={handleChange} <React.Fragment>
placeholder="Leave a comment" <textarea
className="newCommentBody" value={body}
/> onChange={handleChange}
<Button placeholder="Leave a comment"
onClick={() => handleSubmit(body, parentId)} className="newCommentBody"
className="submitCommentButton"> />
Submit <Button
</Button> onClick={() => handleSubmit(body, parentId)}
className="submitCommentButton">
{ isSubmitting ? <Spinner /> : 'Submit' }
</Button>
</React.Fragment>
:
<a href="/users/sign_in" className="loginInfo">You need to log in to post comments.</a>
}
</div> </div>
); );

View File

@@ -36,6 +36,7 @@ class PostP extends React.Component<Props> {
post, post,
postStatuses, postStatuses,
isLoggedIn,
isPowerUser, isPowerUser,
authenticityToken, authenticityToken,
@@ -73,6 +74,7 @@ class PostP extends React.Component<Props> {
<Comments <Comments
postId={this.props.postId} postId={this.props.postId}
isLoggedIn={isLoggedIn}
authenticityToken={authenticityToken} authenticityToken={authenticityToken}
/> />
</div> </div>

View File

@@ -2,7 +2,7 @@ import * as React from 'react';
import { FormEvent } from 'react'; import { FormEvent } from 'react';
interface Props { interface Props {
children: string; children: any;
onClick(e: FormEvent): void; onClick(e: FormEvent): void;
className?: string; className?: string;
outline?: boolean; outline?: boolean;

View File

@@ -1,7 +1,7 @@
import * as React from 'react'; import * as React from 'react';
const Spinner = () => ( const Spinner = () => (
<div className="spinner-border d-block mx-auto" role="status"> <div className="spinner-grow d-block mx-auto" role="status">
<span className="sr-only">Loading...</span> <span className="sr-only">Loading...</span>
</div> </div>
); );

View File

@@ -1,3 +1,9 @@
a {
color: #333;
&:hover { color: inherit; }
}
.card { .card {
@extend .card; @extend .card;

View File

@@ -20,3 +20,7 @@
margin-bottom: auto; margin-bottom: auto;
margin-right: 4px; margin-right: 4px;
} }
.loginInfo {
text-decoration: underline;
}