mirror of
https://github.com/astuto/astuto.git
synced 2025-12-15 11:17:49 +01:00
Add button to toggle comment is post update
This commit is contained in:
45
app/javascript/actions/updateComment.ts
Normal file
45
app/javascript/actions/updateComment.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { ThunkAction } from "redux-thunk";
|
||||
import { State } from "../reducers/rootReducer";
|
||||
import { Action } from "redux";
|
||||
|
||||
export const TOGGLE_COMMENT_IS_UPDATE_SUCCESS = 'TOGGLE_COMMENT_IS_UPDATE_SUCCESS';
|
||||
export interface ToggleIsUpdateSuccessAction {
|
||||
type: typeof TOGGLE_COMMENT_IS_UPDATE_SUCCESS;
|
||||
commentId: number;
|
||||
}
|
||||
|
||||
const toggleIsUpdateSuccess = (
|
||||
commentId: number,
|
||||
): ToggleIsUpdateSuccessAction => ({
|
||||
type: TOGGLE_COMMENT_IS_UPDATE_SUCCESS,
|
||||
commentId,
|
||||
});
|
||||
|
||||
export const toggleCommentIsUpdate = (
|
||||
postId: number,
|
||||
commentId: number,
|
||||
currentIsPostUpdate: boolean,
|
||||
authenticityToken: string,
|
||||
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
|
||||
try {
|
||||
const response = await fetch(`/posts/${postId}/comments/${commentId}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-Token': authenticityToken,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
comment: {
|
||||
is_post_update: !currentIsPostUpdate,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
if (response.status === 200) {
|
||||
dispatch(toggleIsUpdateSuccess(commentId));
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import friendlyDate from '../../helpers/friendlyDate';
|
||||
interface Props {
|
||||
id: number;
|
||||
body: string;
|
||||
isPostUpdate: boolean;
|
||||
userFullName: string;
|
||||
userEmail: string;
|
||||
updatedAt: string;
|
||||
@@ -19,6 +20,7 @@ interface Props {
|
||||
replyForm: ReplyFormState;
|
||||
handleToggleCommentReply(): void;
|
||||
handleCommentReplyBodyChange(e: React.FormEvent): void;
|
||||
handleToggleIsCommentUpdate(commentId: number, currentIsPostUpdate: boolean): void;
|
||||
handleSubmitComment(body: string, parentId: number): void;
|
||||
|
||||
isLoggedIn: boolean;
|
||||
@@ -29,6 +31,7 @@ interface Props {
|
||||
const Comment = ({
|
||||
id,
|
||||
body,
|
||||
isPostUpdate,
|
||||
userFullName,
|
||||
userEmail,
|
||||
updatedAt,
|
||||
@@ -36,6 +39,7 @@ const Comment = ({
|
||||
replyForm,
|
||||
handleToggleCommentReply,
|
||||
handleCommentReplyBodyChange,
|
||||
handleToggleIsCommentUpdate,
|
||||
handleSubmitComment,
|
||||
|
||||
isLoggedIn,
|
||||
@@ -46,17 +50,26 @@ const Comment = ({
|
||||
<div className="commentHeader">
|
||||
<Gravatar email={userEmail} size={24} className="gravatar" />
|
||||
<span className="commentAuthor">{userFullName}</span>
|
||||
{ isPostUpdate ? <span className="postUpdateBadge">Post update</span> : null }
|
||||
</div>
|
||||
<p className="commentBody">{body}</p>
|
||||
<div className="commentFooter">
|
||||
<a className="commentReplyButton" onClick={handleToggleCommentReply}>
|
||||
<a className="commentReplyButton commentLink" onClick={handleToggleCommentReply}>
|
||||
{ replyForm.isOpen ? 'Cancel' : 'Reply' }
|
||||
</a>
|
||||
{
|
||||
isPowerUser ?
|
||||
<React.Fragment>
|
||||
<Separator />
|
||||
<a
|
||||
onClick={() => handleToggleIsCommentUpdate(id, isPostUpdate)}
|
||||
className="commentLink"
|
||||
>
|
||||
{ isPostUpdate ? 'No post update' : 'Post update' }
|
||||
</a>
|
||||
<Separator />
|
||||
<a href={`/admin/comments/${id}`} data-turbolinks="false">Edit</a>
|
||||
|
||||
</React.Fragment>
|
||||
:
|
||||
null
|
||||
|
||||
@@ -13,6 +13,7 @@ interface Props {
|
||||
|
||||
toggleCommentReply(commentId: number): void;
|
||||
setCommentReplyBody(commentId: number, body: string): void;
|
||||
handleToggleIsCommentUpdate(commentId: number, currentIsPostUpdate: boolean): void;
|
||||
handleSubmitComment(body: string, parentId: number): void;
|
||||
|
||||
isLoggedIn: boolean;
|
||||
@@ -28,6 +29,7 @@ const CommentList = ({
|
||||
|
||||
toggleCommentReply,
|
||||
setCommentReplyBody,
|
||||
handleToggleIsCommentUpdate,
|
||||
handleSubmitComment,
|
||||
|
||||
isLoggedIn,
|
||||
@@ -47,6 +49,7 @@ const CommentList = ({
|
||||
setCommentReplyBody(comment.id, (e.target as HTMLTextAreaElement).value)
|
||||
)
|
||||
}
|
||||
handleToggleIsCommentUpdate={handleToggleIsCommentUpdate}
|
||||
handleSubmitComment={handleSubmitComment}
|
||||
{...comment}
|
||||
|
||||
@@ -63,6 +66,7 @@ const CommentList = ({
|
||||
|
||||
toggleCommentReply={toggleCommentReply}
|
||||
setCommentReplyBody={setCommentReplyBody}
|
||||
handleToggleIsCommentUpdate={handleToggleIsCommentUpdate}
|
||||
handleSubmitComment={handleSubmitComment}
|
||||
|
||||
isLoggedIn={isLoggedIn}
|
||||
|
||||
@@ -23,6 +23,12 @@ interface Props {
|
||||
requestComments(postId: number, page?: number): void;
|
||||
toggleCommentReply(commentId: number): void;
|
||||
setCommentReplyBody(commentId: number, body: string): void;
|
||||
toggleCommentIsPostUpdate(
|
||||
postId: number,
|
||||
commentId: number,
|
||||
currentIsPostUpdate: boolean,
|
||||
authenticityToken: string,
|
||||
): void;
|
||||
submitComment(
|
||||
postId: number,
|
||||
body: string,
|
||||
@@ -36,6 +42,15 @@ class CommentsP extends React.Component<Props> {
|
||||
this.props.requestComments(this.props.postId);
|
||||
}
|
||||
|
||||
_handleToggleIsCommentUpdate = (commentId: number, currentIsPostUpdate: boolean) => {
|
||||
this.props.toggleCommentIsPostUpdate(
|
||||
this.props.postId,
|
||||
commentId,
|
||||
currentIsPostUpdate,
|
||||
this.props.authenticityToken,
|
||||
);
|
||||
}
|
||||
|
||||
_handleSubmitComment = (body: string, parentId: number) => {
|
||||
this.props.submitComment(
|
||||
this.props.postId,
|
||||
@@ -92,6 +107,7 @@ class CommentsP extends React.Component<Props> {
|
||||
replyForms={replyForms}
|
||||
toggleCommentReply={toggleCommentReply}
|
||||
setCommentReplyBody={setCommentReplyBody}
|
||||
handleToggleIsCommentUpdate={this._handleToggleIsCommentUpdate}
|
||||
handleSubmitComment={this._handleSubmitComment}
|
||||
parentId={null}
|
||||
level={1}
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
toggleCommentReply,
|
||||
setCommentReplyBody,
|
||||
} from '../actions/handleCommentReplies';
|
||||
import { toggleCommentIsUpdate } from '../actions/updateComment';
|
||||
import { submitComment } from '../actions/submitComment';
|
||||
|
||||
import { State } from '../reducers/rootReducer';
|
||||
@@ -31,6 +32,15 @@ const mapDispatchToProps = (dispatch) => ({
|
||||
dispatch(setCommentReplyBody(commentId, body));
|
||||
},
|
||||
|
||||
toggleCommentIsPostUpdate(
|
||||
postId: number,
|
||||
commentId: number,
|
||||
currentIsPostUpdate: boolean,
|
||||
authenticityToken: string,
|
||||
) {
|
||||
dispatch(toggleCommentIsUpdate(postId, commentId, currentIsPostUpdate, authenticityToken));
|
||||
},
|
||||
|
||||
submitComment(
|
||||
postId: number,
|
||||
body: string,
|
||||
|
||||
@@ -20,6 +20,11 @@ import {
|
||||
COMMENT_SUBMIT_FAILURE,
|
||||
} from '../actions/submitComment';
|
||||
|
||||
import {
|
||||
ToggleIsUpdateSuccessAction,
|
||||
TOGGLE_COMMENT_IS_UPDATE_SUCCESS,
|
||||
} from '../actions/updateComment';
|
||||
|
||||
import commentReducer from './commentReducer';
|
||||
import replyFormsReducer from './replyFormsReducer';
|
||||
|
||||
@@ -47,7 +52,8 @@ const commentsReducer = (
|
||||
action:
|
||||
CommentsRequestActionTypes |
|
||||
HandleCommentRepliesType |
|
||||
CommentSubmitActionTypes
|
||||
CommentSubmitActionTypes |
|
||||
ToggleIsUpdateSuccessAction
|
||||
): CommentsState => {
|
||||
switch (action.type) {
|
||||
case COMMENTS_REQUEST_START:
|
||||
@@ -95,6 +101,18 @@ const commentsReducer = (
|
||||
replyForms: replyFormsReducer(state.replyForms, action),
|
||||
};
|
||||
|
||||
case TOGGLE_COMMENT_IS_UPDATE_SUCCESS:
|
||||
return {
|
||||
...state,
|
||||
items:
|
||||
state.items.map(comment => {
|
||||
if (comment.id === action.commentId) {
|
||||
comment.isPostUpdate = !comment.isPostUpdate;
|
||||
return comment;
|
||||
} else return comment;
|
||||
})
|
||||
}
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -47,6 +47,11 @@ import {
|
||||
COMMENT_SUBMIT_FAILURE,
|
||||
} from '../actions/submitComment';
|
||||
|
||||
import {
|
||||
ToggleIsUpdateSuccessAction,
|
||||
TOGGLE_COMMENT_IS_UPDATE_SUCCESS,
|
||||
} from '../actions/updateComment';
|
||||
|
||||
import postReducer from './postReducer';
|
||||
import likesReducer from './likesReducer';
|
||||
import commentsReducer from './commentsReducer';
|
||||
@@ -82,7 +87,8 @@ const currentPostReducer = (
|
||||
LikeActionTypes |
|
||||
CommentsRequestActionTypes |
|
||||
HandleCommentRepliesType |
|
||||
CommentSubmitActionTypes
|
||||
CommentSubmitActionTypes |
|
||||
ToggleIsUpdateSuccessAction
|
||||
): CurrentPostState => {
|
||||
switch (action.type) {
|
||||
case POST_REQUEST_START:
|
||||
@@ -130,6 +136,7 @@ const currentPostReducer = (
|
||||
case COMMENT_SUBMIT_START:
|
||||
case COMMENT_SUBMIT_SUCCESS:
|
||||
case COMMENT_SUBMIT_FAILURE:
|
||||
case TOGGLE_COMMENT_IS_UPDATE_SUCCESS:
|
||||
return {
|
||||
...state,
|
||||
comments: commentsReducer(state.comments, action),
|
||||
|
||||
@@ -58,6 +58,13 @@
|
||||
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.postUpdateBadge {
|
||||
@extend
|
||||
.badge,
|
||||
.badgeLight,
|
||||
.ml-2;
|
||||
}
|
||||
}
|
||||
|
||||
.commentBody {
|
||||
@@ -67,7 +74,7 @@
|
||||
.commentFooter {
|
||||
font-size: 14px;
|
||||
|
||||
.commentReplyButton {
|
||||
.commentLink {
|
||||
color: $astuto-black;
|
||||
|
||||
&:hover {
|
||||
|
||||
Reference in New Issue
Block a user