From 4629b791e71057838340f0a9e4af81d944cba4d8 Mon Sep 17 00:00:00 2001
From: dolf321 <65448126+dolf321@users.noreply.github.com>
Date: Fri, 25 Aug 2023 13:29:34 +0300
Subject: [PATCH] ADD comment editing
---
web/src/components/forums/Comment/Comment.jsx | 55 ++++++++++++++-----
.../forums/CommentsList/CommentsList.jsx | 5 +-
web/src/components/forums/Post/Post.jsx | 8 ++-
web/src/pages/core/public/Forum/Forum.jsx | 15 ++++-
4 files changed, 65 insertions(+), 18 deletions(-)
diff --git a/web/src/components/forums/Comment/Comment.jsx b/web/src/components/forums/Comment/Comment.jsx
index b0901402..f34997d2 100644
--- a/web/src/components/forums/Comment/Comment.jsx
+++ b/web/src/components/forums/Comment/Comment.jsx
@@ -4,11 +4,13 @@ import {useSnackbar} from 'notistack';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import ReplyIcon from '@mui/icons-material/Reply';
+import EditIcon from '@mui/icons-material/Edit';
import ExpandLessIcon from '@mui/icons-material/ExpandLess';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import {useStyles} from './Comment.styles';
import {toRelativeDate} from '../../../utils/datetime';
+import Publisher from '../Publisher/Publisher';
import RichTextEditor from '../Editor/RichTextEditor';
export default function Comment({
@@ -18,30 +20,46 @@ export default function Comment({
content,
createdDate,
hasReplies,
+ ownedByMe,
handleCollapse,
isCollapsed,
replyCount,
handleReply,
+ handleEditComment,
}) {
const classes = useStyles();
const enqueueSnackbar = useSnackbar();
-
const [children, setChildren] = useState(undefined);
+ const [openEditor, setOpenEditor] = useState(false);
return (
-
-
- {user[0]}
-
- {user}
-
- {toRelativeDate(new Date(createdDate))}
-
-
-
-
-
+ {!openEditor ? (
+ <>
+
+
+ {user[0]}
+
+ {user}
+
+ {toRelativeDate(new Date(createdDate))}
+
+
+
+
+
+ >
+ ) :
+ (
+ {}}
+ handlePublish={(content) => handleEditComment(id, content)}
+ />
+ )
+ }
{hasReplies &&
-
+
Reply
}
+ {ownedByMe &&
+ setOpenEditor(true)}
+ className={classes.action}
+ >
+
+ Edit
+
+ }
);
diff --git a/web/src/components/forums/CommentsList/CommentsList.jsx b/web/src/components/forums/CommentsList/CommentsList.jsx
index 8e1f80a0..4bb9e4a5 100644
--- a/web/src/components/forums/CommentsList/CommentsList.jsx
+++ b/web/src/components/forums/CommentsList/CommentsList.jsx
@@ -5,9 +5,8 @@ import Comment from '../Comment/Comment';
import Publisher from '../Publisher/Publisher';
import {useStyles} from './CommentsList.styles';
-export default function CommentsList({comments, handleCreateComment}) {
+export default function CommentsList({comments, handleCreateComment, handleEditComment}) {
const classes = useStyles();
-
const Thread = ({hasChildren, comment}) => {
const [isReplying, setIsReplying] = useState(false);
const [collapsed, setCollapsed] = useState(false);
@@ -22,6 +21,7 @@ export default function CommentsList({comments, handleCreateComment}) {
createdDate={comment.created}
hasReplies={comment.children.length > 0}
replyCount={comment.children.length}
+ ownedByMe={comment.owned_by_me}
handleCollapse={() => setCollapsed(false)}
isCollapsed={collapsed}
handleReply={() => {
@@ -29,6 +29,7 @@ export default function CommentsList({comments, handleCreateComment}) {
setCollapsed(false);
setIsReplying(true);
}}
+ handleEditComment={handleEditComment}
/>
{(hasChildren && !collapsed) && comment.children.length > 0 && comment.children.map((childComment, index) => (
diff --git a/web/src/components/forums/Post/Post.jsx b/web/src/components/forums/Post/Post.jsx
index e25c1fea..66c8017c 100644
--- a/web/src/components/forums/Post/Post.jsx
+++ b/web/src/components/forums/Post/Post.jsx
@@ -28,6 +28,7 @@ export default function Post({
updatedDate,
comments,
handleCreateComment,
+ handleEditComment,
handleEditPost,
}) {
const classes = useStyles();
@@ -49,6 +50,7 @@ export default function Post({
{(currentUser) => (
)}
-
+
)}
diff --git a/web/src/pages/core/public/Forum/Forum.jsx b/web/src/pages/core/public/Forum/Forum.jsx
index 01cfd870..634e509d 100644
--- a/web/src/pages/core/public/Forum/Forum.jsx
+++ b/web/src/pages/core/public/Forum/Forum.jsx
@@ -133,7 +133,7 @@ export default function Forum() {
const handleEditPost = React.useCallback((post) => {
axios.patch(`/api/public/forum/post/${selectedPost.id}`, {...post})
.then(() => {
- refreshSelectedPost(false);
+ refreshSelectedPost();
setIsDialogOpen(false);
})
.catch(standardErrorHandler(enqueueSnackbar));
@@ -157,6 +157,18 @@ export default function Forum() {
.catch(standardErrorHandler(enqueueSnackbar));
}, []);
+ const handleEditComment = React.useCallback((id, comment) => {
+ axios.patch(`/api/public/forum/post/comment/${id}`, {
+ content: comment?.content,
+ anonymous: comment?.anonymous,
+ })
+ .then((r) => {
+ standardStatusHandler(r, enqueueSnackbar);
+ refreshSelectedPost();
+ })
+ .catch(standardErrorHandler(enqueueSnackbar));
+ }, []);
+
return (
)}