Skip to content

Commit

Permalink
fix: replacing LMS endpoints with CMS endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Jan 8, 2024
1 parent 42a7db6 commit 3eae62d
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 62 deletions.
4 changes: 4 additions & 0 deletions src/course-unit/course-sequence/Sequence.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const Sequence = ({
const intl = useIntl();
const { IN_PROGRESS, FAILED, SUCCESSFUL } = RequestStatus;
const shouldDisplayNotificationTriggerInSequence = useWindowSize().width < breakpoints.small.minWidth;
const STORE = useSelector(state => state);
console.log('STORE', STORE);

Check warning on line 21 in src/course-unit/course-sequence/Sequence.jsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
const { sequenceStatus, sequenceMightBeUnit } = useSelector(state => state.courseUnit);

const defaultContent = (
Expand All @@ -31,6 +33,8 @@ const Sequence = ({
</div>
);

console.log('sequenceStatus =============', sequenceStatus);

Check warning on line 36 in src/course-unit/course-sequence/Sequence.jsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement

// If sequence might be a unit, we want to keep showing a spinner - the courseware container will redirect us when
// it knows which sequence to actually go to.
const loading = sequenceStatus === IN_PROGRESS || (sequenceStatus === FAILED && sequenceMightBeUnit);
Expand Down
18 changes: 12 additions & 6 deletions src/course-unit/course-sequence/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import { useWindowSize } from '@edx/paragon';

import { useModel } from '../../generic/model-store';
import { RequestStatus } from '../../data/constants';
import { getCourseSectionVertical, getSequenceStatus, sequenceIdsSelector } from '../data/selectors';
import {
getCourseSectionVertical,
getCourseSectionVerticalLoadingStatus,
getSequenceStatus,

Check failure on line 10 in src/course-unit/course-sequence/hooks.js

View workflow job for this annotation

GitHub Actions / tests

'getSequenceStatus' is defined but never used
sequenceIdsSelector

Check failure on line 11 in src/course-unit/course-sequence/hooks.js

View workflow job for this annotation

GitHub Actions / tests

Missing trailing comma
} from '../data/selectors';

export function useSequenceNavigationMetadata(currentSequenceId, currentUnitId) {
const { SUCCESSFUL } = RequestStatus;
const sequenceIds = useSelector(sequenceIdsSelector);
const sequenceStatus = useSelector(getSequenceStatus);
const { courseSectionVerticalLoadingStatus } = useSelector(getCourseSectionVerticalLoadingStatus);
const sequenceStatus = courseSectionVerticalLoadingStatus;
const { nextUrl, prevUrl } = useSelector(getCourseSectionVertical);
const sequence = useModel('sequences', currentSequenceId);
const { courseId, status } = useSelector(state => state.courseDetail);

// console.log('sequence >>>', sequence);
const isCourseOrSequenceNotSuccessful = status !== SUCCESSFUL || sequenceStatus !== SUCCESSFUL;
const areIdsNotValid = !currentSequenceId || !currentUnitId || !sequence.unitIds;
const areIdsNotValid = !currentSequenceId || !currentUnitId || !sequence?.unitIds;
const isNotSuccessfulCompletion = isCourseOrSequenceNotSuccessful || areIdsNotValid;

// If we don't know the sequence and unit yet, then assume no.
Expand All @@ -24,13 +30,13 @@ export function useSequenceNavigationMetadata(currentSequenceId, currentUnitId)
}

const sequenceIndex = sequenceIds.indexOf(currentSequenceId);
const unitIndex = sequence.unitIds.indexOf(currentUnitId);
const unitIndex = sequence?.unitIds.indexOf(currentUnitId);

const isFirstSequence = sequenceIndex === 0;
const isFirstUnitInSequence = unitIndex === 0;
const isFirstUnit = isFirstSequence && isFirstUnitInSequence;
const isLastSequence = sequenceIndex === sequenceIds.length - 1;
const isLastUnitInSequence = unitIndex === sequence.unitIds.length - 1;
const isLastUnitInSequence = unitIndex === sequence?.unitIds.length - 1;

Check failure on line 39 in src/course-unit/course-sequence/hooks.js

View workflow job for this annotation

GitHub Actions / tests

Unsafe arithmetic operation on optional chaining. It can result in NaN
const isLastUnit = isLastSequence && isLastUnitInSequence;

const nextSequenceId = sequenceIndex < sequenceIds.length - 1 ? sequenceIds[sequenceIndex + 1] : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { RequestStatus } from '../../../data/constants';
import messages from '../messages';
import { useSequenceNavigationMetadata } from '../hooks';
import SequenceNavigationTabs from './SequenceNavigationTabs';
import { getCourseSectionVertical } from '../../data/selectors';

Check failure on line 19 in src/course-unit/course-sequence/sequence-navigation/SequenceNavigation.jsx

View workflow job for this annotation

GitHub Actions / tests

'getCourseSectionVertical' is defined but never used

const SequenceNavigation = ({
intl,
Expand All @@ -24,22 +25,23 @@ const SequenceNavigation = ({
className,
}) => {
const { sequenceStatus } = useSelector(state => state.courseUnit);
const MODELS = useSelector(state => state.models);
const {
isFirstUnit, isLastUnit, nextLink, previousLink,
} = useSequenceNavigationMetadata(sequenceId, unitId);
const sequence = useModel('sequences', sequenceId);

const shouldDisplayNotificationTriggerInSequence = useWindowSize().width < breakpoints.small.minWidth;
console.log('MODELS >>>', MODELS);

Check warning on line 34 in src/course-unit/course-sequence/sequence-navigation/SequenceNavigation.jsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
const renderUnitButtons = () => {
if (sequence.unitIds?.length === 0 || unitId === null) {
if (sequence?.unitIds?.length === 0 || unitId === null) {
return (
<div style={{ flexBasis: '100%', minWidth: 0, borderBottom: 'solid 1px #EAEAEA' }} />
);
}

return (
<SequenceNavigationTabs
unitIds={sequence.unitIds || []}
unitIds={sequence?.unitIds || []}
unitId={unitId}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ import { useIndexOfLastVisibleChild } from '../hooks';
import messages from '../messages';
import SequenceNavigationDropdown from './SequenceNavigationDropdown';
import UnitButton from './UnitButton';
import { useSelector } from 'react-redux';

Check failure on line 11 in src/course-unit/course-sequence/sequence-navigation/SequenceNavigationTabs.jsx

View workflow job for this annotation

GitHub Actions / tests

`react-redux` import should occur before import of `../hooks`

Check failure on line 11 in src/course-unit/course-sequence/sequence-navigation/SequenceNavigationTabs.jsx

View workflow job for this annotation

GitHub Actions / tests

'useSelector' is defined but never used

const SequenceNavigationTabs = ({ unitIds, unitId }) => {
const intl = useIntl();

// const { courseId } = useSelector(state => state.courseDetails);
const [
indexOfLastVisibleChild,
containerRef,
invisibleStyle,
] = useIndexOfLastVisibleChild();
const shouldDisplayDropdown = indexOfLastVisibleChild === -1;

// console.log('courseId', courseId);
return (
<div className="sequence-navigation-tabs-wrapper">
<div className="sequence-navigation-tabs-container d-flex" ref={containerRef}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import UnitIcon from './UnitIcon';
const UnitButton = ({
title, contentType, isActive, unitId, className, showTitle,
}) => {
const { courseId, sequenceId } = useSelector(state => state.courseUnit);

const { sequenceId } = useSelector(state => state.courseUnit);
const { courseId } = useSelector(state => state.courseDetail);
return (
<Button
className={className}
Expand Down
8 changes: 5 additions & 3 deletions src/course-unit/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
normalizeMetadata,
normalizeCourseHomeCourseMetadata,
appendBrowserTimezoneToUrl,
normalizeCourseSectionVerticalData,
} from './utils';

const getStudioBaseUrl = () => getConfig().STUDIO_BASE_URL;
Expand All @@ -29,7 +30,7 @@ export const getCourseHomeCourseMetadataApiUrl = (courseId) => `${getLmsBaseUrl(
export async function getCourseUnitData(unitId) {
const { data } = await getAuthenticatedHttpClient()
.get(getCourseUnitApiUrl(unitId));

console.log('GHGHGFHGHG', data);

Check warning on line 33 in src/course-unit/data/api.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
return camelCaseObject(data);
}

Expand Down Expand Up @@ -58,7 +59,7 @@ export async function editUnitDisplayName(unitId, displayName) {
export async function getSequenceMetadata(sequenceId) {
const { data } = await getAuthenticatedHttpClient()
.get(getSequenceMetadataApiUrl(sequenceId), {});

// console.log('normalizeSequenceMetadata(data)', normalizeSequenceMetadata(data));
return normalizeSequenceMetadata(data);
}

Expand All @@ -70,8 +71,9 @@ export async function getSequenceMetadata(sequenceId) {
export async function getCourseSectionVerticalData(unitId) {
const { data } = await getAuthenticatedHttpClient()
.get(getCourseSectionVerticalApiUrl(unitId));
console.log('getCourseSectionVerticalData', normalizeCourseSectionVerticalData(data));

Check warning on line 74 in src/course-unit/data/api.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement

return camelCaseObject(data);
return normalizeCourseSectionVerticalData(data);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/course-unit/data/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const getLoadingStatus = (state) => state.courseUnit.loadingStatus;
export const getSequenceStatus = (state) => state.courseUnit.sequenceStatus;

export const getCourseSectionVertical = (state) => state.courseUnit.courseSectionVertical;
export const getCourseSectionVerticalLoadingStatus = (state) => state.courseUnit.loadingStatus;

export function sequenceIdsSelector(state) {
if (state.courseUnit.courseStatus !== RequestStatus.SUCCESSFUL) {
Expand Down
100 changes: 56 additions & 44 deletions src/course-unit/data/thunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,28 @@ export function fetchCourseUnitQuery(courseId) {
};
}

export function fetchCourseSectionVerticalData(courseId) {
export function fetchCourseSectionVerticalData(courseId, sequenceId) {
return async (dispatch) => {
dispatch(updateLoadingCourseSectionVerticalDataStatus({ status: RequestStatus.IN_PROGRESS }));

dispatch(fetchSequenceRequest({ sequenceId }));
try {
const courseSectionVerticalData = await getCourseSectionVerticalData(courseId);
dispatch(fetchCourseSectionVerticalDataSuccess(courseSectionVerticalData));
dispatch(updateLoadingCourseSectionVerticalDataStatus({ status: RequestStatus.SUCCESSFUL }));
// console.log('courseSectionVerticalData SICK', courseSectionVerticalData.sequence);
dispatch(updateModel({
modelType: 'sequences',
model: courseSectionVerticalData.sequence,
}));
dispatch(updateModels({
modelType: 'units',
models: courseSectionVerticalData.units,
}));
dispatch(fetchSequenceSuccess({ sequenceId }));
return true;
} catch (error) {
dispatch(updateLoadingCourseSectionVerticalDataStatus({ status: RequestStatus.FAILED }));
dispatch(fetchSequenceFailure({ sequenceId }));
return false;
}
};
Expand All @@ -84,44 +95,45 @@ export function editCourseItemQuery(itemId, displayName) {
};
}

export function fetchSequence(sequenceId) {
return async (dispatch) => {
dispatch(fetchSequenceRequest({ sequenceId }));
try {
const { sequence, units } = await getSequenceMetadata(sequenceId);

if (sequence.blockType !== 'sequential') {
// Some other block types (particularly 'chapter') can be returned
// by this API. We want to error in that case, since downstream
// courseware code is written to render Sequences of Units.
logError(
`Requested sequence '${sequenceId}' `
+ `has block type '${sequence.blockType}'; expected block type 'sequential'.`,
);
dispatch(fetchSequenceFailure({ sequenceId }));
} else {
dispatch(updateModel({
modelType: 'sequences',
model: sequence,
}));
dispatch(updateModels({
modelType: 'units',
models: units,
}));
dispatch(fetchSequenceSuccess({ sequenceId }));
}
} catch (error) {
// Some errors are expected - for example, CoursewareContainer may request sequence metadata for a unit and rely
// on the request failing to notice that it actually does have a unit (mostly so it doesn't have to know anything
// about the opaque key structure). In such cases, the backend gives us a 422.
const sequenceMightBeUnit = error?.response?.status === 422;
if (!sequenceMightBeUnit) {
logError(error);
}
dispatch(fetchSequenceFailure({ sequenceId, sequenceMightBeUnit }));
}
};
}
// export function fetchSequence(sequenceId) {
// return async (dispatch) => {
// dispatch(fetchSequenceRequest({ sequenceId }));
// try {
// const { sequence, units } = await getSequenceMetadata(sequenceId);
//
// if (sequence.blockType !== 'sequential') {
// // Some other block types (particularly 'chapter') can be returned
// // by this API. We want to error in that case, since downstream
// // courseware code is written to render Sequences of Units.
// logError(
// `Requested sequence '${sequenceId}' `
// + `has block type '${sequence.blockType}'; expected block type 'sequential'.`,
// );
// dispatch(fetchSequenceFailure({ sequenceId }));
// } else {
// console.log('sequence', sequence);
// dispatch(updateModel({
// modelType: 'sequences',
// model: sequence,
// }));
// dispatch(updateModels({
// modelType: 'units',
// models: units,
// }));
// dispatch(fetchSequenceSuccess({ sequenceId }));
// }
// } catch (error) {
// // Some errors are expected - for example, CoursewareContainer may request sequence metadata for a unit and rely

Check failure on line 126 in src/course-unit/data/thunk.js

View workflow job for this annotation

GitHub Actions / tests

This line has a length of 121. Maximum allowed is 120
// // on the request failing to notice that it actually does have a unit (mostly so it doesn't have to know anything

Check failure on line 127 in src/course-unit/data/thunk.js

View workflow job for this annotation

GitHub Actions / tests

This line has a length of 122. Maximum allowed is 120
// // about the opaque key structure). In such cases, the backend gives us a 422.
// const sequenceMightBeUnit = error?.response?.status === 422;
// if (!sequenceMightBeUnit) {
// logError(error);
// }
// dispatch(fetchSequenceFailure({ sequenceId, sequenceMightBeUnit }));
// }
// };
// }

export function fetchCourse(courseId) {
return async (dispatch) => {
Expand Down Expand Up @@ -166,10 +178,10 @@ export function fetchCourse(courseId) {
modelsMap: sections,
}));
// We update for sequences because the sequence metadata may have come back first.
dispatch(updateModelsMap({
modelType: 'sequences',
modelsMap: sequences,
}));
// dispatch(updateModelsMap({
// modelType: 'sequences',
// modelsMap: sequences,
// }));
}

const fetchedMetadata = courseMetadataResult.status === 'fulfilled';
Expand Down
23 changes: 23 additions & 0 deletions src/course-unit/data/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,26 @@ export function normalizeCourseHomeCourseMetadata(metadata, rootSlug) {
isMasquerading: data.originalUserIsStaff && !data.isStaff,
};
}

export function normalizeCourseSectionVerticalData(metadata) {
const data = camelCaseObject(metadata);
return {
...data,
sequence: {
id: data.subsectionLocation,
title: data.xblock.displayName,
unitIds: data.xblockInfo.ancestorInfo.ancestors[0].childInfo.children.map((item) => item.id),
},
units: data.xblockInfo.ancestorInfo.ancestors[0].childInfo.children.map((unit) => ({
id: unit.id,
sequenceId: data.subsectionLocation,
bookmarked: unit.bookmarked,
complete: unit.complete,
title: unit.displayName,
contentType: unit.type,
graded: unit.graded,
containsContentTypeGatedContent: unit.contains_content_type_gated_content,
})),

};
}
6 changes: 3 additions & 3 deletions src/course-unit/hooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const useCourseUnit = ({ courseId, blockId }) => {

const unitTitle = courseUnit.metadata?.displayName || '';
const sequenceId = courseUnit.ancestorInfo?.ancestors[0].id;

console.log('sequenceId', sequenceId);

Check warning on line 31 in src/course-unit/hooks.jsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
const headerNavigationsActions = {
handleViewLive: () => {
window.open(config.LMS_BASE_URL + getUnitViewLivePath(courseId, blockId), '_blank');
Expand Down Expand Up @@ -63,8 +63,8 @@ const useCourseUnit = ({ courseId, blockId }) => {

useEffect(() => {
dispatch(fetchCourseUnitQuery(blockId));
dispatch(fetchCourseSectionVerticalData(blockId));
dispatch(fetchSequence(sequenceId));
dispatch(fetchCourseSectionVerticalData(blockId, sequenceId));
// dispatch(fetchSequence(sequenceId));
dispatch(fetchCourse(courseId));
handleNavigate(sequenceId);
}, [courseId, blockId, sequenceId]);
Expand Down

0 comments on commit 3eae62d

Please sign in to comment.