mirror of
https://github.com/astuto/astuto.git
synced 2025-12-16 03:37:56 +01:00
Add comment replies toggle and body to state
This commit is contained in:
27
app/javascript/actions/handleCommentReplies.ts
Normal file
27
app/javascript/actions/handleCommentReplies.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
export const TOGGLE_COMMENT_REPLY = 'TOGGLE_COMMENT_REPLY';
|
||||||
|
interface ToggleCommentReplyAction {
|
||||||
|
type: typeof TOGGLE_COMMENT_REPLY;
|
||||||
|
commentId: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const SET_COMMENT_REPLY_BODY = 'SET_COMMENT_REPLY_BODY';
|
||||||
|
interface SetCommentReplyBodyAction {
|
||||||
|
type: typeof SET_COMMENT_REPLY_BODY;
|
||||||
|
commentId: number;
|
||||||
|
body: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const toggleCommentReply = (commentId): ToggleCommentReplyAction => ({
|
||||||
|
type: TOGGLE_COMMENT_REPLY,
|
||||||
|
commentId,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const setCommentReplyBody = (commentId, body): SetCommentReplyBodyAction => ({
|
||||||
|
type: SET_COMMENT_REPLY_BODY,
|
||||||
|
commentId,
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
|
||||||
|
export type HandleCommentRepliesType =
|
||||||
|
ToggleCommentReplyAction |
|
||||||
|
SetCommentReplyBodyAction;
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
import { FormEvent } from 'react';
|
||||||
|
|
||||||
import { MutedText } from '../shared/CustomTexts';
|
import { MutedText } from '../shared/CustomTexts';
|
||||||
|
|
||||||
|
import { CommentRepliesState } from '../../reducers/commentRepliesReducer';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
id: number;
|
id: number;
|
||||||
body: string;
|
body: string;
|
||||||
@@ -10,6 +13,9 @@ interface Props {
|
|||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
|
|
||||||
level: number;
|
level: number;
|
||||||
|
reply: CommentRepliesState;
|
||||||
|
handleToggleCommentReply(): void;
|
||||||
|
handleCommentReplyBodyChange(e: FormEvent): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Comment = ({
|
const Comment = ({
|
||||||
@@ -20,6 +26,9 @@ const Comment = ({
|
|||||||
updatedAt,
|
updatedAt,
|
||||||
|
|
||||||
level,
|
level,
|
||||||
|
reply,
|
||||||
|
handleToggleCommentReply,
|
||||||
|
handleCommentReplyBodyChange,
|
||||||
}: Props) => (
|
}: Props) => (
|
||||||
<div className="comment">
|
<div className="comment">
|
||||||
<div className="commentHeader">
|
<div className="commentHeader">
|
||||||
@@ -27,9 +36,18 @@ const Comment = ({
|
|||||||
</div>
|
</div>
|
||||||
<p className="commentBody">{body}</p>
|
<p className="commentBody">{body}</p>
|
||||||
<div className="commentFooter">
|
<div className="commentFooter">
|
||||||
<a href="#">Reply</a>
|
<a onClick={handleToggleCommentReply}>Reply</a>
|
||||||
<MutedText>{updatedAt}</MutedText>
|
<MutedText>{updatedAt}</MutedText>
|
||||||
</div>
|
</div>
|
||||||
|
{
|
||||||
|
reply.isOpen ?
|
||||||
|
<textarea
|
||||||
|
value={reply.body}
|
||||||
|
onChange={handleCommentReplyBodyChange}
|
||||||
|
/>
|
||||||
|
:
|
||||||
|
null
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +1,55 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
import { FormEvent } from 'react';
|
||||||
|
|
||||||
import Comment from './Comment';
|
import Comment from './Comment';
|
||||||
|
|
||||||
import IComment from '../../interfaces/IComment';
|
import IComment from '../../interfaces/IComment';
|
||||||
|
import { CommentRepliesState } from '../../reducers/commentRepliesReducer';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
comments: Array<IComment>;
|
comments: Array<IComment>;
|
||||||
|
replies: Array<CommentRepliesState>;
|
||||||
parentId: number;
|
parentId: number;
|
||||||
level: number;
|
level: number;
|
||||||
|
|
||||||
|
toggleCommentReply(commentId: number);
|
||||||
|
setCommentReplyBody(commentId: number, body: string);
|
||||||
}
|
}
|
||||||
|
|
||||||
const CommentList = ({ comments, parentId, level }: Props) => (
|
const CommentList = ({
|
||||||
|
comments,
|
||||||
|
replies,
|
||||||
|
parentId,
|
||||||
|
level,
|
||||||
|
|
||||||
|
toggleCommentReply,
|
||||||
|
setCommentReplyBody,
|
||||||
|
}: Props) => (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
{comments.map((comment, i) => {
|
{comments.map((comment, i) => {
|
||||||
if (comment.parentId === parentId) {
|
if (comment.parentId === parentId) {
|
||||||
return (
|
return (
|
||||||
<div className="commentList">
|
<div className="commentList">
|
||||||
<Comment level={level} {...comment} />
|
<Comment
|
||||||
|
level={level}
|
||||||
|
reply={replies.find(reply => reply.commentId === comment.id)}
|
||||||
|
handleToggleCommentReply={() => toggleCommentReply(comment.id)}
|
||||||
|
handleCommentReplyBodyChange={
|
||||||
|
(e: FormEvent) => (
|
||||||
|
setCommentReplyBody(comment.id, (e.target as HTMLTextAreaElement).value)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
{...comment}
|
||||||
|
/>
|
||||||
|
|
||||||
<CommentList
|
<CommentList
|
||||||
comments={comments}
|
comments={comments}
|
||||||
|
replies={replies}
|
||||||
parentId={comment.id}
|
parentId={comment.id}
|
||||||
level={level+1}
|
level={level+1}
|
||||||
|
|
||||||
|
toggleCommentReply={toggleCommentReply}
|
||||||
|
setCommentReplyBody={setCommentReplyBody}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -5,15 +5,19 @@ import Spinner from '../shared/Spinner';
|
|||||||
import { DangerText } from '../shared/CustomTexts';
|
import { DangerText } from '../shared/CustomTexts';
|
||||||
|
|
||||||
import IComment from '../../interfaces/IComment';
|
import IComment from '../../interfaces/IComment';
|
||||||
|
import { CommentRepliesState } from '../../reducers/commentRepliesReducer';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
postId: number;
|
postId: number;
|
||||||
|
|
||||||
comments: Array<IComment>;
|
comments: Array<IComment>;
|
||||||
|
replies: Array<CommentRepliesState>;
|
||||||
areLoading: boolean;
|
areLoading: boolean;
|
||||||
error: string;
|
error: string;
|
||||||
|
|
||||||
requestComments(postId: number, page?: number);
|
requestComments(postId: number, page?: number);
|
||||||
|
toggleCommentReply(commentId: number);
|
||||||
|
setCommentReplyBody(commentId: number, body: string);
|
||||||
}
|
}
|
||||||
|
|
||||||
class CommentsP extends React.Component<Props> {
|
class CommentsP extends React.Component<Props> {
|
||||||
@@ -24,8 +28,12 @@ class CommentsP extends React.Component<Props> {
|
|||||||
render() {
|
render() {
|
||||||
const {
|
const {
|
||||||
comments,
|
comments,
|
||||||
|
replies,
|
||||||
areLoading,
|
areLoading,
|
||||||
error,
|
error,
|
||||||
|
|
||||||
|
toggleCommentReply,
|
||||||
|
setCommentReplyBody,
|
||||||
} = this.props;
|
} = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -37,6 +45,9 @@ class CommentsP extends React.Component<Props> {
|
|||||||
|
|
||||||
<CommentList
|
<CommentList
|
||||||
comments={comments}
|
comments={comments}
|
||||||
|
replies={replies}
|
||||||
|
toggleCommentReply={toggleCommentReply}
|
||||||
|
setCommentReplyBody={setCommentReplyBody}
|
||||||
parentId={null}
|
parentId={null}
|
||||||
level={1}
|
level={1}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
|
|
||||||
import { requestComments } from '../actions/requestComments';
|
import { requestComments } from '../actions/requestComments';
|
||||||
|
import {
|
||||||
|
toggleCommentReply,
|
||||||
|
setCommentReplyBody,
|
||||||
|
} from '../actions/handleCommentReplies';
|
||||||
|
|
||||||
import { State } from '../reducers/rootReducer';
|
import { State } from '../reducers/rootReducer';
|
||||||
|
|
||||||
@@ -8,6 +12,7 @@ import CommentsP from '../components/Comments/CommentsP';
|
|||||||
|
|
||||||
const mapStateToProps = (state: State) => ({
|
const mapStateToProps = (state: State) => ({
|
||||||
comments: state.currentPost.comments.items,
|
comments: state.currentPost.comments.items,
|
||||||
|
replies: state.currentPost.comments.replies,
|
||||||
areLoading: state.currentPost.comments.areLoading,
|
areLoading: state.currentPost.comments.areLoading,
|
||||||
error: state.currentPost.comments.error,
|
error: state.currentPost.comments.error,
|
||||||
});
|
});
|
||||||
@@ -16,6 +21,14 @@ const mapDispatchToProps = (dispatch) => ({
|
|||||||
requestComments(postId: number) {
|
requestComments(postId: number) {
|
||||||
dispatch(requestComments(postId));
|
dispatch(requestComments(postId));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
toggleCommentReply(commentId: number) {
|
||||||
|
dispatch(toggleCommentReply(commentId));
|
||||||
|
},
|
||||||
|
|
||||||
|
setCommentReplyBody(commentId: number, body: string) {
|
||||||
|
dispatch(setCommentReplyBody(commentId, body));
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default connect(
|
export default connect(
|
||||||
|
|||||||
50
app/javascript/reducers/commentRepliesReducer.ts
Normal file
50
app/javascript/reducers/commentRepliesReducer.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import {
|
||||||
|
COMMENT_REQUEST_SUCCESS,
|
||||||
|
} from '../actions/requestComment';
|
||||||
|
import {
|
||||||
|
HandleCommentRepliesType,
|
||||||
|
TOGGLE_COMMENT_REPLY,
|
||||||
|
SET_COMMENT_REPLY_BODY,
|
||||||
|
} from '../actions/handleCommentReplies';
|
||||||
|
|
||||||
|
export interface CommentRepliesState {
|
||||||
|
commentId: number;
|
||||||
|
isOpen: boolean;
|
||||||
|
body: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialState: CommentRepliesState = {
|
||||||
|
commentId: undefined,
|
||||||
|
isOpen: false,
|
||||||
|
body: '',
|
||||||
|
}
|
||||||
|
|
||||||
|
const commentRepliesReducer = (
|
||||||
|
state = initialState,
|
||||||
|
action,
|
||||||
|
) => {
|
||||||
|
switch (action.type) {
|
||||||
|
case COMMENT_REQUEST_SUCCESS:
|
||||||
|
return {
|
||||||
|
...initialState,
|
||||||
|
commentId: action.comment.id,
|
||||||
|
};
|
||||||
|
|
||||||
|
case TOGGLE_COMMENT_REPLY:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
isOpen: !state.isOpen,
|
||||||
|
};
|
||||||
|
|
||||||
|
case SET_COMMENT_REPLY_BODY:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
body: action.body,
|
||||||
|
};
|
||||||
|
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default commentRepliesReducer;
|
||||||
@@ -4,20 +4,31 @@ import {
|
|||||||
COMMENTS_REQUEST_SUCCESS,
|
COMMENTS_REQUEST_SUCCESS,
|
||||||
COMMENTS_REQUEST_FAILURE,
|
COMMENTS_REQUEST_FAILURE,
|
||||||
} from '../actions/requestComments';
|
} from '../actions/requestComments';
|
||||||
|
|
||||||
import { commentRequestSuccess } from '../actions/requestComment';
|
import { commentRequestSuccess } from '../actions/requestComment';
|
||||||
|
|
||||||
|
import {
|
||||||
|
HandleCommentRepliesType,
|
||||||
|
TOGGLE_COMMENT_REPLY,
|
||||||
|
SET_COMMENT_REPLY_BODY,
|
||||||
|
} from '../actions/handleCommentReplies';
|
||||||
|
|
||||||
import commentReducer from './commentReducer';
|
import commentReducer from './commentReducer';
|
||||||
|
import commentRepliesReducer from './commentRepliesReducer';
|
||||||
|
|
||||||
import IComment from '../interfaces/IComment';
|
import IComment from '../interfaces/IComment';
|
||||||
|
import { CommentRepliesState } from './commentRepliesReducer';
|
||||||
|
|
||||||
export interface CommentsState {
|
export interface CommentsState {
|
||||||
items: Array<IComment>;
|
items: Array<IComment>;
|
||||||
|
replies: Array<CommentRepliesState>;
|
||||||
areLoading: boolean;
|
areLoading: boolean;
|
||||||
error: string;
|
error: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialState: CommentsState = {
|
const initialState: CommentsState = {
|
||||||
items: [],
|
items: [],
|
||||||
|
replies: [],
|
||||||
areLoading: false,
|
areLoading: false,
|
||||||
error: '',
|
error: '',
|
||||||
};
|
};
|
||||||
@@ -39,6 +50,9 @@ const commentsReducer = (
|
|||||||
items: action.comments.map(
|
items: action.comments.map(
|
||||||
comment => commentReducer(undefined, commentRequestSuccess(comment))
|
comment => commentReducer(undefined, commentRequestSuccess(comment))
|
||||||
),
|
),
|
||||||
|
replies: action.comments.map(
|
||||||
|
comment => commentRepliesReducer(undefined, commentRequestSuccess(comment))
|
||||||
|
),
|
||||||
areLoading: false,
|
areLoading: false,
|
||||||
error: '',
|
error: '',
|
||||||
};
|
};
|
||||||
@@ -50,6 +64,20 @@ const commentsReducer = (
|
|||||||
error: action.error,
|
error: action.error,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
case TOGGLE_COMMENT_REPLY:
|
||||||
|
case SET_COMMENT_REPLY_BODY:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
replies: state.replies.map(
|
||||||
|
reply => (
|
||||||
|
reply.commentId === action.commentId ?
|
||||||
|
commentRepliesReducer(reply, action)
|
||||||
|
:
|
||||||
|
reply
|
||||||
|
)
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ import {
|
|||||||
COMMENTS_REQUEST_FAILURE,
|
COMMENTS_REQUEST_FAILURE,
|
||||||
} from '../actions/requestComments';
|
} from '../actions/requestComments';
|
||||||
|
|
||||||
|
import {
|
||||||
|
HandleCommentRepliesType,
|
||||||
|
TOGGLE_COMMENT_REPLY,
|
||||||
|
SET_COMMENT_REPLY_BODY,
|
||||||
|
} from '../actions/handleCommentReplies';
|
||||||
|
|
||||||
import postReducer from './postReducer';
|
import postReducer from './postReducer';
|
||||||
import commentsReducer from './commentsReducer';
|
import commentsReducer from './commentsReducer';
|
||||||
|
|
||||||
@@ -40,7 +46,11 @@ const initialState: CurrentPostState = {
|
|||||||
|
|
||||||
const currentPostReducer = (
|
const currentPostReducer = (
|
||||||
state = initialState,
|
state = initialState,
|
||||||
action: PostRequestActionTypes | ChangePostStatusSuccessAction | CommentsRequestActionTypes,
|
action:
|
||||||
|
PostRequestActionTypes |
|
||||||
|
ChangePostStatusSuccessAction |
|
||||||
|
CommentsRequestActionTypes |
|
||||||
|
HandleCommentRepliesType
|
||||||
): CurrentPostState => {
|
): CurrentPostState => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case POST_REQUEST_START:
|
case POST_REQUEST_START:
|
||||||
@@ -73,6 +83,8 @@ const currentPostReducer = (
|
|||||||
case COMMENTS_REQUEST_START:
|
case COMMENTS_REQUEST_START:
|
||||||
case COMMENTS_REQUEST_SUCCESS:
|
case COMMENTS_REQUEST_SUCCESS:
|
||||||
case COMMENTS_REQUEST_FAILURE:
|
case COMMENTS_REQUEST_FAILURE:
|
||||||
|
case TOGGLE_COMMENT_REPLY:
|
||||||
|
case SET_COMMENT_REPLY_BODY:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
comments: commentsReducer(state.comments, action),
|
comments: commentsReducer(state.comments, action),
|
||||||
|
|||||||
Reference in New Issue
Block a user