Extensive refactor of application data management. (#32)
* Extensive refactor of application data management. - “course-blocks” and “course-meta” are replaced with “courseware” module. This obscures the difference between the two from the application itself. - a generic “model-store” module is used to store all course, section, sequence, and unit data in a normalized way, agnostic to the metadata vs. blocks APIs. - SequenceContainer has been removed, and it’s work is just done in CourseContainer instead. - UI components are - in general - more responsible for deciding their own behavior during data loading. If they want to show a spinner or nothing, it’s up to their discretion. - The API layer is responsible for normalizing data into a form the app will want to use, prior to putting it into the model store. * Organizing into some more sub-modules. - Bookmarks becomes it’s own module. - SequenceNavigation becomes another one. * More modularization of data directories. - Moving model-store up to the top. - Moving fetchCourse and fetchSequence up to the top-level data directory, since they’re used by both courseware and outline. - Moving getBlockCompletion and updateSequencePosition into the courseware/data directory, since they pertain to that page. * Normalizing on using the word “title” * Using history.replace instead of history.push This fixes TNL-7125 * Allowing sub-components to use hooks and redux This reduces the amount of data we need to pass around, and lets us move some complexity to more natural modules. * Fixing bug where enrollment alert is shown for undefined isEnrolled The enrollment alert would inadvertently be shown if a user navigated from the outline to the course. This was because it interpreted an undefined “isEnrolled” flag as false. Instead, we should wait for the isEnrolled flag to be explicitly true or false. * Organizing modules. - Renaming “outline” to “course-home”. - Moving sequence and sequence-navigation modules under the course module. * Some final application organization and ADR write-ups. * Final refactoring - Favoring passing data by ID and looking it up in the store with useModel. - Moving headers into course-header directory. * Updating ADRs. Splitting model-store information out into its own ADR.
This commit is contained in:
47
src/courseware/data/api.js
Normal file
47
src/courseware/data/api.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import { getConfig } from '@edx/frontend-platform';
|
||||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
|
||||
|
||||
const getSequenceXModuleHandlerUrl = (courseUsageKey, sequenceId) => `${getConfig().LMS_BASE_URL}/courses/${courseUsageKey}/xblock/${sequenceId}/handler/xmodule_handler`;
|
||||
|
||||
export async function getBlockCompletion(courseUsageKey, sequenceId, usageKey) {
|
||||
// Post data sent to this endpoint must be url encoded
|
||||
// TODO: Remove the need for this to be the case.
|
||||
// TODO: Ensure this usage of URLSearchParams is working in Internet Explorer
|
||||
const urlEncoded = new URLSearchParams();
|
||||
urlEncoded.append('usage_key', usageKey);
|
||||
const requestConfig = {
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
};
|
||||
|
||||
const { data } = await getAuthenticatedHttpClient().post(
|
||||
`${getSequenceXModuleHandlerUrl(courseUsageKey, sequenceId)}/get_completion`,
|
||||
urlEncoded.toString(),
|
||||
requestConfig,
|
||||
);
|
||||
|
||||
if (data.complete) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function updateSequencePosition(courseUsageKey, sequenceId, position) {
|
||||
// Post data sent to this endpoint must be url encoded
|
||||
// TODO: Remove the need for this to be the case.
|
||||
// TODO: Ensure this usage of URLSearchParams is working in Internet Explorer
|
||||
const urlEncoded = new URLSearchParams();
|
||||
// Position is 1-indexed on the server and 0-indexed in this app. Adjust here.
|
||||
urlEncoded.append('position', position + 1);
|
||||
const requestConfig = {
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
};
|
||||
|
||||
const { data } = await getAuthenticatedHttpClient().post(
|
||||
`${getSequenceXModuleHandlerUrl(courseUsageKey, sequenceId)}/goto_position`,
|
||||
urlEncoded.toString(),
|
||||
requestConfig,
|
||||
);
|
||||
|
||||
return data;
|
||||
}
|
||||
20
src/courseware/data/selectors.js
Normal file
20
src/courseware/data/selectors.js
Normal file
@@ -0,0 +1,20 @@
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
export function sequenceIdsSelector(state) {
|
||||
if (state.courseware.courseStatus !== 'loaded') {
|
||||
return [];
|
||||
}
|
||||
const { sectionIds } = state.models.courses[state.courseware.courseUsageKey];
|
||||
let sequenceIds = [];
|
||||
sectionIds.forEach(sectionId => {
|
||||
sequenceIds = [...sequenceIds, ...state.models.sections[sectionId].sequenceIds];
|
||||
});
|
||||
return sequenceIds;
|
||||
}
|
||||
|
||||
export function firstSequenceIdSelector(state) {
|
||||
if (state.courseware.courseStatus !== 'loaded') {
|
||||
return null;
|
||||
}
|
||||
const sectionId = state.models.courses[state.courseware.courseUsageKey].sectionIds[0];
|
||||
return state.models.sections[sectionId].sequenceIds[0];
|
||||
}
|
||||
66
src/courseware/data/thunks.js
Normal file
66
src/courseware/data/thunks.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import { logError } from '@edx/frontend-platform/logging';
|
||||
import {
|
||||
getBlockCompletion,
|
||||
updateSequencePosition,
|
||||
} from './api';
|
||||
import {
|
||||
updateModel,
|
||||
} from '../../model-store';
|
||||
|
||||
export function checkBlockCompletion(courseUsageKey, sequenceId, unitId) {
|
||||
return async (dispatch, getState) => {
|
||||
const { models } = getState();
|
||||
if (models.units[unitId].complete) {
|
||||
return; // do nothing. Things don't get uncompleted after they are completed.
|
||||
}
|
||||
|
||||
try {
|
||||
const isComplete = await getBlockCompletion(courseUsageKey, sequenceId, unitId);
|
||||
dispatch(updateModel({
|
||||
modelType: 'units',
|
||||
model: {
|
||||
id: unitId,
|
||||
complete: isComplete,
|
||||
},
|
||||
}));
|
||||
} catch (error) {
|
||||
logError(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function saveSequencePosition(courseUsageKey, sequenceId, position) {
|
||||
return async (dispatch, getState) => {
|
||||
const { models } = getState();
|
||||
const initialPosition = models.sequences[sequenceId].position;
|
||||
// Optimistically update the position.
|
||||
dispatch(updateModel({
|
||||
modelType: 'sequences',
|
||||
model: {
|
||||
id: sequenceId,
|
||||
position,
|
||||
},
|
||||
}));
|
||||
try {
|
||||
await updateSequencePosition(courseUsageKey, sequenceId, position);
|
||||
// Update again under the assumption that the above call succeeded, since it doesn't return a
|
||||
// meaningful response.
|
||||
dispatch(updateModel({
|
||||
modelType: 'sequences',
|
||||
model: {
|
||||
id: sequenceId,
|
||||
position,
|
||||
},
|
||||
}));
|
||||
} catch (error) {
|
||||
logError(error);
|
||||
dispatch(updateModel({
|
||||
modelType: 'sequences',
|
||||
model: {
|
||||
id: sequenceId,
|
||||
position: initialPosition,
|
||||
},
|
||||
}));
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user