From 422fbf61738b7675d4aec294d07855157c431360 Mon Sep 17 00:00:00 2001
From: ayesha waris <73840786+ayesha-waris@users.noreply.github.com>
Date: Wed, 26 Jun 2024 17:13:52 +0500
Subject: [PATCH] fix: fixed author liking its own post (#720)
---
src/discussions/common/HoverCard.jsx | 4 +++-
src/discussions/data/hooks.js | 13 +++++++++++--
.../post-comments/comments/comment/Comment.jsx | 8 ++++----
src/discussions/posts/post/Post.jsx | 8 ++++----
4 files changed, 22 insertions(+), 11 deletions(-)
diff --git a/src/discussions/common/HoverCard.jsx b/src/discussions/common/HoverCard.jsx
index feedc7f83..a47777676 100644
--- a/src/discussions/common/HoverCard.jsx
+++ b/src/discussions/common/HoverCard.jsx
@@ -12,7 +12,7 @@ import classNames from 'classnames';
import { useIntl } from '@edx/frontend-platform/i18n';
import { ThreadType } from '../../data/constants';
-import { useUserPostingEnabled } from '../data/hooks';
+import { useHasLikePermission, useUserPostingEnabled } from '../data/hooks';
import PostCommentsContext from '../post-comments/postCommentsContext';
import ActionsDropdown from './ActionsDropdown';
import DiscussionContext from './context';
@@ -33,6 +33,7 @@ const HoverCard = ({
const { enableInContextSidebar } = useContext(DiscussionContext);
const { isClosed } = useContext(PostCommentsContext);
const isUserPrivilegedInPostingRestriction = useUserPostingEnabled();
+ const userHasLikePermission = useHasLikePermission(contentType, id);
return (
{
e.preventDefault();
diff --git a/src/discussions/data/hooks.js b/src/discussions/data/hooks.js
index 2576824bb..6de5e1f0f 100644
--- a/src/discussions/data/hooks.js
+++ b/src/discussions/data/hooks.js
@@ -16,10 +16,11 @@ import { AppContext } from '@edx/frontend-platform/react';
import selectCourseTabs from '../../components/NavigationBar/data/selectors';
import { LOADED } from '../../components/NavigationBar/data/slice';
import fetchTab from '../../components/NavigationBar/data/thunks';
-import { RequestStatus, Routes } from '../../data/constants';
+import { ContentActions, RequestStatus, Routes } from '../../data/constants';
import { selectTopicsUnderCategory } from '../../data/selectors';
import fetchCourseBlocks from '../../data/thunks';
import DiscussionContext from '../common/context';
+import PostCommentsContext from '../post-comments/postCommentsContext';
import { clearRedirect } from '../posts/data';
import { threadsLoadingStatus } from '../posts/data/selectors';
import { selectTopics } from '../topics/data/selectors';
@@ -27,7 +28,8 @@ import tourCheckpoints from '../tours/constants';
import selectTours from '../tours/data/selectors';
import { updateTourShowStatus } from '../tours/data/thunks';
import messages from '../tours/messages';
-import { discussionsPath } from '../utils';
+import { checkPermissions, discussionsPath } from '../utils';
+import { ContentSelectors } from './constants';
import {
selectAreThreadsFiltered,
selectEnableInContext,
@@ -284,3 +286,10 @@ export const useDebounce = (value, delay) => {
);
return debouncedValue;
};
+
+export const useHasLikePermission = (contentType, id) => {
+ const { postType } = useContext(PostCommentsContext);
+ const content = { ...useSelector(ContentSelectors[contentType](id)), postType };
+
+ return checkPermissions(content, ContentActions.VOTE);
+};
diff --git a/src/discussions/post-comments/comments/comment/Comment.jsx b/src/discussions/post-comments/comments/comment/Comment.jsx
index 3e24f0d35..290bb95ab 100644
--- a/src/discussions/post-comments/comments/comment/Comment.jsx
+++ b/src/discussions/post-comments/comments/comment/Comment.jsx
@@ -104,6 +104,10 @@ const Comment = ({
hideReportConfirmation();
}, [abuseFlagged, id, hideReportConfirmation]);
+ const handleCommentLike = useCallback(async () => {
+ await dispatch(editComment(id, { voted: !voted }));
+ }, [id, voted]);
+
const actionHandlers = useMemo(() => ({
[ContentActions.EDIT_CONTENT]: handleEditContent,
[ContentActions.ENDORSE]: handleCommentEndorse,
@@ -124,10 +128,6 @@ const Comment = ({
}
}, [isUserPrivilegedInPostingRestriction]);
- const handleCommentLike = useCallback(async () => {
- await dispatch(editComment(id, { voted: !voted }));
- }, [id, voted]);
-
const handleCloseEditor = useCallback(() => {
setEditing(false);
}, []);
diff --git a/src/discussions/posts/post/Post.jsx b/src/discussions/posts/post/Post.jsx
index 958d1c163..423ffe13d 100644
--- a/src/discussions/posts/post/Post.jsx
+++ b/src/discussions/posts/post/Post.jsx
@@ -85,6 +85,10 @@ const Post = ({ handleAddResponseButton }) => {
updateExistingThread(postId, { pinned: !pinned }),
), [postId, pinned]);
+ const handlePostLike = useCallback(() => {
+ dispatch(updateExistingThread(postId, { voted: !voted }));
+ }, [postId, voted]);
+
const handlePostReport = useCallback(() => {
if (abuseFlagged) {
dispatch(updateExistingThread(postId, { flagged: !abuseFlagged }));
@@ -109,10 +113,6 @@ const Post = ({ handleAddResponseButton }) => {
hideClosePostModal();
}, [postId, hideClosePostModal]);
- const handlePostLike = useCallback(() => {
- dispatch(updateExistingThread(postId, { voted: !voted }));
- }, [postId, voted]);
-
const handlePostFollow = useCallback(() => {
dispatch(updateExistingThread(postId, { following: !following }));
}, [postId, following]);