Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add auto scroll to new sections when created #14

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -34,8 +34,10 @@ import ConfigureModal from './configure-modal/ConfigureModal';
import DeleteModal from './delete-modal/DeleteModal';
import { useCourseOutline } from './hooks';
import messages from './messages';
import { scrollToElement } from './utils';

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

const {
Expand Down Expand Up @@ -77,6 +79,10 @@ const CourseOutline = ({ courseId }) => {
handleNewSectionSubmit,
} = useCourseOutline({ courseId });

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

const {
isShow: isShowProcessingNotification,
title: processingNotificationTitle,
Expand Down Expand Up @@ -155,6 +161,7 @@ const CourseOutline = ({ courseId }) => {
onEditSectionSubmit={handleEditSectionSubmit}
onDuplicateSubmit={handleDuplicateSectionSubmit}
isSectionsExpanded={isSectionsExpanded}
ref={listRef}
/>
))}
<Button
Expand Down
3 changes: 3 additions & 0 deletions src/course-outline/CourseOutline.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ let store;
const mockPathname = '/foo-bar';
const courseId = '123';

window.HTMLElement.prototype.scrollIntoView = jest.fn();
steff456 marked this conversation as resolved.
Show resolved Hide resolved

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: () => ({
Expand Down Expand Up @@ -131,6 +133,7 @@ describe('<CourseOutline />', () => {

element = await findAllByTestId('section-card');
expect(element.length).toBe(5);
expect(window.HTMLElement.prototype.scrollIntoView).toBeCalled();
});

it('render error alert after failed reindex correctly', async () => {
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 @@ -22,7 +27,7 @@ const SectionCard = ({
onOpenDeleteModal,
onDuplicateSubmit,
isSectionsExpanded,
}) => {
}, lastItemRef) => {
const intl = useIntl();
const dispatch = useDispatch();
const [isExpanded, setIsExpanded] = useState(isSectionsExpanded);
Expand Down Expand Up @@ -80,7 +85,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 @@ -130,7 +135,7 @@ const SectionCard = ({
)}
</div>
);
};
});

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

const scrollToElement = (ref) => {
ref.current?.scrollIntoView({ behavior: 'smooth' });
};

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