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

fix: [AXIMST-14] Course unit - New unit btn, Replacing LMS endpoints with CMS endpoints #102

Closed
2 changes: 1 addition & 1 deletion .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ENABLE_PROGRESS_GRAPH_SETTINGS=false
ENABLE_TEAM_TYPE_SETTING=false
ENABLE_NEW_EDITOR_PAGES=true
ENABLE_NEW_VIDEO_UPLOAD_PAGE = false
ENABLE_UNIT_PAGE = false
ENABLE_UNIT_PAGE = true
ENABLE_VIDEO_UPLOAD_PAGE_LINK_IN_CONTENT_DROPDOWN = false
ENABLE_TAGGING_TAXONOMY_PAGES = true
BBB_LEARN_MORE_URL=''
Expand Down
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"react-textarea-autosize": "^8.4.1",
"react-transition-group": "4.4.5",
"redux": "4.0.5",
"redux-mock-store": "^1.5.4",
"regenerator-runtime": "0.13.7",
"universal-cookie": "^4.0.4",
"uuid": "^3.4.0",
Expand Down
13 changes: 8 additions & 5 deletions src/CourseAuthoringRoutes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Navigate, Routes, Route, useParams,
} from 'react-router-dom';
import { PageWrap } from '@edx/frontend-platform/react';
import Placeholder from '@edx/frontend-lib-content-components';
import CourseAuthoringPage from './CourseAuthoringPage';
import { PagesAndResources } from './pages-and-resources';
import EditorContainer from './editors/EditorContainer';
Expand All @@ -16,8 +15,10 @@ import ScheduleAndDetails from './schedule-and-details';
import { GradingSettings } from './grading-settings';
import CourseTeam from './course-team/CourseTeam';
import { CourseUpdates } from './course-updates';
import { CourseUnit } from './course-unit';
import CourseExportPage from './export-page/CourseExportPage';
import CourseImportPage from './import-page/CourseImportPage';
import { DECODED_ROUTES } from './constants';

