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: use react-router to properly handle basepaths for internal links #422

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@edx/frontend-platform": "^4.0.0 || ^5.0.0 || ^6.0.0",
"prop-types": "^15.5.10",
"react": "^16.9.0 || ^17.0.0",
"react-dom": "^16.9.0 || ^17.0.0"
"react-dom": "^16.9.0 || ^17.0.0",
"react-router-dom": "^6.14.2"
}
}
43 changes: 25 additions & 18 deletions src/studio-header/CourseLockUp.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { useNavigate, useResolvedPath } from 'react-router-dom';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import {
OverlayTrigger,
Expand All @@ -14,26 +15,32 @@
title,
// injected
intl,
}) => (
<OverlayTrigger
placement="bottom"
overlay={(
<Tooltip id="course-lock-up">
{title}
</Tooltip>
}) => {
const navigate = useNavigate();
const resolvedPath = useResolvedPath(outlineLink);

return (
<OverlayTrigger
placement="bottom"
overlay={(
<Tooltip id="course-lock-up">
{title}
</Tooltip>
)}
>
<a
className="course-title-lockup w-25 mr-2"
href={outlineLink}
aria-label={intl.formatMessage(messages['header.label.courseOutline'])}
data-testid="course-lock-up-block"
>
<span className="d-block small m-0 text-gray-800" data-testid="course-org-number">{org} {number}</span>
<span className="d-block m-0 font-weight-bold text-gray-800" data-testid="course-title">{title}</span>
</a>
</OverlayTrigger>
);
<a
className="course-title-lockup w-25 mr-2"
href={resolvedPath.pathname}
onClick={(e) => { e.preventDefault(); navigate(resolvedPath.pathname); }}

Check warning on line 34 in src/studio-header/CourseLockUp.jsx

View check run for this annotation

Codecov / codecov/patch

src/studio-header/CourseLockUp.jsx#L34

Added line #L34 was not covered by tests
aria-label={intl.formatMessage(messages['header.label.courseOutline'])}
data-testid="course-lock-up-block"
>
<span className="d-block small m-0 text-gray-800" data-testid="course-org-number">{org} {number}</span>
<span className="d-block m-0 font-weight-bold text-gray-800" data-testid="course-title">{title}</span>
</a>
</OverlayTrigger>
);
};

CourseLockUp.propTypes = {
number: PropTypes.string,
Expand Down
12 changes: 9 additions & 3 deletions src/studio-header/MobileMenu.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { NavLink } from 'react-router-dom';
import { Collapsible } from '@edx/paragon';

const MobileMenu = ({
Expand All @@ -21,9 +22,14 @@
<ul className="p-0" style={{ listStyleType: 'none' }}>
{items.map(item => (
<li className="mobile-menu-item">
<a href={item.href}>
{item.title}
</a>
{(
/^(?:\w+:)?\/\//.test(item.href)
? (
<a href={item.href}>

Check warning on line 28 in src/studio-header/MobileMenu.jsx

View check run for this annotation

Codecov / codecov/patch

src/studio-header/MobileMenu.jsx#L28

Added line #L28 was not covered by tests
{item.title}
</a>
) : <NavLink to={item.href}>{item.title}</NavLink>
)}
</li>
))}
</ul>
Expand Down
42 changes: 34 additions & 8 deletions src/studio-header/NavDropdownMenu.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
import React from 'react';
import PropTypes from 'prop-types';
import { useNavigate, useResolvedPath } from 'react-router-dom';

import {
Dropdown,
DropdownButton,
} from '@edx/paragon';

const NavDropdownItem = ({ item }) => {
const navigate = useNavigate();
const resolvedPath = useResolvedPath(item.href);

return (
<Dropdown.Item
href={resolvedPath.pathname}
onClick={(e) => { e.preventDefault(); navigate(resolvedPath.pathname); }}

Check warning on line 17 in src/studio-header/NavDropdownMenu.jsx

View check run for this annotation

Codecov / codecov/patch

src/studio-header/NavDropdownMenu.jsx#L17

Added line #L17 was not covered by tests
className="small"
>
{item.title}
</Dropdown.Item>
);
};

NavDropdownItem.propTypes = {
item: PropTypes.shape({
href: PropTypes.string,
title: PropTypes.string,
}).isRequired,
};

const NavDropdownMenu = ({
id,
buttonTitle,
Expand All @@ -15,14 +39,16 @@
title={buttonTitle}
variant="tertiary"
>
{items.map(item => (
<Dropdown.Item
href={item.href}
className="small"
>
{item.title}
</Dropdown.Item>
))}
{items.map(item => (/^(?:\w+:)?\/\//.test(item.href)
? (
<Dropdown.Item
href={item.href}
className="small"
>
{item.title}
</Dropdown.Item>
)
: <NavDropdownItem item={item} />))}
</DropdownButton>
);

Expand Down
21 changes: 12 additions & 9 deletions src/studio-header/StudioHeader.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fireEvent,
waitFor,
} from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';

import { AppContext } from '@edx/frontend-platform/react';
import { IntlProvider } from '@edx/frontend-platform/i18n';
Expand Down Expand Up @@ -40,15 +41,17 @@ const RootWrapper = ({

return (
// eslint-disable-next-line react/jsx-no-constructed-context-values, react/prop-types
<IntlProvider locale="en">
<AppContext.Provider value={appContextValue}>
<ResponsiveContext.Provider value={responsiveContextValue}>
<StudioHeader
{...props}
/>
</ResponsiveContext.Provider>
</AppContext.Provider>
</IntlProvider>
<MemoryRouter>
<IntlProvider locale="en">
<AppContext.Provider value={appContextValue}>
<ResponsiveContext.Provider value={responsiveContextValue}>
<StudioHeader
{...props}
/>
</ResponsiveContext.Provider>
</AppContext.Provider>
</IntlProvider>
</MemoryRouter>
);
};

Expand Down
4 changes: 2 additions & 2 deletions src/studio-header/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const getUserMenuItems = ({
}) => {
let items = [
{
href: `${studioBaseUrl}}`,
href: `${studioBaseUrl}`,
title: intl.formatMessage(messages['header.user.menu.studio']),
}, {
href: `${logoutUrl}`,
Expand All @@ -18,7 +18,7 @@ const getUserMenuItems = ({
if (isAdmin) {
items = [
{
href: `${studioBaseUrl}}`,
href: `${studioBaseUrl}`,
title: intl.formatMessage(messages['header.user.menu.studio']),
}, {
href: `${studioBaseUrl}/maintenance`,
Expand Down