Files
frontend-app-discussions/src/discussions/topics/data/thunks.js

41 lines
1.2 KiB
JavaScript

/* eslint-disable import/prefer-default-export */
import { camelCaseObject } from '@edx/frontend-platform';
import { logError } from '@edx/frontend-platform/logging';
import { getCourseTopics } from './api';
import { 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) {
dispatch(fetchCourseTopicsFailed());
logError(error);
}
};
}