diff --git a/src/learning-sequence/LearningSequencePage.jsx b/src/learning-sequence/LearningSequencePage.jsx index 408430f0..fdd5f571 100644 --- a/src/learning-sequence/LearningSequencePage.jsx +++ b/src/learning-sequence/LearningSequencePage.jsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect, useContext } from 'react'; +import React from 'react'; import PropTypes from 'prop-types'; import { injectIntl, intlShape } from '@edx/frontend-platform/i18n'; @@ -6,7 +6,7 @@ import PageLoading from './PageLoading'; import messages from './messages'; import CourseBreadcrumbs from './CourseBreadcrumbs'; -// import SubSection from './SubSection'; +import SubSection from './SubSection'; import { useCourseStructure } from './hooks'; import CourseStructureContext from './CourseStructureContext'; @@ -37,7 +37,7 @@ function LearningSequencePage({ match, intl }) { />} {loaded && } - {/* */} + diff --git a/src/learning-sequence/SubSection.jsx b/src/learning-sequence/SubSection.jsx new file mode 100644 index 00000000..abe455ef --- /dev/null +++ b/src/learning-sequence/SubSection.jsx @@ -0,0 +1,239 @@ +import React, { Component, useContext, useEffect, useState } from 'react'; +import PropTypes from 'prop-types'; +import { getConfig } from '@edx/frontend-platform'; + +import SubSectionNavigation from './SubSectionNavigation'; +import { getSubSectionMetadata } from './api'; +import CourseStructureContext from './CourseStructureContext'; +import Unit from './Unit'; + +function useSubSectionMetadata(courseId, subSectionId) { + const [metadata, setMetadata] = useState(null); + const [loaded, setLoaded] = useState(false); + + useEffect(() => { + setLoaded(false); + getSubSectionMetadata(courseId, subSectionId).then((data) => { + setMetadata(data); + setLoaded(true); + }); + }, [courseId, subSectionId]); + + return { + metadata, + loaded, + }; +} + +function useExamRedirect(metadata, blocks) { + useEffect(() => { + if (metadata !== null && blocks !== null) { + if (metadata.isTimeLimited) { + global.location.href = blocks[metadata.itemId].lmsWebUrl; + } + } + }, [metadata, blocks]); +} + +/* + + + const calculateUnitId = (metadata, options) => { + const { first, last, preferredUnitId } = options; + let position = metadata.position - 1; // metadata's position is 1's indexed + position = first ? 0 : position; + position = last ? metadata.unitIds.length - 1 : position; + position = preferredUnitId ? metadata.unitIds.indexOf(preferredUnitId) : position; + const unitId = metadata.items[position].id; + + return unitId; + } + + handleUnitChange = (unitId) => { + this.setState({ + unitId, + }); + } + + */ + +export default function SubSection() { + const { + courseId, + subSectionId, + unitId, + blocks, + } = useContext(CourseStructureContext); + const { metadata } = useSubSectionMetadata(courseId, subSectionId); + + useExamRedirect(metadata, blocks); + + if (blocks === null || metadata === null) { + return null; + } + + const unit = blocks[unitId]; + + // units={this.state.units} + // unitIds={this.state.subSectionMetadata.unitIds} + // activeUnitId={this.state.unitId} + // unitClickHandler={this.handleUnitChange} + // nextClickHandler={this.handleNextClick} + // previousClickHandler={this.handlePreviousClick} + return ( +
+ + + +
+ ); +} + + +/* + +

{course.displayName}

+ + + + +