mirror of
https://github.com/astuto/astuto.git
synced 2025-12-15 11:17:49 +01:00
Basic comments visualisation
This commit is contained in:
@@ -5,7 +5,7 @@ class CommentsController < ApplicationController
|
|||||||
comments = Comment
|
comments = Comment
|
||||||
.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.updated_at, users.full_name as user_full_name')
|
.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
|
||||||
|
|||||||
36
app/javascript/components/Comments/Comment.tsx
Normal file
36
app/javascript/components/Comments/Comment.tsx
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import * as React from 'react';
|
||||||
|
|
||||||
|
import { MutedText } from '../shared/CustomTexts';
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
id: number;
|
||||||
|
body: string;
|
||||||
|
parentId: number;
|
||||||
|
userFullName: string;
|
||||||
|
updatedAt: string;
|
||||||
|
|
||||||
|
level: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Comment = ({
|
||||||
|
id,
|
||||||
|
body,
|
||||||
|
parentId,
|
||||||
|
userFullName,
|
||||||
|
updatedAt,
|
||||||
|
|
||||||
|
level,
|
||||||
|
}: Props) => (
|
||||||
|
<div className="comment">
|
||||||
|
<div className="commentHeader">
|
||||||
|
<span className="commentAuthor">{userFullName}</span>
|
||||||
|
</div>
|
||||||
|
<p className="commentBody">{body}</p>
|
||||||
|
<div className="commentFooter">
|
||||||
|
<a href="#">Reply</a>
|
||||||
|
<MutedText>{updatedAt}</MutedText>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
export default Comment;
|
||||||
33
app/javascript/components/Comments/CommentList.tsx
Normal file
33
app/javascript/components/Comments/CommentList.tsx
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import * as React from 'react';
|
||||||
|
|
||||||
|
import Comment from './Comment';
|
||||||
|
|
||||||
|
import IComment from '../../interfaces/IComment';
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
comments: Array<IComment>;
|
||||||
|
parentId: number;
|
||||||
|
level: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const CommentList = ({ comments, parentId, level }: Props) => (
|
||||||
|
<React.Fragment>
|
||||||
|
{comments.map((comment, i) => {
|
||||||
|
if (comment.parentId === parentId) {
|
||||||
|
return (
|
||||||
|
<div className="commentList">
|
||||||
|
<Comment level={level} {...comment} />
|
||||||
|
|
||||||
|
<CommentList
|
||||||
|
comments={comments}
|
||||||
|
parentId={comment.id}
|
||||||
|
level={level+1}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
} else return null;
|
||||||
|
})}
|
||||||
|
</React.Fragment>
|
||||||
|
);
|
||||||
|
|
||||||
|
export default CommentList;
|
||||||
@@ -1,5 +1,9 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
|
||||||
|
import CommentList from './CommentList';
|
||||||
|
import Spinner from '../shared/Spinner';
|
||||||
|
import { DangerText } from '../shared/CustomTexts';
|
||||||
|
|
||||||
import IComment from '../../interfaces/IComment';
|
import IComment from '../../interfaces/IComment';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -25,10 +29,17 @@ class CommentsP extends React.Component<Props> {
|
|||||||
} = this.props;
|
} = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div className="comments">
|
||||||
{comments.map((comment, i) => (
|
<h2>Comments</h2>
|
||||||
<div key={i}>{comment.body}</div>
|
|
||||||
))}
|
{ areLoading ? <Spinner /> : null }
|
||||||
|
{ error ? <DangerText>{error}</DangerText> : null }
|
||||||
|
|
||||||
|
<CommentList
|
||||||
|
comments={comments}
|
||||||
|
parentId={null}
|
||||||
|
level={1}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
interface IComment {
|
interface IComment {
|
||||||
id: number;
|
id: number;
|
||||||
body: string;
|
body: string;
|
||||||
|
parentId: number;
|
||||||
userFullName: string;
|
userFullName: string;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
interface ICommentJSON {
|
interface ICommentJSON {
|
||||||
id: number;
|
id: number;
|
||||||
body: string;
|
body: string;
|
||||||
|
parent_id: number;
|
||||||
user_full_name: string;
|
user_full_name: string;
|
||||||
updated_at: string;
|
updated_at: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ import IComment from '../interfaces/IComment';
|
|||||||
const initialState: IComment = {
|
const initialState: IComment = {
|
||||||
id: 0,
|
id: 0,
|
||||||
body: '',
|
body: '',
|
||||||
userFullName: '',
|
parentId: null,
|
||||||
updatedAt: '',
|
userFullName: '<Unknown user>',
|
||||||
|
updatedAt: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
const commentReducer = (
|
const commentReducer = (
|
||||||
@@ -20,6 +21,7 @@ const commentReducer = (
|
|||||||
return {
|
return {
|
||||||
id: action.comment.id,
|
id: action.comment.id,
|
||||||
body: action.comment.body,
|
body: action.comment.body,
|
||||||
|
parentId: action.comment.parent_id,
|
||||||
userFullName: action.comment.user_full_name,
|
userFullName: action.comment.user_full_name,
|
||||||
updatedAt: action.comment.updated_at,
|
updatedAt: action.comment.updated_at,
|
||||||
};
|
};
|
||||||
|
|||||||
21
app/javascript/stylesheets/components/Comments.scss
Normal file
21
app/javascript/stylesheets/components/Comments.scss
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
.comments {
|
||||||
|
.commentList > .commentList {
|
||||||
|
padding-left: 32px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.comment {
|
||||||
|
margin-bottom: 32px;
|
||||||
|
|
||||||
|
.commentHeader {
|
||||||
|
@extend
|
||||||
|
.font-weight-bolder;
|
||||||
|
|
||||||
|
font-size: 17px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.commentBody {
|
||||||
|
@extend
|
||||||
|
.my-2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,11 +6,8 @@
|
|||||||
@import 'general/index';
|
@import 'general/index';
|
||||||
@import 'general/navbar';
|
@import 'general/navbar';
|
||||||
|
|
||||||
/*
|
/* Components */
|
||||||
Components stylesheets are not imported here. Instead,
|
|
||||||
they are imported in the specific React components
|
|
||||||
that use them
|
|
||||||
*/
|
|
||||||
@import 'components/Board';
|
@import 'components/Board';
|
||||||
|
@import 'components/Comments';
|
||||||
@import 'components/Post';
|
@import 'components/Post';
|
||||||
@import 'components/Roadmap';
|
@import 'components/Roadmap';
|
||||||
Reference in New Issue
Block a user