Skip to content

Commit

Permalink
fix: don't display SUPPORT_URL and Progams if is not confogured
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoa committed Sep 29, 2024
1 parent 1007dc4 commit fb3a02b
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 21 deletions.
1 change: 0 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ LMS_CLIENT_ID='login-service-client-id'
SEGMENT_KEY=''
FEATURE_FLAGS={}
MARKETING_SITE_BASE_URL='http://localhost:18000'
SUPPORT_URL='http://localhost:18000/support'
CONTACT_URL='http://localhost:18000/contact'
OPEN_SOURCE_URL='http://localhost:18000/openedx'
TERMS_OF_SERVICE_URL='http://localhost:18000/terms-of-service'
Expand Down
14 changes: 8 additions & 6 deletions src/containers/LearnerDashboardHeader/LearnerDashboardMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const getLearnerHeaderMenu = (
courseSearchUrl,
authenticatedUser,
exploreCoursesClick,
programsEnabled = false,
) => ({
mainMenu: [
{
Expand All @@ -17,11 +18,11 @@ const getLearnerHeaderMenu = (
content: formatMessage(messages.course),
isActive: true,
},
{
...(programsEnabled ? [{
type: 'item',
href: `${urls.programsUrl()}`,
content: formatMessage(messages.program),
},
}] : []),
{
type: 'item',
href: `${urls.baseAppUrl(courseSearchUrl)}`,
Expand All @@ -32,11 +33,11 @@ const getLearnerHeaderMenu = (
},
],
secondaryMenu: [
{
...(getConfig().SUPPORT_URL ? [{
type: 'item',
href: `${getConfig().SUPPORT_URL}`,
href: getConfig().SUPPORT_URL,
content: formatMessage(messages.help),
},
}] : []),
],
userMenu: [
{
Expand Down Expand Up @@ -70,6 +71,7 @@ const getLearnerHeaderMenu = (
],
},
],
});
}
);

