Skip to content

Commit 1484bc5

Browse files
refactor: replace query-string pkg, remove unused <ShareButton> (#1676)
1 parent 6b197aa commit 1484bc5

File tree

10 files changed

+101
-229
lines changed

10 files changed

+101
-229
lines changed

package-lock.json

Lines changed: 0 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
"lodash.camelcase": "4.3.0",
6060
"postcss-loader": "^8.1.1",
6161
"prop-types": "15.8.1",
62-
"query-string": "^7.1.3",
6362
"react": "^18.3.1",
6463
"react-dom": "^18.3.1",
6564
"react-helmet": "6.1.0",

src/courseware/RedirectPage.test.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jest.mock('react-router-dom', () => ({
1616
useLocation: () => ({
1717
search: '?consentPath=/some-path',
1818
}),
19+
useSearchParams: () => [new URLSearchParams('?consentPath=/some-path'), () => {}],
1920
}));
2021

2122
describe('RedirectPage component', () => {

src/courseware/RedirectPage.jsx renamed to src/courseware/RedirectPage.tsx

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import PropTypes from 'prop-types';
21
import {
3-
generatePath, useParams, useLocation,
2+
generatePath, useParams, useLocation, useSearchParams,
43
} from 'react-router-dom';
54
import { getConfig } from '@edx/frontend-platform';
65

7-
import queryString from 'query-string';
86
import { REDIRECT_MODES } from '../constants';
97

10-
const RedirectPage = ({
11-
pattern, mode,
12-
}) => {
8+
interface Props {
9+
pattern: string;
10+
mode: string;
11+
}
12+
13+
const RedirectPage = ({ pattern = '', mode }: Props) => {
1314
const { courseId } = useParams();
1415
const location = useLocation();
15-
const { consentPath } = queryString.parse(location?.search);
16+
const [searchParams] = useSearchParams();
17+
const consentPath = searchParams.get('consentPath') ?? '';
1618

1719
const {
1820
LMS_BASE_URL,
@@ -39,13 +41,4 @@ const RedirectPage = ({
3941
return null;
4042
};
4143

42-
RedirectPage.propTypes = {
43-
pattern: PropTypes.string,
44-
mode: PropTypes.string.isRequired,
45-
};
46-
47-
RedirectPage.defaultProps = {
48-
pattern: null,
49-
};
50-
5144
export default RedirectPage;

src/courseware/course/sequence/Unit/urls.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/courseware/course/sequence/Unit/urls.test.js

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { getConfig } from '@edx/frontend-platform';
2+
import { getIFrameUrl } from './urls';
3+
4+
jest.mock('@edx/frontend-platform', () => ({
5+
getConfig: jest.fn(),
6+
}));
7+
const config = { LMS_BASE_URL: 'https://test-lms-url' };
8+
getConfig.mockReturnValue(config);
9+
10+
const props = {
11+
id: 'test-id',
12+
view: 'test-view',
13+
format: 'test-format',
14+
examAccess: { blockAccess: false, accessToken: 'test-access-token' },
15+
preview: false,
16+
};
17+
18+
describe('urls module getIFrameUrl', () => {
19+
test('format provided, exam access and token available', () => {
20+
expect(getIFrameUrl(props)).toEqual('https://test-lms-url/xblock/test-id?exam_access=test-access-token&format=test-format&preview=false&recheck_access=1&show_bookmark=0&show_title=0&view=test-view');
21+
});
22+
test('no format provided, exam access blocked', () => {
23+
expect(getIFrameUrl({
24+
id: props.id,
25+
view: props.view,
26+
preview: props.preview,
27+
examAccess: { blockAccess: true },
28+
})).toEqual('https://test-lms-url/xblock/test-id?preview=false&recheck_access=1&show_bookmark=0&show_title=0&view=test-view');
29+
});
30+
test('jumpToId and fragmentIdentifier is added to url', () => {
31+
expect(getIFrameUrl({
32+
...props,
33+
jumpToId: 'some-xblock-id',
34+
})).toEqual('https://test-lms-url/xblock/test-id?exam_access=test-access-token&format=test-format&jumpToId=some-xblock-id&preview=false&recheck_access=1&show_bookmark=0&show_title=0&view=test-view#some-xblock-id');
35+
});
36+
test('preview is true and url param equals 1', () => {
37+
expect(getIFrameUrl({
38+
...props,
39+
preview: true,
40+
})).toEqual('https://test-lms-url/xblock/test-id?exam_access=test-access-token&format=test-format&preview=true&recheck_access=1&show_bookmark=0&show_title=0&view=test-view');
41+
});
42+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { getConfig } from '@edx/frontend-platform';
2+
3+
export const iframeParams = {
4+
show_title: 0,
5+
show_bookmark: 0,
6+
recheck_access: 1,
7+
};
8+
9+
interface Props {
10+
id: string;
11+
view: string;
12+
format?: string | null;
13+
examAccess: { blockAccess: boolean, accessToken?: string };
14+
jumpToId?: string;
15+
preview: boolean;
16+
}
17+
18+
export const getIFrameUrl = ({
19+
id,
20+
view,
21+
format = null,
22+
examAccess,
23+
jumpToId,
24+
preview,
25+
}: Props) => {
26+
const xblockUrl = new URL(`${getConfig().LMS_BASE_URL}/xblock/${id}`);
27+
for (const [key, value] of Object.entries(iframeParams)) {
28+
xblockUrl.searchParams.set(key, String(value));
29+
}
30+
xblockUrl.searchParams.set('view', view);
31+
xblockUrl.searchParams.set('preview', String(preview));
32+
if (format) {
33+
xblockUrl.searchParams.set('format', format);
34+
}
35+
if (!examAccess.blockAccess) {
36+
xblockUrl.searchParams.set('exam_access', examAccess.accessToken!);
37+
}
38+
// Pass jumpToId as query param as fragmentIdentifier is not passed to server.
39+
if (jumpToId) {
40+
xblockUrl.searchParams.set('jumpToId', jumpToId);
41+
xblockUrl.hash = `#${jumpToId}`; // this is used by browser to scroll to correct block.
42+
}
43+
xblockUrl.searchParams.sort();
44+
return xblockUrl.toString();
45+
};
46+
47+
export default {
48+
getIFrameUrl,
49+
};

src/courseware/course/share/ShareButton.jsx

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/courseware/course/share/messages.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)