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 ( <>
@@ -163,7 +180,14 @@ const OutlineTab = ({ intl }) => { <>
-
@@ -176,6 +200,7 @@ const OutlineTab = ({ intl }) => { defaultOpen={sections[sectionId].resumeBlock} expand={expandAll} section={sections[sectionId]} + setExpandedSections={setExpandedSections} /> ))} diff --git a/src/course-home/outline-tab/Section.jsx b/src/course-home/outline-tab/Section.jsx index 3de888a89a..e08adff48b 100644 --- a/src/course-home/outline-tab/Section.jsx +++ b/src/course-home/outline-tab/Section.jsx @@ -18,6 +18,7 @@ const Section = ({ expand, intl, section, + setExpandedSections, }) => { const { complete, @@ -32,12 +33,17 @@ const Section = ({ const [open, setOpen] = useState(defaultOpen); + const updateExpandedSection = (status) => { + setOpen(status); + setExpandedSections((prevObj) => ({ ...prevObj, [section.id]: status })); + }; + useEffect(() => { setOpen(expand); }, [expand]); useEffect(() => { - setOpen(defaultOpen); + updateExpandedSection(defaultOpen); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); @@ -78,12 +84,12 @@ const Section = ({ styling="card-lg" title={sectionTitle} open={open} - onToggle={() => { setOpen(!open); }} + onToggle={() => updateExpandedSection(!open)} iconWhenClosed={( { setOpen(true); }} + onClick={() => updateExpandedSection(true)} size="sm" /> )} @@ -91,7 +97,7 @@ const Section = ({ { setOpen(false); }} + onClick={() => updateExpandedSection(false)} size="sm" /> )} @@ -118,6 +124,7 @@ Section.propTypes = { expand: PropTypes.bool.isRequired, intl: intlShape.isRequired, section: PropTypes.shape().isRequired, + setExpandedSections: PropTypes.func.isRequired, }; export default injectIntl(Section);