From 25cf4ce4c81e00156def62a78f04f1492f971817 Mon Sep 17 00:00:00 2001 From: Awais Ansari <79941147+awais-ansari@users.noreply.github.com> Date: Wed, 3 Aug 2022 13:56:19 +0500 Subject: [PATCH] fix: display direct link post at top of the list (#226) --- src/discussions/comments/CommentsView.jsx | 2 +- src/discussions/posts/data/slices.js | 14 +++++++++++++- src/discussions/posts/data/thunks.js | 9 +++++++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/discussions/comments/CommentsView.jsx b/src/discussions/comments/CommentsView.jsx index 9f912520..b1418362 100644 --- a/src/discussions/comments/CommentsView.jsx +++ b/src/discussions/comments/CommentsView.jsx @@ -114,7 +114,7 @@ function CommentsView({ intl }) { const thread = usePost(postId); const dispatch = useDispatch(); if (!thread) { - dispatch(fetchThread(postId)); + dispatch(fetchThread(postId, true)); return ( ); diff --git a/src/discussions/posts/data/slices.js b/src/discussions/posts/data/slices.js index 5344fe5b..dfbe947e 100644 --- a/src/discussions/posts/data/slices.js +++ b/src/discussions/posts/data/slices.js @@ -55,8 +55,12 @@ const threadsSlice = createSlice({ state.pages = []; state.author = payload.author; } + if (state.pages[payload.page - 1]) { + state.pages[payload.page - 1] = [...state.pages[payload.page - 1], ...payload.ids]; + } else { + state.pages[payload.page - 1] = payload.ids; + } state.status = RequestStatus.SUCCESSFUL; - state.pages[payload.page - 1] = payload.ids; state.threadsById = { ...state.threadsById, ...payload.threadsById }; state.threadsInTopic = { ...state.threadsInTopic, ...payload.threadsInTopic }; state.avatars = { ...state.avatars, ...payload.avatars }; @@ -78,6 +82,13 @@ const threadsSlice = createSlice({ state.threadsById = { ...state.threadsById, ...payload.threadsById }; state.avatars = { ...state.avatars, ...payload.avatars }; }, + fetchThreadByDirectLinkSuccess: (state, { payload }) => { + state.status = RequestStatus.SUCCESSFUL; + state.pages[payload.page - 1] = payload.ids; + state.threadsInTopic = { ...payload.threadsInTopic, ...state.threadsInTopic }; + state.threadsById = { ...payload.threadsById, ...state.threadsById }; + state.avatars = { ...state.avatars, ...payload.avatars }; + }, fetchThreadFailed: (state) => { state.status = RequestStatus.FAILED; }, @@ -194,6 +205,7 @@ export const { fetchThreadsRequest, fetchThreadsSuccess, fetchThreadSuccess, + fetchThreadByDirectLinkSuccess, postThreadDenied, postThreadFailed, postThreadRequest, diff --git a/src/discussions/posts/data/thunks.js b/src/discussions/posts/data/thunks.js index 71fc0d94..1d037c77 100644 --- a/src/discussions/posts/data/thunks.js +++ b/src/discussions/posts/data/thunks.js @@ -14,6 +14,7 @@ import { deleteThreadFailed, deleteThreadRequest, deleteThreadSuccess, + fetchThreadByDirectLinkSuccess, fetchThreadDenied, fetchThreadFailed, fetchThreadRequest, @@ -148,12 +149,16 @@ export function fetchThreads(courseId, { }; } -export function fetchThread(threadId) { +export function fetchThread(threadId, isDirectLinkPost = false) { return async (dispatch) => { try { dispatch(fetchThreadRequest({ threadId })); const data = await getThread(threadId); - dispatch(fetchThreadSuccess(normaliseThreads(camelCaseObject(data)))); + if (isDirectLinkPost) { + dispatch(fetchThreadByDirectLinkSuccess({ ...normaliseThreads(camelCaseObject(data)), page: 1 })); + } else { + dispatch(fetchThreadSuccess(normaliseThreads(camelCaseObject(data)))); + } } catch (error) { if (getHttpErrorStatus(error) === 403) { dispatch(fetchThreadDenied());