Basic discussions forum framework
Adds the basic structure for the Discussions MFE around which future development will happen.
This commit is contained in:
35
src/discussions/posts/data/api.js
Normal file
35
src/discussions/posts/data/api.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
|
||||
import { API_BASE_URL } from '../../../data/constants';
|
||||
|
||||
export async function getCourseThreads(
|
||||
courseId, topicIds, {
|
||||
page, pageSize, textSearch, orderBy, following, view, requestedFields,
|
||||
} = {},
|
||||
) {
|
||||
const url = new URL(`${API_BASE_URL}/api/discussion/v1/threads/`);
|
||||
const paramsMap = {
|
||||
page,
|
||||
page_size: pageSize,
|
||||
topic_id: topicIds && topicIds.join(','),
|
||||
text_search: textSearch,
|
||||
order_by: orderBy,
|
||||
following,
|
||||
view,
|
||||
requested_fields: requestedFields,
|
||||
};
|
||||
url.searchParams.append('course_id', courseId);
|
||||
Object.keys(paramsMap)
|
||||
.forEach(
|
||||
(param) => {
|
||||
const paramValue = paramsMap[param];
|
||||
if (paramValue) {
|
||||
url.searchParams.append(param, paramValue);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
const { data } = await getAuthenticatedHttpClient()
|
||||
.get(url);
|
||||
return data;
|
||||
}
|
||||
1
src/discussions/posts/data/index.js
Normal file
1
src/discussions/posts/data/index.js
Normal file
@@ -0,0 +1 @@
|
||||
export * from './slices';
|
||||
4
src/discussions/posts/data/selectors.js
Normal file
4
src/discussions/posts/data/selectors.js
Normal file
@@ -0,0 +1,4 @@
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
export const selectCourseThreads = topicId => state => state.threads.threads[topicId] || [];
|
||||
|
||||
export const courseTopicsStatus = state => state.topics.status;
|
||||
55
src/discussions/posts/data/slices.js
Normal file
55
src/discussions/posts/data/slices.js
Normal file
@@ -0,0 +1,55 @@
|
||||
/* eslint-disable no-param-reassign,import/prefer-default-export */
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
import { LoadingStatus } from '../../../data/constants';
|
||||
|
||||
function normaliseThreads(rawThreadsData) {
|
||||
const topicThreadMap = {};
|
||||
rawThreadsData.forEach(
|
||||
thread => {
|
||||
if (!topicThreadMap[thread.topic_id]) {
|
||||
topicThreadMap[thread.topic_id] = [];
|
||||
}
|
||||
topicThreadMap[thread.topic_id].push(thread);
|
||||
},
|
||||
);
|
||||
return topicThreadMap;
|
||||
}
|
||||
|
||||
const courseThreadsSlice = createSlice({
|
||||
name: 'courseThreads',
|
||||
initialState: {
|
||||
status: LoadingStatus.LOADING,
|
||||
page: null,
|
||||
threads: {
|
||||
// Mapping of topic ids to threads in them
|
||||
},
|
||||
totalPages: null,
|
||||
totalThreads: null,
|
||||
},
|
||||
reducers: {
|
||||
fetchCourseThreadsRequest: (state) => {
|
||||
state.status = LoadingStatus.LOADING;
|
||||
},
|
||||
fetchCourseThreadsSuccess: (state, { payload }) => {
|
||||
state.status = LoadingStatus.LOADED;
|
||||
state.threads = normaliseThreads(payload.results);
|
||||
state.page = payload.pagination.page;
|
||||
state.totalPages = payload.pagination.num_pages;
|
||||
state.totalThreads = payload.pagination.count;
|
||||
},
|
||||
fetchCourseThreadsFailed: (state) => {
|
||||
state.status = LoadingStatus.FAILED;
|
||||
},
|
||||
fetchCourseThreadsDenied: (state) => {
|
||||
state.status = LoadingStatus.DENIED;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const {
|
||||
fetchCourseThreadsRequest,
|
||||
fetchCourseThreadsSuccess,
|
||||
fetchCourseThreadsFailed,
|
||||
} = courseThreadsSlice.actions;
|
||||
|
||||
export const courseThreadsReducer = courseThreadsSlice.reducer;
|
||||
17
src/discussions/posts/data/thunks.js
Normal file
17
src/discussions/posts/data/thunks.js
Normal file
@@ -0,0 +1,17 @@
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
import { logError } from '@edx/frontend-platform/logging';
|
||||
import { getCourseThreads } from './api';
|
||||
import { fetchCourseThreadsFailed, fetchCourseThreadsRequest, fetchCourseThreadsSuccess } from './slices';
|
||||
|
||||
export function fetchCourseThreads(courseId, topicIds) {
|
||||
return async (dispatch) => {
|
||||
try {
|
||||
dispatch(fetchCourseThreadsRequest({ courseId }));
|
||||
const data = await getCourseThreads(courseId, topicIds);
|
||||
dispatch(fetchCourseThreadsSuccess(data));
|
||||
} catch (error) {
|
||||
dispatch(fetchCourseThreadsFailed());
|
||||
logError(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user