Add gravatar picture to comments

This commit is contained in:
riggraz
2019-09-30 23:28:52 +02:00
parent 4fdb71d488
commit be8f003d70
6 changed files with 25 additions and 3 deletions

View File

@@ -3,9 +3,16 @@ class CommentsController < ApplicationController
def index def index
comments = Comment comments = Comment
.select(
:id,
:body,
:parent_id,
:updated_at,
'users.full_name as user_full_name',
'users.email as user_email',
)
.where(post_id: params[:post_id]) .where(post_id: params[:post_id])
.left_outer_joins(:user) .left_outer_joins(:user)
.select('comments.id, comments.body, comments.parent_id, comments.updated_at, users.full_name as user_full_name')
.order(updated_at: :desc) .order(updated_at: :desc)
render json: comments render json: comments
@@ -15,8 +22,9 @@ class CommentsController < ApplicationController
comment = Comment.new(comment_params) comment = Comment.new(comment_params)
if comment.save if comment.save
render json: comment.attributes.merge({ user_full_name: current_user.full_name}), render json: comment.attributes.merge(
status: :created { user_full_name: current_user.full_name, user_email: current_user.email}
), status: :created
else else
render json: { render json: {
error: I18n.t('errors.comment.create', message: comment.errors.full_messages) error: I18n.t('errors.comment.create', message: comment.errors.full_messages)

View File

@@ -1,4 +1,5 @@
import * as React from 'react'; import * as React from 'react';
import Gravatar from 'react-gravatar';
import NewComment from './NewComment'; import NewComment from './NewComment';
import Separator from '../shared/Separator'; import Separator from '../shared/Separator';
@@ -12,6 +13,7 @@ interface Props {
id: number; id: number;
body: string; body: string;
userFullName: string; userFullName: string;
userEmail: string;
updatedAt: string; updatedAt: string;
replyForm: ReplyFormState; replyForm: ReplyFormState;
@@ -27,6 +29,7 @@ const Comment = ({
id, id,
body, body,
userFullName, userFullName,
userEmail,
updatedAt, updatedAt,
replyForm, replyForm,
@@ -39,6 +42,7 @@ const Comment = ({
}: Props) => ( }: Props) => (
<div className="comment"> <div className="comment">
<div className="commentHeader"> <div className="commentHeader">
<Gravatar email={userEmail} size={24} className="gravatar" />
<span className="commentAuthor">{userFullName}</span> <span className="commentAuthor">{userFullName}</span>
</div> </div>
<p className="commentBody">{body}</p> <p className="commentBody">{body}</p>

View File

@@ -3,6 +3,7 @@ interface IComment {
body: string; body: string;
parentId: number; parentId: number;
userFullName: string; userFullName: string;
userEmail: string;
updatedAt: string; updatedAt: string;
} }

View File

@@ -3,6 +3,7 @@ interface ICommentJSON {
body: string; body: string;
parent_id: number; parent_id: number;
user_full_name: string; user_full_name: string;
user_email: string;
updated_at: string; updated_at: string;
} }

View File

@@ -10,6 +10,7 @@ const initialState: IComment = {
body: '', body: '',
parentId: null, parentId: null,
userFullName: '<Unknown user>', userFullName: '<Unknown user>',
userEmail: 'example@example.com',
updatedAt: undefined, updatedAt: undefined,
}; };
@@ -24,6 +25,7 @@ const commentReducer = (
body: action.comment.body, body: action.comment.body,
parentId: action.comment.parent_id, parentId: action.comment.parent_id,
userFullName: action.comment.user_full_name, userFullName: action.comment.user_full_name,
userEmail: action.comment.user_email,
updatedAt: action.comment.updated_at, updatedAt: action.comment.updated_at,
}; };

View File

@@ -45,6 +45,12 @@
.commentHeader { .commentHeader {
@extend .titleText; @extend .titleText;
.commentAuthor {
@extend .ml-2;
vertical-align: middle;
}
} }
.commentBody { .commentBody {