From 14c03d84610d94ff94d37edd1b0bd71888a137c6 Mon Sep 17 00:00:00 2001 From: Victor Navarro Date: Thu, 15 May 2025 09:57:21 -0600 Subject: [PATCH] fix: add missing translation for notices not found (#612) --- src/components/NoticesWrapper/api.js | 8 ++------ src/components/NoticesWrapper/hooks.js | 7 ++++++- src/components/NoticesWrapper/hooks.test.js | 12 +++++++++--- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/components/NoticesWrapper/api.js b/src/components/NoticesWrapper/api.js index 6c64ab4..7228424 100644 --- a/src/components/NoticesWrapper/api.js +++ b/src/components/NoticesWrapper/api.js @@ -1,21 +1,17 @@ import { getConfig } from '@edx/frontend-platform'; import { getAuthenticatedHttpClient, getAuthenticatedUser } from '@edx/frontend-platform/auth'; import { logError, logInfo } from '@edx/frontend-platform/logging'; -import messages from './messages'; export const noticesUrl = `${getConfig().LMS_BASE_URL}/notices/api/v1/unacknowledged`; -// Export the error message for backward compatibility with tests -export const error404Message = messages.error404Message.defaultMessage; - -export const getNotices = ({ onLoad }) => { +export const getNotices = ({ onLoad, notFoundMessage }) => { const authenticatedUser = getAuthenticatedUser(); const handleError = async (e) => { // Error probably means that notices is not installed, which is fine. const { customAttributes: { httpErrorStatus } } = e; if (httpErrorStatus === 404) { - logInfo(`${e}. ${error404Message}`); + logInfo(`${e}. ${notFoundMessage}`); } else { logError(e); } diff --git a/src/components/NoticesWrapper/hooks.js b/src/components/NoticesWrapper/hooks.js index 5c97608..3c398ae 100644 --- a/src/components/NoticesWrapper/hooks.js +++ b/src/components/NoticesWrapper/hooks.js @@ -1,9 +1,11 @@ import React from 'react'; import { getConfig } from '@edx/frontend-platform'; +import { useIntl } from 'react-intl'; import { StrictDict } from 'utils'; import { getNotices } from './api'; import * as module from './hooks'; +import messages from './messages'; /** * This component uses the platform-plugin-notices plugin to function. @@ -17,6 +19,8 @@ export const state = StrictDict({ export const useNoticesWrapperData = () => { const [isRedirected, setIsRedirected] = module.state.isRedirected(); + const { formatMessage } = useIntl(); + React.useEffect(() => { if (getConfig().ENABLE_NOTICES) { getNotices({ @@ -26,9 +30,10 @@ export const useNoticesWrapperData = () => { window.location.replace(`${data.data.results[0]}?next=${window.location.href}`); } }, + notFoundMessage: formatMessage(messages.error404Message), }); } - }, [setIsRedirected]); + }, [setIsRedirected, formatMessage]); return { isRedirected }; }; diff --git a/src/components/NoticesWrapper/hooks.test.js b/src/components/NoticesWrapper/hooks.test.js index fc26586..d08f54f 100644 --- a/src/components/NoticesWrapper/hooks.test.js +++ b/src/components/NoticesWrapper/hooks.test.js @@ -8,6 +8,12 @@ import * as hooks from './hooks'; jest.mock('@edx/frontend-platform', () => ({ getConfig: jest.fn() })); jest.mock('./api', () => ({ getNotices: jest.fn() })); +const mockFormatMessage = jest.fn(message => message.defaultMessage || 'translated-string'); +jest.mock('react-intl', () => ({ + useIntl: () => ({ + formatMessage: mockFormatMessage, + }), +})); getConfig.mockReturnValue({ ENABLE_NOTICES: true }); const state = new MockUseState(hooks); @@ -34,7 +40,7 @@ describe('NoticesWrapper hooks', () => { getConfig.mockReturnValueOnce({ ENABLE_NOTICES: false }); hooks.useNoticesWrapperData(); const [cb, prereqs] = React.useEffect.mock.calls[0]; - expect(prereqs).toEqual([state.setState.isRedirected]); + expect(prereqs).toEqual([state.setState.isRedirected, mockFormatMessage]); cb(); expect(getNotices).not.toHaveBeenCalled(); }); @@ -43,7 +49,7 @@ describe('NoticesWrapper hooks', () => { hooks.useNoticesWrapperData(); expect(React.useEffect).toHaveBeenCalled(); const [cb, prereqs] = React.useEffect.mock.calls[0]; - expect(prereqs).toEqual([state.setState.isRedirected]); + expect(prereqs).toEqual([state.setState.isRedirected, mockFormatMessage]); cb(); expect(getNotices).toHaveBeenCalled(); const { onLoad } = getNotices.mock.calls[0][0]; @@ -59,7 +65,7 @@ describe('NoticesWrapper hooks', () => { window.location = { replace: jest.fn(), href: 'test-old-href' }; hooks.useNoticesWrapperData(); const [cb, prereqs] = React.useEffect.mock.calls[0]; - expect(prereqs).toEqual([state.setState.isRedirected]); + expect(prereqs).toEqual([state.setState.isRedirected, mockFormatMessage]); cb(); expect(getNotices).toHaveBeenCalled(); const { onLoad } = getNotices.mock.calls[0][0];