* fix: display subsection name when there is no units in subsection * feat: display archived topics is post editor and topics list * fix: posts filter are affecting topic posts result
65 lines
1.8 KiB
JavaScript
65 lines
1.8 KiB
JavaScript
/* eslint-disable import/prefer-default-export */
|
|
import { reduce } from 'lodash';
|
|
|
|
import { logError } from '@edx/frontend-platform/logging';
|
|
|
|
import { getCourseTopicsV3 } from './api';
|
|
import { fetchCourseTopicsFailed, fetchCourseTopicsRequest, fetchCourseTopicsSuccess } from './slices';
|
|
|
|
function normalizeTopicsV3(topics) {
|
|
const coursewareUnits = reduce(topics, (arrayOfUnits, chapter) => {
|
|
if (chapter?.children) {
|
|
return [
|
|
...arrayOfUnits,
|
|
...reduce(chapter.children, (units, sequential) => {
|
|
if (sequential?.children) {
|
|
return [
|
|
...units,
|
|
...sequential.children.map((unit) => ({
|
|
...unit,
|
|
parentId: sequential.id,
|
|
parentTitle: sequential.displayName,
|
|
})),
|
|
];
|
|
}
|
|
return units;
|
|
}, []),
|
|
];
|
|
}
|
|
return arrayOfUnits;
|
|
}, []);
|
|
|
|
const archivedTopics = reduce(topics, (arrayOfArchivedTopics, topic) => {
|
|
if (topic.id === 'archived') {
|
|
return topic.children;
|
|
}
|
|
return arrayOfArchivedTopics;
|
|
}, []);
|
|
|
|
const coursewareTopics = topics.filter((topic) => topic.courseware);
|
|
const nonCoursewareTopics = topics.filter((topic) => !topic.courseware && topic.enabledInContext);
|
|
const nonCoursewareIds = nonCoursewareTopics?.map((topic) => topic.id);
|
|
|
|
return {
|
|
topics,
|
|
units: coursewareUnits,
|
|
coursewareTopics,
|
|
nonCoursewareTopics,
|
|
nonCoursewareIds,
|
|
archivedTopics,
|
|
};
|
|
}
|
|
|
|
export function fetchCourseTopicsV3(courseId) {
|
|
return async (dispatch) => {
|
|
try {
|
|
dispatch(fetchCourseTopicsRequest({ courseId }));
|
|
const data = await getCourseTopicsV3(courseId);
|
|
dispatch(fetchCourseTopicsSuccess(normalizeTopicsV3(data)));
|
|
} catch (error) {
|
|
dispatch(fetchCourseTopicsFailed());
|
|
logError(error);
|
|
}
|
|
};
|
|
}
|