/**
* As of this writing, these routes are mounted at a path prefixed with the following:
Expand Down Expand Up @@ -69,10 +70,12 @@ const CourseAuthoringRoutes = () => {
path="custom-pages/*"
element={<PageWrap><CustomPages courseId={courseId} /></PageWrap>}
/>
<Route
path="/container/:blockId"
element={process.env.ENABLE_UNIT_PAGE === 'true' ? <PageWrap><Placeholder /></PageWrap> : null}
/>
{DECODED_ROUTES.COURSE_UNIT.map((path) => (
<Route
path={path}
element={process.env.ENABLE_UNIT_PAGE === 'true' ? <PageWrap><CourseUnit courseId={courseId} /></PageWrap> : null}
/>
))}
<Route
path="editor/course-videos/:blockId"
element={process.env.ENABLE_NEW_EDITOR_PAGES === 'true' ? <PageWrap><VideoSelectorContainer courseId={courseId} /></PageWrap> : null}
Expand Down
8 changes: 8 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const NOTIFICATION_MESSAGES = {
saving: 'Saving',
duplicating: 'Duplicating',
deleting: 'Deleting',
adding: 'Adding',
empty: '',
};

Expand All @@ -35,3 +36,10 @@ export const COURSE_CREATOR_STATES = {
denied: 'denied',
disallowedForThisSite: 'disallowed_for_this_site',
};

export const DECODED_ROUTES = {
COURSE_UNIT: [
'/container/:blockId/:sequenceId',
'/container/:blockId',
],
};
10 changes: 6 additions & 4 deletions src/course-outline/CourseOutline.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
React, useState, useEffect,
} from 'react';
import { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { useIntl } from '@edx/frontend-platform/i18n';
import {
Expand Down Expand Up @@ -40,7 +38,9 @@ import EmptyPlaceholder from './empty-placeholder/EmptyPlaceholder';
import PublishModal from './publish-modal/PublishModal';
import ConfigureModal from './configure-modal/ConfigureModal';
import DeleteModal from './delete-modal/DeleteModal';
import { useCourseOutline } from './hooks';
import {
useCourseOutline, useScrollToLocatorElement,
} from './hooks';
import messages from './messages';

const CourseOutline = ({ courseId }) => {
Expand Down Expand Up @@ -88,6 +88,8 @@ const CourseOutline = ({ courseId }) => {
handleDragNDrop,
} = useCourseOutline({ courseId });

useScrollToLocatorElement({ isLoading });

const [sections, setSections] = useState(sectionsList);

const initialSections = [...sectionsList];
Expand Down
16 changes: 15 additions & 1 deletion src/course-outline/CourseOutline.test.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import {
act, render, waitFor, cleanup, fireEvent, within,
} from '@testing-library/react';
Expand Down Expand Up @@ -46,6 +45,8 @@ let axiosMock;
let store;
const mockPathname = '/foo-bar';
const courseId = '123';
const locatorSectionName = 'Demo Course Overview';
const locatorSectionId = 'block-v1:edX+DemoX+Demo_Course+type@sequential+block@edx_introduction';

window.HTMLElement.prototype.scrollIntoView = jest.fn();

Expand All @@ -54,6 +55,7 @@ jest.mock('react-router-dom', () => ({
useLocation: () => ({
pathname: mockPathname,
}),
useSearchParams: () => [new URLSearchParams({ show: locatorSectionId })],
}));

jest.mock('../help-urls/hooks', () => ({
Expand Down Expand Up @@ -380,6 +382,18 @@ describe('<CourseOutline />', () => {
expect(await findAllByTestId('section-card')).toHaveLength(5);
});

it('check correct scrolling to the locator section when URL has a "show" param', async () => {
const scrollIntoViewFn = jest.fn();
window.HTMLElement.prototype.scrollIntoView = scrollIntoViewFn;
const { getByText } = render(<RootWrapper />);

await waitFor(() => {
expect(getByText(locatorSectionName)).toBeInTheDocument();
expect(scrollIntoViewFn).toHaveBeenCalled();
expect(scrollIntoViewFn).toHaveBeenCalledWith({ behavior: 'smooth' });
});
});

it('check whether subsection is duplicated successfully', async () => {
const { findAllByTestId } = render(<RootWrapper />);
const section = courseOutlineIndexMock.courseStructure.childInfo.children[0];
Expand Down
8 changes: 7 additions & 1 deletion src/course-outline/card-header/CardHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import messages from './messages';
const CardHeader = ({
title,
status,
sectionId,
hasChanges,
isExpanded,
onClickPublish,
Expand Down Expand Up @@ -58,7 +59,11 @@ const CardHeader = ({
});

return (
<div className="item-card-header" data-testid={`${namePrefix}-card-header`}>
<div
className="item-card-header"
data-locator={sectionId}
data-testid={`${namePrefix}-card-header`}
>
{isFormOpen ? (
<Form.Group className="m-0">
<Form.Control
Expand Down Expand Up @@ -167,6 +172,7 @@ const CardHeader = ({
CardHeader.propTypes = {
title: PropTypes.string.isRequired,
status: PropTypes.string.isRequired,
sectionId: PropTypes.string.isRequired,
hasChanges: PropTypes.bool.isRequired,
isExpanded: PropTypes.bool.isRequired,
onExpand: PropTypes.func.isRequired,
Expand Down
24 changes: 22 additions & 2 deletions src/course-outline/hooks.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useSearchParams } from 'react-router-dom';
import { useToggle } from '@edx/paragon';

import { RequestStatus } from '../data/constants';
Expand Down Expand Up @@ -211,5 +212,24 @@ const useCourseOutline = ({ courseId }) => {
};
};

// eslint-disable-next-line import/prefer-default-export
export { useCourseOutline };
const useScrollToLocatorElement = ({ isLoading }) => {
const [searchParams] = useSearchParams();

useEffect(() => {
const locator = searchParams.get('show');
if (!locator) {
return;
}

const locatorToShow = document.querySelector(`[data-locator="${locator}"]`);
if (!locatorToShow) {
return;
}
locatorToShow.scrollIntoView({ behavior: 'smooth' });
}, [isLoading]);
};

export {
useCourseOutline,
useScrollToLocatorElement,
};
13 changes: 11 additions & 2 deletions src/course-outline/subsection-card/SubsectionCard.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState, useRef } from 'react';
import PropTypes from 'prop-types';
import { useDispatch } from 'react-redux';
import { useSearchParams } from 'react-router-dom';
import { useIntl } from '@edx/frontend-platform/i18n';
import { Button, useToggle } from '@edx/paragon';
import { Add as IconAdd } from '@edx/paragon/icons';
Expand All @@ -24,7 +25,10 @@ const SubsectionCard = ({
const currentRef = useRef(null);
const intl = useIntl();
const dispatch = useDispatch();
const [isExpanded, setIsExpanded] = useState(false);
const [searchParams] = useSearchParams();
const locatorId = searchParams.get('show');
const isScrolledToElement = locatorId === subsection.id;
const [isExpanded, setIsExpanded] = useState(locatorId ? isScrolledToElement : false);
const [isFormOpen, openForm, closeForm] = useToggle(false);

const {
Expand Down Expand Up @@ -81,7 +85,12 @@ const SubsectionCard = ({
}, [savingStatus]);

return (
<div className="subsection-card" data-testid="subsection-card" ref={currentRef}>
<div
className="subsection-card"
data-locator={subsection.id}
data-testid="subsection-card"
ref={currentRef}
>
<CardHeader
title={displayName}
status={subsectionStatus}
Expand Down
51 changes: 31 additions & 20 deletions src/course-outline/subsection-card/SubsectionCard.test.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { render, fireEvent } from '@testing-library/react';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import { AppProvider } from '@edx/frontend-platform/react';
Expand Down Expand Up @@ -38,25 +38,27 @@ const subsection = {

const onEditSubectionSubmit = jest.fn();

const renderComponent = (props) => render(
<AppProvider store={store}>
<IntlProvider locale="en">
<SubsectionCard
section={section}
subsection={subsection}
onOpenPublishModal={jest.fn()}
onOpenHighlightsModal={jest.fn()}
onOpenDeleteModal={jest.fn()}
onEditClick={jest.fn()}
savingStatus=""
onEditSubmit={onEditSubectionSubmit}
onDuplicateSubmit={jest.fn()}
namePrefix="subsection"
{...props}
>
<span>children</span>
</SubsectionCard>
</IntlProvider>,
const renderComponent = (props, entry = '/') => render(
<AppProvider store={store} wrapWithRouter={false}>
<MemoryRouter initialEntries={[entry]}>
<IntlProvider locale="en">
<SubsectionCard
section={section}
subsection={subsection}
onOpenPublishModal={jest.fn()}
onOpenHighlightsModal={jest.fn()}
onOpenDeleteModal={jest.fn()}
onEditClick={jest.fn()}
savingStatus=""
onEditSubmit={onEditSubectionSubmit}
onDuplicateSubmit={jest.fn()}
namePrefix="subsection"
{...props}
>
<span>children</span>
</SubsectionCard>
</IntlProvider>
</MemoryRouter>
</AppProvider>,
);

Expand Down Expand Up @@ -122,4 +124,13 @@ describe('<SubsectionCard />', () => {
fireEvent.keyDown(editField, { key: 'Enter', keyCode: 13 });
expect(onEditSubectionSubmit).toHaveBeenCalled();
});

it('check extended section when URL has a "show" param', async () => {
const { findByTestId } = renderComponent(null, `?show=${section.id}`);

const cardUnits = await findByTestId('subsection-card__units');
const newUnitButton = await findByTestId('new-unit-button');
expect(cardUnits).toBeInTheDocument();
expect(newUnitButton).toBeInTheDocument();
});
});
Loading