diff --git a/src/course-home/outline-tab/OutlineTab.jsx b/src/course-home/outline-tab/OutlineTab.jsx index a2ce8101ea..073cfd953d 100644 --- a/src/course-home/outline-tab/OutlineTab.jsx +++ b/src/course-home/outline-tab/OutlineTab.jsx @@ -106,6 +106,14 @@ const OutlineTab = ({ intl }) => { const location = useLocation(); + const getSectionsExpandStatus = (status = false) => courses[rootCourseId].sectionIds.reduce((obj, sectionId) => { + // eslint-disable-next-line no-param-reassign + obj[sectionId] = status; + return obj; + }, {}); + + const [expandedSections, setExpandedSections] = useState(() => getSectionsExpandStatus()); + useEffect(() => { const currentParams = new URLSearchParams(location.search); const startCourse = currentParams.get('start_course'); @@ -123,6 +131,15 @@ const OutlineTab = ({ intl }) => { } }, [location.search]); + useEffect(() => { + const allSectionsExpanded = Object.values(expandedSections); + const isAllExpanded = allSectionsExpanded.every(Boolean); + if (isAllExpanded) { setExpandAll(true); } + + const isAllCollapsed = allSectionsExpanded.every(val => val === false); + if (isAllCollapsed) { setExpandAll(false); } + }, [expandedSections]); + return ( <>