Skip to content

Commit cbb35e7

Browse files
authored
fix: null error at useRouteMatch when running on tutor (#623)
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 cf8ef15 commit cbb35e7

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
@@ -465,6 +465,20 @@ describe('ThreadView', () => {
465465
assertLastUpdateData({ pinned: false });
466466
});
467467

468+
it('should allow copying a link to the post', async () => {
469+
await waitFor(() => renderComponent(discussionPostId));
470+
const post = await screen.findByTestId('post-thread-1');
471+
const hoverCard = within(post).getByTestId('hover-card-thread-1');
472+
Object.assign(navigator, { clipboard: { writeText: jest.fn() } });
473+
await act(async () => {
474+
fireEvent.click(within(hoverCard).getByRole('button', { name: /actions menu/i }));
475+
});
476+
await act(async () => {
477+
fireEvent.click(within(hoverCard).getByRole('button', { name: /copy link/i }));
478+
});
479+
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(`http://localhost/${courseId}/posts/${discussionPostId}`);
480+
});
481+
468482
it('should allow reporting the post', async () => {
469483
await waitFor(() => renderComponent(discussionPostId));
470484
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));
@@ -78,8 +78,7 @@ const Post = ({ handleAddResponseButton }) => {
7878
}, [closed, postId, reasonCodesEnabled, showClosePostModal]);
7979

8080
const handlePostCopyLink = useCallback(() => {
81-
const postURL = new URL(`${getConfig().PUBLIC_PATH}${courseId}/posts/${postId}`, window.location.origin);
82-
navigator.clipboard.writeText(postURL.href);
81+
navigator.clipboard.writeText(getFullUrl(`${courseId}/posts/${postId}`));
8382
}, [window.location.origin, postId, courseId]);
8483

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

0 commit comments

Comments
 (0)