Skip to content

Commit

Permalink
feat: add auto scroll to new sections when created
Browse files Browse the repository at this point in the history
  • Loading branch information
steff456 committed Nov 29, 2023
1 parent 7d988b4 commit 3e5408e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/course-outline/CourseOutline.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import { React, useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import { useIntl } from '@edx/frontend-platform/i18n';
import {
Expand Down Expand Up @@ -32,8 +32,10 @@ import PublishModal from './publish-modal/PublishModal';
import DeleteModal from './delete-modal/DeleteModal';
import { useCourseOutline } from './hooks';
import messages from './messages';
import { scrollToBottom } from './utils';

const CourseOutline = ({ courseId }) => {
const listRef = useRef(null);
const intl = useIntl();

const {
Expand Down Expand Up @@ -73,6 +75,10 @@ const CourseOutline = ({ courseId }) => {

document.title = getPageHeadTitle(courseName, intl.formatMessage(messages.headingTitle));

useEffect(() => {
scrollToBottom(listRef);
}, [sectionsList]);

const {
isShow: isShowProcessingNotification,
title: processingNotificationTitle,
Expand Down Expand Up @@ -147,6 +153,7 @@ const CourseOutline = ({ courseId }) => {
onEditSectionSubmit={handleEditSectionSubmit}
onDuplicateSubmit={handleDuplicateSectionSubmit}
isSectionsExpanded={isSectionsExpanded}
ref={listRef}
/>
))}
<Button
Expand Down
15 changes: 10 additions & 5 deletions src/course-outline/section-card/SectionCard.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import React, { useEffect, useState } from 'react';
import {
React,
forwardRef,
useEffect,
useState,
} from 'react';
import PropTypes from 'prop-types';
import { useDispatch } from 'react-redux';
import { useIntl } from '@edx/frontend-platform/i18n';
Expand All @@ -11,7 +16,7 @@ import CardHeader from '../card-header/CardHeader';
import { getSectionStatus } from '../utils';
import messages from './messages';

const SectionCard = ({
const SectionCard = forwardRef(({
section,
children,
onOpenHighlightsModal,
Expand All @@ -21,7 +26,7 @@ const SectionCard = ({
onOpenDeleteModal,
onDuplicateSubmit,
isSectionsExpanded,
}) => {
}, lastItemRef) => {
const intl = useIntl();
const dispatch = useDispatch();
const [isExpanded, setIsExpanded] = useState(isSectionsExpanded);
Expand Down Expand Up @@ -79,7 +84,7 @@ const SectionCard = ({
}, [savingStatus]);

return (
<div className="section-card" data-testid="section-card">
<div className="section-card" data-testid="section-card" ref={lastItemRef}>
<CardHeader
sectionId={id}
title={displayName}
Expand Down Expand Up @@ -128,7 +133,7 @@ const SectionCard = ({
)}
</div>
);
};
});

SectionCard.defaultProps = {
children: null,
Expand Down
8 changes: 8 additions & 0 deletions src/course-outline/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,16 @@ const getHighlightsFormValues = (currentHighlights) => {
return formValues;
};

/* eslint no-param-reassign: "error" */
const scrollToBottom = (ref) => {
if (ref.current) {
ref.current.scrollIntoView({ behavior: 'smooth' });
}
};

export {
getSectionStatus,
getSectionStatusBadgeContent,
getHighlightsFormValues,
scrollToBottom,
};

0 comments on commit 3e5408e

Please sign in to comment.