Skip to content

Commit b36c026

Browse files
authored
fix: null error at useRouteMatch when running on tutor (#613)
tutor sets the PUBLIC_PATH to '/discussions' which causes frontend-platform to treat all URLs for matching etc to be relative to this path. Since many places include '/discussions' in the match it causes those matches to break. This change makes the default PUBLIC_PATH in .env.development to match the one set by tutor and removes it from the base path of the router letting frontend platform handle the prefix. This also allows for deployments to customise this path to be something other than 'discussions'.
1 parent 0d5df18 commit b36c026

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

src/data/constants.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { getConfig } from '@edx/frontend-platform';
22

33
export const getApiBaseUrl = () => getConfig().LMS_BASE_URL;
4+
export const getFullUrl = (path) => (
5+
new URL(`${getConfig().PUBLIC_PATH.replace(/\/$/, '')}/${path}`, window.location.origin).href
6+
);
47

58
/**
69
* Enum for thread types.
@@ -137,7 +140,7 @@ export const DiscussionProvider = {
137140
OPEN_EDX: 'openedx',
138141
};
139142

140-
const BASE_PATH = `${getConfig().PUBLIC_PATH}:courseId`;
143+
const BASE_PATH = '/:courseId';
141144

142145
export const Routes = {
143146
DISCUSSIONS: {

src/discussions/post-comments/PostCommentsView.test.jsx

+14
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,20 @@ describe('ThreadView', () => {
442442
assertLastUpdateData({ pinned: false });
443443
});
444444

445+
it('should allow copying a link to the post', async () => {
446+
await waitFor(() => renderComponent(discussionPostId));
447+
const post = await screen.findByTestId('post-thread-1');
448+
const hoverCard = within(post).getByTestId('hover-card-thread-1');
449+
Object.assign(navigator, { clipboard: { writeText: jest.fn() } });
450+
await act(async () => {
451+
fireEvent.click(within(hoverCard).getByRole('button', { name: /actions menu/i }));
452+
});
453+
await act(async () => {
454+
fireEvent.click(within(hoverCard).getByRole('button', { name: /copy link/i }));
455+
});
456+
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(`http://localhost/${courseId}/posts/${discussionPostId}`);
457+
});
458+
445459
it('should allow reporting the post', async () => {
446460
await waitFor(() => renderComponent(discussionPostId));
447461
const post = await screen.findByTestId('post-thread-1');

src/discussions/posts/data/__factories__/threads.factory.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Factory.define('thread')
2626
'type',
2727
'voted',
2828
'pinned',
29+
'copy_link',
2930
],
3031
author: 'test_user',
3132
author_label: 'Staff',

src/discussions/posts/post/Post.jsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { useIntl } from '@edx/frontend-platform/i18n';
1111
import { Hyperlink, useToggle } from '@edx/paragon';
1212

1313
import HTMLLoader from '../../../components/HTMLLoader';
14-
import { ContentActions } from '../../../data/constants';
14+
import { ContentActions, getFullUrl } from '../../../data/constants';
1515
import { selectorForUnitSubsection, selectTopicContext } from '../../../data/selectors';
1616
import { AlertBanner, Confirmation } from '../../common';
1717
import { DiscussionContext } from '../../common/context';
@@ -37,7 +37,7 @@ const Post = ({ handleAddResponseButton }) => {
3737
const location = useLocation();
3838
const history = useHistory();
3939
const dispatch = useDispatch();
40-
const courseId = useSelector((state) => state.config.id);
40+
const { courseId } = useContext(DiscussionContext);
4141
const topic = useSelector(selectTopic(topicId));
4242
const getTopicSubsection = useSelector(selectorForUnitSubsection);
4343
const topicContext = useSelector(selectTopicContext(topicId));
@@ -75,8 +75,7 @@ const Post = ({ handleAddResponseButton }) => {
7575
}, [closed, postId, showClosePostModal]);
7676

7777
const handlePostCopyLink = useCallback(() => {
78-
const postURL = new URL(`${getConfig().PUBLIC_PATH}${courseId}/posts/${postId}`, window.location.origin);
79-
navigator.clipboard.writeText(postURL.href);
78+
navigator.clipboard.writeText(getFullUrl(`${courseId}/posts/${postId}`));
8079
}, [window.location.origin, postId, courseId]);
8180

8281
const handlePostPin = useCallback(() => dispatch(

0 commit comments

Comments
 (0)