fix: threads lost issue on load more in topics

This commit is contained in:
Mehak Nasir
2022-08-25 14:32:04 +05:00
committed by Mehak Nasir
parent 41ee555e88
commit 5704b402cd
2 changed files with 21 additions and 6 deletions

View File

@@ -2,12 +2,25 @@
import { createSlice } from '@reduxjs/toolkit';
import {
PostsStatusFilter,
RequestStatus,
ThreadOrdering,
ThreadType,
PostsStatusFilter, RequestStatus, ThreadOrdering, ThreadType,
} from '../../../data/constants';
const mergeThreadsInTopics = (dataFromState, dataFromPayload) => {
const mergedArray = [];
mergedArray.push(dataFromState);
mergedArray.push(dataFromPayload);
return mergedArray.reduce((acc, obj) => {
const keys = Object.keys(obj);
const values = Object.values(obj);
keys.forEach((key, index) => {
if (!acc[key]) { acc[key] = []; }
if (Array.isArray(acc[key])) { acc[key] = acc[key].concat(values[index]); } else { acc[key].push(values[index]); }
return acc;
});
return acc;
}, {});
};
const threadsSlice = createSlice({
name: 'thread',
initialState: {
@@ -62,7 +75,7 @@ const threadsSlice = createSlice({
}
state.status = RequestStatus.SUCCESSFUL;
state.threadsById = { ...state.threadsById, ...payload.threadsById };
state.threadsInTopic = { ...state.threadsInTopic, ...payload.threadsInTopic };
state.threadsInTopic = mergeThreadsInTopics(state.threadsInTopic, payload.threadsInTopic);
state.avatars = { ...state.avatars, ...payload.avatars };
state.nextPage = (payload.page < payload.pagination.numPages) ? payload.page + 1 : null;
state.totalPages = payload.pagination.numPages;

View File

@@ -137,7 +137,9 @@ export function fetchThreads(courseId, {
dispatch(fetchThreadsRequest({ courseId }));
const data = await getThreads(courseId, options);
const normalisedData = normaliseThreads(camelCaseObject(data), topicIds);
dispatch(fetchThreadsSuccess({ ...normalisedData, page, author }));
dispatch(fetchThreadsSuccess({
...normalisedData, page, author,
}));
} catch (error) {
if (getHttpErrorStatus(error) === 403) {
dispatch(fetchThreadsDenied());