Skip to content

Commit 14c03d8

Browse files
authored
fix: add missing translation for notices not found (#612)
1 parent 5562d8a commit 14c03d8

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/components/NoticesWrapper/api.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
import { getConfig } from '@edx/frontend-platform';
22
import { getAuthenticatedHttpClient, getAuthenticatedUser } from '@edx/frontend-platform/auth';
33
import { logError, logInfo } from '@edx/frontend-platform/logging';
4-
import messages from './messages';
54

65
export const noticesUrl = `${getConfig().LMS_BASE_URL}/notices/api/v1/unacknowledged`;
76

8-
// Export the error message for backward compatibility with tests
9-
export const error404Message = messages.error404Message.defaultMessage;
10-
11-
export const getNotices = ({ onLoad }) => {
7+
export const getNotices = ({ onLoad, notFoundMessage }) => {
128
const authenticatedUser = getAuthenticatedUser();
139

1410
const handleError = async (e) => {
1511
// Error probably means that notices is not installed, which is fine.
1612
const { customAttributes: { httpErrorStatus } } = e;
1713
if (httpErrorStatus === 404) {
18-
logInfo(`${e}. ${error404Message}`);
14+
logInfo(`${e}. ${notFoundMessage}`);
1915
} else {
2016
logError(e);
2117
}

src/components/NoticesWrapper/hooks.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React from 'react';
22
import { getConfig } from '@edx/frontend-platform';
3+
import { useIntl } from 'react-intl';
34

45
import { StrictDict } from 'utils';
56
import { getNotices } from './api';
67
import * as module from './hooks';
8+
import messages from './messages';
79

810
/**
911
* This component uses the platform-plugin-notices plugin to function.
@@ -17,6 +19,8 @@ export const state = StrictDict({
1719

1820
export const useNoticesWrapperData = () => {
1921
const [isRedirected, setIsRedirected] = module.state.isRedirected();
22+
const { formatMessage } = useIntl();
23+
2024
React.useEffect(() => {
2125
if (getConfig().ENABLE_NOTICES) {
2226
getNotices({
@@ -26,9 +30,10 @@ export const useNoticesWrapperData = () => {
2630
window.location.replace(`${data.data.results[0]}?next=${window.location.href}`);
2731
}
2832
},
33+
notFoundMessage: formatMessage(messages.error404Message),
2934
});
3035
}
31-
}, [setIsRedirected]);
36+
}, [setIsRedirected, formatMessage]);
3237
return { isRedirected };
3338
};
3439

src/components/NoticesWrapper/hooks.test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import * as hooks from './hooks';
88

99
jest.mock('@edx/frontend-platform', () => ({ getConfig: jest.fn() }));
1010
jest.mock('./api', () => ({ getNotices: jest.fn() }));
11+
const mockFormatMessage = jest.fn(message => message.defaultMessage || 'translated-string');
12+
jest.mock('react-intl', () => ({
13+
useIntl: () => ({
14+
formatMessage: mockFormatMessage,
15+
}),
16+
}));
1117

1218
getConfig.mockReturnValue({ ENABLE_NOTICES: true });
1319
const state = new MockUseState(hooks);
@@ -34,7 +40,7 @@ describe('NoticesWrapper hooks', () => {
3440
getConfig.mockReturnValueOnce({ ENABLE_NOTICES: false });
3541
hooks.useNoticesWrapperData();
3642
const [cb, prereqs] = React.useEffect.mock.calls[0];
37-
expect(prereqs).toEqual([state.setState.isRedirected]);
43+
expect(prereqs).toEqual([state.setState.isRedirected, mockFormatMessage]);
3844
cb();
3945
expect(getNotices).not.toHaveBeenCalled();
4046
});
@@ -43,7 +49,7 @@ describe('NoticesWrapper hooks', () => {
4349
hooks.useNoticesWrapperData();
4450
expect(React.useEffect).toHaveBeenCalled();
4551
const [cb, prereqs] = React.useEffect.mock.calls[0];
46-
expect(prereqs).toEqual([state.setState.isRedirected]);
52+
expect(prereqs).toEqual([state.setState.isRedirected, mockFormatMessage]);
4753
cb();
4854
expect(getNotices).toHaveBeenCalled();
4955
const { onLoad } = getNotices.mock.calls[0][0];
@@ -59,7 +65,7 @@ describe('NoticesWrapper hooks', () => {
5965
window.location = { replace: jest.fn(), href: 'test-old-href' };
6066
hooks.useNoticesWrapperData();
6167
const [cb, prereqs] = React.useEffect.mock.calls[0];
62-
expect(prereqs).toEqual([state.setState.isRedirected]);
68+
expect(prereqs).toEqual([state.setState.isRedirected, mockFormatMessage]);
6369
cb();
6470
expect(getNotices).toHaveBeenCalled();
6571
const { onLoad } = getNotices.mock.calls[0][0];

0 commit comments

Comments
 (0)