* test: added newly cases to cover codeCov * test: added new cases of empty posts * test: added test cases of legacy topics * refactor: removed extra lines and extra const objects * test: added test cases for postPreviewpane * test: added test cases for post comment view api * refactor: removed extra lines and code * refactor: fixed issues identified during code review * refactor: changed description of one test case --------- Co-authored-by: sundasnoreen12 <sundasnoreen12@ggmail.com>
48 lines
1.4 KiB
JavaScript
48 lines
1.4 KiB
JavaScript
/* eslint-disable import/prefer-default-export */
|
|
import { camelCaseObject } from '@edx/frontend-platform';
|
|
import { logError } from '@edx/frontend-platform/logging';
|
|
|
|
import { getHttpErrorStatus } from '../../utils';
|
|
import { getCourseTopics } from './api';
|
|
import {
|
|
fetchCourseTopicsDenied, fetchCourseTopicsFailed, fetchCourseTopicsRequest, fetchCourseTopicsSuccess,
|
|
} from './slices';
|
|
|
|
function normaliseTopics(data) {
|
|
const topicsInCategory = {};
|
|
const topics = {};
|
|
const categoryIds = [];
|
|
data.coursewareTopics.forEach(category => {
|
|
topicsInCategory[category.name] = category.children.map(topic => {
|
|
topics[topic.id] = { ...topic, categoryId: category.name };
|
|
return topic.id;
|
|
});
|
|
categoryIds.push(category.name);
|
|
});
|
|
const nonCoursewareIds = data.nonCoursewareTopics.map(topic => {
|
|
topics[topic.id] = topic;
|
|
return topic.id;
|
|
});
|
|
return {
|
|
categoryIds, topicsInCategory, topics, nonCoursewareIds,
|
|
};
|
|
}
|
|
|
|
export function fetchCourseTopics(courseId) {
|
|
return async (dispatch) => {
|
|
try {
|
|
dispatch(fetchCourseTopicsRequest({ courseId }));
|
|
|
|
const data = normaliseTopics(camelCaseObject(await getCourseTopics(courseId)));
|
|
dispatch(fetchCourseTopicsSuccess(data));
|
|
} catch (error) {
|
|
if (getHttpErrorStatus(error) === 403) {
|
|
dispatch(fetchCourseTopicsDenied());
|
|
} else {
|
|
dispatch(fetchCourseTopicsFailed());
|
|
}
|
|
logError(error);
|
|
}
|
|
};
|
|
}
|