fix: filtering posts in a topic doesn't work when filtered results are empty (#203)
A list of all posts in a topic is stored in an object for quick lookup. This is used when switching between topics to get the list of posts in that topic. When filters change the list of posts in a topics is updated and this is picked up by the UI. If changing filters when browsing a topic results in an empty result, this would cause the original list of topics for that topic to be retained, causing the filter to simply not apply. This commit fixes that by resetting the post list in that case.
This commit is contained in:
@@ -27,6 +27,7 @@ export const coursesApiUrl = `${apiBaseUrl}/api/discussion/v1/courses/`;
|
||||
* @param {string} threadType Can be 'discussion' or 'question'.
|
||||
* @param {ThreadViewStatus} view Set to "unread" on "unanswered" to filter to only those statuses.
|
||||
* @param {boolean} countFlagged If true, abuseFlaggedCount will be available.
|
||||
* @param {number} cohort
|
||||
* @returns {Promise<{}>}
|
||||
*/
|
||||
export async function getThreads(
|
||||
|
||||
@@ -44,9 +44,10 @@ import {
|
||||
* Normalises raw data returned by threads API by mapping threads to id and
|
||||
* mapping topic ids to threads in them.
|
||||
* @param data
|
||||
* @param {[string]?} topicIds
|
||||
* @returns {{pagination, threadsById: {}, threadsInTopic: {}, avatars: {}}}
|
||||
*/
|
||||
export function normaliseThreads(data) {
|
||||
export function normaliseThreads(data, topicIds = null) {
|
||||
const normalized = {};
|
||||
let threads;
|
||||
if ('results' in data) {
|
||||
@@ -59,6 +60,11 @@ export function normaliseThreads(data) {
|
||||
const threadsById = {};
|
||||
let avatars = {};
|
||||
const ids = [];
|
||||
if (topicIds) {
|
||||
topicIds.forEach(topicId => {
|
||||
threadsInTopic[topicId] = [];
|
||||
});
|
||||
}
|
||||
threads.forEach(
|
||||
thread => {
|
||||
const { topicId, id } = thread;
|
||||
@@ -129,7 +135,7 @@ export function fetchThreads(courseId, {
|
||||
try {
|
||||
dispatch(fetchThreadsRequest({ courseId }));
|
||||
const data = await getThreads(courseId, options);
|
||||
const normalisedData = normaliseThreads(camelCaseObject(data));
|
||||
const normalisedData = normaliseThreads(camelCaseObject(data), topicIds);
|
||||
dispatch(fetchThreadsSuccess({ ...normalisedData, page, author }));
|
||||
} catch (error) {
|
||||
if (getHttpErrorStatus(error) === 403) {
|
||||
|
||||
Reference in New Issue
Block a user