From f3226c729b34510131663e7fab236b1c8bfd19fe Mon Sep 17 00:00:00 2001 From: SaadYousaf Date: Tue, 28 Feb 2023 19:55:32 +0500 Subject: [PATCH] feat: send enableInContextSidebar param to backend to identify source of content for events --- src/discussions/common/HoverCard.test.jsx | 2 ++ .../post-comments/PostCommentsView.test.jsx | 2 ++ .../post-comments/comments/comment/CommentEditor.jsx | 4 +++- src/discussions/post-comments/data/api.js | 11 +++++++++-- src/discussions/post-comments/data/hooks.js | 5 ++++- src/discussions/post-comments/data/thunks.js | 9 ++++++--- src/discussions/posts/data/api.js | 4 +++- src/discussions/posts/data/thunks.js | 3 ++- src/discussions/posts/post-editor/PostEditor.jsx | 1 + 9 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/discussions/common/HoverCard.test.jsx b/src/discussions/common/HoverCard.test.jsx index e00d7b1b..c2e90d29 100644 --- a/src/discussions/common/HoverCard.test.jsx +++ b/src/discussions/common/HoverCard.test.jsx @@ -28,6 +28,7 @@ const discussionPostId = 'thread-1'; const questionPostId = 'thread-2'; const courseId = 'course-v1:edX+TestX+Test_Course'; const reverseOrder = false; +const enableInContextSidebar = false; let store; let axiosMock; let container; @@ -45,6 +46,7 @@ function mockAxiosReturnPagedComments() { requested_fields: 'profile_image', endorsed, reverse_order: reverseOrder, + enable_in_context_sidebar: enableInContextSidebar, }, }) .reply(200, Factory.build('commentsResult', { can_delete: true }, { diff --git a/src/discussions/post-comments/PostCommentsView.test.jsx b/src/discussions/post-comments/PostCommentsView.test.jsx index 3835467c..bb5d0fb5 100644 --- a/src/discussions/post-comments/PostCommentsView.test.jsx +++ b/src/discussions/post-comments/PostCommentsView.test.jsx @@ -35,6 +35,7 @@ const closedPostId = 'thread-2'; const courseId = 'course-v1:edX+TestX+Test_Course'; const topicsApiUrl = `${getApiBaseUrl()}/api/discussion/v1/course_topics/${courseId}`; const reverseOrder = false; +const enableInContextSidebar = false; let store; let axiosMock; let testLocation; @@ -52,6 +53,7 @@ function mockAxiosReturnPagedComments() { requested_fields: 'profile_image', endorsed, reverse_order: reverseOrder, + enable_in_context_sidebar: enableInContextSidebar, }, }) .reply(200, Factory.build('commentsResult', { can_delete: true }, { diff --git a/src/discussions/post-comments/comments/comment/CommentEditor.jsx b/src/discussions/post-comments/comments/comment/CommentEditor.jsx index 6d6ac867..f590e214 100644 --- a/src/discussions/post-comments/comments/comment/CommentEditor.jsx +++ b/src/discussions/post-comments/comments/comment/CommentEditor.jsx @@ -13,6 +13,7 @@ import { TinyMCEEditor } from '../../../../components'; import FormikErrorFeedback from '../../../../components/FormikErrorFeedback'; import PostPreviewPane from '../../../../components/PostPreviewPane'; import { useDispatchWithState } from '../../../../data/hooks'; +import { DiscussionContext } from '../../../common/context'; import { selectModerationSettings, selectUserHasModerationPrivileges, @@ -32,6 +33,7 @@ function CommentEditor({ }) { const editorRef = useRef(null); const { authenticatedUser } = useContext(AppContext); + const { enableInContextSidebar } = useContext(DiscussionContext); const userHasModerationPrivileges = useSelector(selectUserHasModerationPrivileges); const userIsGroupTa = useSelector(selectUserIsGroupTa); const userIsStaff = useSelector(selectUserIsStaff); @@ -71,7 +73,7 @@ function CommentEditor({ }; await dispatch(editComment(comment.id, payload)); } else { - await dispatch(addComment(values.comment, comment.threadId, comment.parentId)); + await dispatch(addComment(values.comment, comment.threadId, comment.parentId, enableInContextSidebar)); } /* istanbul ignore if: TinyMCE is mocked so this cannot be easily tested */ if (editorRef.current) { diff --git a/src/discussions/post-comments/data/api.js b/src/discussions/post-comments/data/api.js index c4cc9c21..1313c7c1 100644 --- a/src/discussions/post-comments/data/api.js +++ b/src/discussions/post-comments/data/api.js @@ -16,6 +16,8 @@ export const getCommentsApiUrl = () => `${getConfig().LMS_BASE_URL}/api/discussi * @param {EndorsementStatus} endorsed * @param {number=} page * @param {number=} pageSize + * @param reverseOrder + * @param enableInContextSidebar * @returns {Promise<{}>} */ export async function getThreadComments( @@ -24,6 +26,7 @@ export async function getThreadComments( page, pageSize, reverseOrder, + enableInContextSidebar = false, } = {}, ) { const params = snakeCaseObject({ @@ -33,6 +36,7 @@ export async function getThreadComments( pageSize, reverseOrder, requestedFields: 'profile_image', + enableInContextSidebar, }); const { data } = await getAuthenticatedHttpClient() @@ -69,11 +73,14 @@ export async function getCommentResponses( * @param {string} comment Raw comment data to post. * @param {string} threadId Thread ID for thread in which to post comment. * @param {string=} parentId ID for a comments parent. + * @param {boolean} enableInContextSidebar * @returns {Promise<{}>} */ -export async function postComment(comment, threadId, parentId = null) { +export async function postComment(comment, threadId, parentId = null, enableInContextSidebar = false) { const { data } = await getAuthenticatedHttpClient() - .post(getCommentsApiUrl(), snakeCaseObject({ threadId, raw_body: comment, parentId })); + .post(getCommentsApiUrl(), snakeCaseObject({ + threadId, raw_body: comment, parentId, enableInContextSidebar, + })); return data; } diff --git a/src/discussions/post-comments/data/hooks.js b/src/discussions/post-comments/data/hooks.js index 5f704975..5cb072b0 100644 --- a/src/discussions/post-comments/data/hooks.js +++ b/src/discussions/post-comments/data/hooks.js @@ -1,4 +1,4 @@ -import { useEffect } from 'react'; +import { useContext, useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; @@ -6,6 +6,7 @@ import { sendTrackEvent } from '@edx/frontend-platform/analytics'; import { EndorsementStatus } from '../../../data/constants'; import { useDispatchWithState } from '../../../data/hooks'; +import { DiscussionContext } from '../../common/context'; import { selectThread } from '../../posts/data/selectors'; import { markThreadAsRead } from '../../posts/data/thunks'; import { @@ -42,6 +43,7 @@ export function usePostComments(postId, endorsed = null) { const reverseOrder = useSelector(selectCommentSortOrder); const hasMorePages = useSelector(selectThreadHasMorePages(postId, endorsed)); const currentPage = useSelector(selectThreadCurrentPage(postId, endorsed)); + const { enableInContextSidebar } = useContext(DiscussionContext); const handleLoadMoreResponses = async () => { const params = { @@ -58,6 +60,7 @@ export function usePostComments(postId, endorsed = null) { endorsed, page: 1, reverseOrder, + enableInContextSidebar, })); }, [postId, reverseOrder]); diff --git a/src/discussions/post-comments/data/thunks.js b/src/discussions/post-comments/data/thunks.js index 820c46ff..9c6dbef9 100644 --- a/src/discussions/post-comments/data/thunks.js +++ b/src/discussions/post-comments/data/thunks.js @@ -80,12 +80,15 @@ export function fetchThreadComments( page = 1, reverseOrder, endorsed = EndorsementStatus.DISCUSSION, + enableInContextSidebar, } = {}, ) { return async (dispatch) => { try { dispatch(fetchCommentsRequest()); - const data = await getThreadComments(threadId, { page, reverseOrder, endorsed }); + const data = await getThreadComments(threadId, { + page, reverseOrder, endorsed, enableInContextSidebar, + }); dispatch(fetchCommentsSuccess({ ...normaliseComments(camelCaseObject(data)), endorsed, @@ -144,7 +147,7 @@ export function editComment(commentId, comment, action = null) { }; } -export function addComment(comment, threadId, parentId = null) { +export function addComment(comment, threadId, parentId = null, enableInContextSidebar = false) { return async (dispatch) => { try { dispatch(postCommentRequest({ @@ -152,7 +155,7 @@ export function addComment(comment, threadId, parentId = null) { threadId, parentId, })); - const data = await postComment(comment, threadId, parentId); + const data = await postComment(comment, threadId, parentId, enableInContextSidebar); dispatch(postCommentSuccess(camelCaseObject(data))); } catch (error) { if (getHttpErrorStatus(error) === 403) { diff --git a/src/discussions/posts/data/api.js b/src/discussions/posts/data/api.js index a465415f..a5b417e7 100644 --- a/src/discussions/posts/data/api.js +++ b/src/discussions/posts/data/api.js @@ -87,6 +87,7 @@ export async function getThread(threadId, courseId) { * @param {boolean} following Follow the thread after creating * @param {boolean} anonymous Should the thread be anonymous to all users * @param {boolean} anonymousToPeers Should the thread be anonymous to peers + * @param {boolean} enableInContextSidebar * @returns {Promise<{}>} */ export async function postThread( @@ -101,6 +102,7 @@ export async function postThread( anonymous, anonymousToPeers, } = {}, + enableInContextSidebar = false, ) { const postData = snakeCaseObject({ courseId, @@ -112,8 +114,8 @@ export async function postThread( anonymous, anonymousToPeers, groupId: cohort, + enableInContextSidebar, }); - const { data } = await getAuthenticatedHttpClient() .post(getThreadsApiUrl(), postData); return data; diff --git a/src/discussions/posts/data/thunks.js b/src/discussions/posts/data/thunks.js index 86f939f8..691967ec 100644 --- a/src/discussions/posts/data/thunks.js +++ b/src/discussions/posts/data/thunks.js @@ -204,6 +204,7 @@ export function createNewThread({ anonymous, anonymousToPeers, cohort, + enableInContextSidebar, }) { return async (dispatch) => { try { @@ -223,7 +224,7 @@ export function createNewThread({ following, anonymous, anonymousToPeers, - }); + }, enableInContextSidebar); dispatch(postThreadSuccess(camelCaseObject(data))); } catch (error) { if (getHttpErrorStatus(error) === 403) { diff --git a/src/discussions/posts/post-editor/PostEditor.jsx b/src/discussions/posts/post-editor/PostEditor.jsx index 0335fed9..4967c3f3 100644 --- a/src/discussions/posts/post-editor/PostEditor.jsx +++ b/src/discussions/posts/post-editor/PostEditor.jsx @@ -187,6 +187,7 @@ function PostEditor({ anonymous: allowAnonymous ? values.anonymous : undefined, anonymousToPeers: allowAnonymousToPeers ? values.anonymousToPeers : undefined, cohort, + enableInContextSidebar, })); } /* istanbul ignore if: TinyMCE is mocked so this cannot be easily tested */