export default getLearnerHeaderMenu;
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@ exports[`LearnerDashboardHeader render 1`] = `
},
]
}
secondaryMenuItems={
[
{
"content": "Help",
"href": "http://localhost:18000/support",
"type": "item",
},
]
}
secondaryMenuItems={[]}
userMenuItems={
[
{
Expand Down
4 changes: 3 additions & 1 deletion src/containers/LearnerDashboardHeader/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import track from 'tracking';
import { StrictDict } from 'utils';
import { linkNames } from 'tracking/constants';

import { apiHooks } from 'hooks';
import getLearnerHeaderMenu from './LearnerDashboardMenu';

import * as module from './hooks';
Expand All @@ -30,8 +31,9 @@ export const findCoursesNavDropdownClicked = (href) => track.findCourses.findCou
export const useLearnerDashboardHeaderMenu = ({
courseSearchUrl, authenticatedUser, exploreCoursesClick,
}) => {
const { enabled: programsEnabled } = apiHooks.useProgramsConfig();
const { formatMessage } = useIntl();
return getLearnerHeaderMenu(formatMessage, courseSearchUrl, authenticatedUser, exploreCoursesClick);
return getLearnerHeaderMenu(formatMessage, courseSearchUrl, authenticatedUser, exploreCoursesClick, programsEnabled);
};

export const useLearnerDashboardHeaderData = () => {
Expand Down
24 changes: 20 additions & 4 deletions src/containers/LearnerDashboardHeader/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { linkNames } from 'tracking/constants';

import { MockUseState } from 'testUtils';

import { apiHooks } from 'hooks';
import { mergeConfig } from '@edx/frontend-platform';
import * as hooks from './hooks';

const state = new MockUseState(hooks);
Expand All @@ -22,6 +24,12 @@ jest.mock('tracking', () => ({
},
}));

jest.mock('hooks', () => ({
apiHooks: {
useProgramsConfig: jest.fn(() => ({})),
},
}));

const url = 'http://example.com';

describe('LearnerDashboardHeader hooks', () => {
Expand Down Expand Up @@ -50,13 +58,21 @@ describe('LearnerDashboardHeader hooks', () => {
});

describe('getLearnerDashboardHeaderMenu', () => {
const courseSearchUrl = '/courses';
const authenticatedUser = {
username: 'test',
};
test('calls header menu data hook', () => {
const courseSearchUrl = '/courses';
const authenticatedUser = {
username: 'test',
};
const learnerHomeHeaderMenu = useLearnerDashboardHeaderMenu({ courseSearchUrl, authenticatedUser });
expect(learnerHomeHeaderMenu.mainMenu.length).toBe(2);
expect(learnerHomeHeaderMenu.secondaryMenu.length).toBe(0);
});
test('should add programs and/or support link to menu if the service are configured', () => {
mergeConfig({ SUPPORT_URL: 'http://localhost:18000/support' });
apiHooks.useProgramsConfig.mockReturnValue({ enabled: true });
const learnerHomeHeaderMenu = useLearnerDashboardHeaderMenu({ courseSearchUrl, authenticatedUser });
expect(learnerHomeHeaderMenu.mainMenu.length).toBe(3);
expect(learnerHomeHeaderMenu.secondaryMenu.length).toBe(1);
});
});

Expand Down
3 changes: 3 additions & 0 deletions src/containers/LearnerDashboardHeader/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ jest.mock('hooks', () => ({
courseSearchUrl: '/course-search-url',
})),
},
apiHooks: {
useProgramsConfig: jest.fn(() => ({ enabled: true })),
},
}));
jest.mock('./hooks', () => ({
...jest.requireActual('./hooks'),
Expand Down
3 changes: 3 additions & 0 deletions src/data/services/lms/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const initializeList = ({ user } = {}) => get(
stringifyUrl(urls.getInitApiUrl(), { [apiKeys.user]: user }),
);

export const getProgramsConfig = () => get(urls.programsConfigUrl());

export const updateEntitlementEnrollment = ({ uuid, courseId }) => post(
urls.entitlementEnrollment(uuid),
{ [apiKeys.courseRunId]: courseId },
Expand Down Expand Up @@ -73,6 +75,7 @@ export const createCreditRequest = ({ providerId, courseId, username }) => post(

export default {
initializeList,
getProgramsConfig,
unenrollFromCourse,
updateEmailSettings,
updateEntitlementEnrollment,
Expand Down
2 changes: 2 additions & 0 deletions src/data/services/lms/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const baseAppUrl = (url) => updateUrl(getBaseUrl(), url);
export const learningMfeUrl = (url) => updateUrl(getConfig().LEARNING_BASE_URL, url);

// static view url
const programsConfigUrl = () => baseAppUrl('/config/programs');
const programsUrl = () => baseAppUrl('/dashboard/programs');

export const creditPurchaseUrl = (courseId) => `${getEcommerceUrl()}/credit/checkout/${courseId}/`;
Expand All @@ -37,6 +38,7 @@ export default StrictDict({
event,
getInitApiUrl,
learningMfeUrl,
programsConfigUrl,
programsUrl,
updateEmailSettings,
});
19 changes: 19 additions & 0 deletions src/hooks/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ export const useInitializeApp = () => {
});
};

export const useProgramsConfig = () => {
const [config, setConfig] = React.useState({});

React.useEffect(() => {
const fetchProgramsConfig = async () => {
try {
const { data } = await api.getProgramsConfig();
setConfig(data);

Check warning on line 41 in src/hooks/api.js

View check run for this annotation

Codecov / codecov/patch

src/hooks/api.js#L41

Added line #L41 was not covered by tests
} catch (error) {
console.error('Error accessing programs configuration', error);

Check warning on line 43 in src/hooks/api.js

View workflow job for this annotation

GitHub Actions / tests (18)

Unexpected console statement

Check warning on line 43 in src/hooks/api.js

View workflow job for this annotation

GitHub Actions / tests (20)

Unexpected console statement
}
};

fetchProgramsConfig();
}, []);

return config;
};

export const useNewEntitlementEnrollment = (cardId) => {
const { uuid } = reduxHooks.useCardEntitlementData(cardId);
const onSuccess = module.useInitializeApp();
Expand Down

0 comments on commit fb3a02b

Please sign in to comment.