Refactoring data management. Using context now.
Ripped out the subSection and unit code for the moment to refactor it to use the context - breadcrumbs is currently working with it, though.
This commit is contained in:
39
src/learning-sequence/hooks.js
Normal file
39
src/learning-sequence/hooks.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { useContext, useMemo, useState, useEffect } from 'react';
|
||||
import { AppContext } from '@edx/frontend-platform/react';
|
||||
|
||||
import CourseStructureContext from './CourseStructureContext';
|
||||
import { findBlockAncestry, getCourseBlocks, createBlocksMap } from './api';
|
||||
|
||||
export function useBlockAncestry(blockId) {
|
||||
const { blocks, loaded } = useContext(CourseStructureContext);
|
||||
return useMemo(() => {
|
||||
if (!loaded) {
|
||||
return [];
|
||||
}
|
||||
return findBlockAncestry(
|
||||
blocks,
|
||||
blockId,
|
||||
);
|
||||
}, [blocks, blockId, loaded]);
|
||||
}
|
||||
|
||||
export function useCourseStructure(courseId) {
|
||||
const { authenticatedUser } = useContext(AppContext);
|
||||
|
||||
const [blocks, setBlocks] = useState({});
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const [courseBlockId, setCourseBlockId] = useState();
|
||||
|
||||
useEffect(() => {
|
||||
setLoaded(false);
|
||||
getCourseBlocks(courseId, authenticatedUser.username).then((blocksData) => {
|
||||
setBlocks(createBlocksMap(blocksData.blocks));
|
||||
setCourseBlockId(blocksData.root);
|
||||
setLoaded(true);
|
||||
});
|
||||
}, [courseId]);
|
||||
|
||||
return {
|
||||
blocks, loaded, courseBlockId,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user