diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 7ae65358..4a8ab393 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -15,7 +15,8 @@ class CommentsController < ApplicationController comment = Comment.new(comment_params) if comment.save - render json: comment, status: :created + render json: comment.attributes.merge({ user_full_name: current_user.full_name}), + status: :created else render json: { error: I18n.t('errors.comment.create', message: comment.errors.full_messages) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index b1c8144a..d153bc09 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -3,7 +3,9 @@ class PostsController < ApplicationController def index posts = Post - .select(:id, :title, :description, :post_status_id) + .select('posts.id, title, description, post_status_id, COUNT(comments.id) as comments_count') + .left_outer_joins(:comments) + .group('posts.id') .where(filter_params) .search_by_name_or_description(params[:search]) .page(params[:page]) @@ -16,7 +18,7 @@ class PostsController < ApplicationController post = Post.new(post_params) if post.save - render json: post, status: :no_content + render json: post, status: :created else render json: { error: I18n.t('errors.post.create', message: post.errors.full_messages) diff --git a/app/javascript/components/Board/NewPost.tsx b/app/javascript/components/Board/NewPost.tsx index 7a688e7f..6ed9c499 100644 --- a/app/javascript/components/Board/NewPost.tsx +++ b/app/javascript/components/Board/NewPost.tsx @@ -92,7 +92,7 @@ class NewPost extends React.Component { } try { - let res = await fetch('/posts', { + const res = await fetch('/posts', { method: 'POST', headers: { Accept: 'application/json', @@ -107,18 +107,22 @@ class NewPost extends React.Component { }, }), }); + const json = await res.json(); this.setState({isLoading: false}); - if (res.status === 204) { + if (res.status === 201) { this.setState({ - success: 'Your post has been published!', + success: 'Post published! You will be redirected soon...', title: '', description: '', }); + + setTimeout(() => ( + window.location.href = `/posts/${json.id}` + ), 1000); } else { - let data = await res.json(); - this.setState({error: data.error}); + this.setState({error: json.error}); } } catch (e) { diff --git a/app/javascript/components/Board/PostList.tsx b/app/javascript/components/Board/PostList.tsx index 88c3896c..261a1bd5 100644 --- a/app/javascript/components/Board/PostList.tsx +++ b/app/javascript/components/Board/PostList.tsx @@ -50,6 +50,7 @@ const PostList = ({ title={post.title} description={post.description} postStatus={postStatuses.find(postStatus => postStatus.id === post.postStatusId)} + commentsCount={post.commentsCount} key={i} /> diff --git a/app/javascript/components/Board/PostListItem.tsx b/app/javascript/components/Board/PostListItem.tsx index e2a77b38..6487e2a5 100644 --- a/app/javascript/components/Board/PostListItem.tsx +++ b/app/javascript/components/Board/PostListItem.tsx @@ -11,16 +11,17 @@ interface Props { title: string; description?: string; postStatus: IPostStatus; + commentsCount: number; } -const PostListItem = ({ id, title, description, postStatus}: Props) => ( +const PostListItem = ({ id, title, description, postStatus, commentsCount }: Props) => (
{title} {description}
- + { postStatus ? : null }
diff --git a/app/javascript/components/Comments/NewComment.tsx b/app/javascript/components/Comments/NewComment.tsx index f6f230b1..e50f3043 100644 --- a/app/javascript/components/Comments/NewComment.tsx +++ b/app/javascript/components/Comments/NewComment.tsx @@ -31,6 +31,7 @@ const NewComment = ({ value={body} onChange={handleChange} placeholder="Leave a comment" + autoFocus className="newCommentBody" />