Skip to content

Commit

Permalink
fix: force LMS url to reload when changed
Browse files Browse the repository at this point in the history
 Just change makes it possible if LMS_BASE_URL is updated by
 dynamic config to get the latest value.
  • Loading branch information
ghassanmas committed Apr 18, 2023
1 parent 5e716ec commit 68cd4d9
Show file tree
Hide file tree
Showing 9 changed files with 2,448 additions and 6,672 deletions.
9,081 changes: 2,425 additions & 6,656 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"whatwg-fetch": "^3.6.2"
},
"devDependencies": {
"@edx/frontend-build": "11.0.1",
"@edx/frontend-build": "12.4.0",
"@edx/reactifex": "^2.1.1",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ jest.mock('containers/LearnerDashboardHeader/hooks', () => ({
findCoursesNavDropdownClicked: (href) => jest.fn().mockName(`findCoursesNavDropdownClicked('${href}')`),
}));

jest.mock('data/services/lms/urls',()=>({
programsUrl: 'http://localhost:18000/dashboard/programs',
}));

const config = {
ACCOUNT_PROFILE_URL: 'http://account-profile-url.test',
ACCOUNT_SETTINGS_URL: 'http://account-settings-url.test',
Expand Down
2 changes: 1 addition & 1 deletion src/data/services/lms/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as module from './api';
* GET Actions
*********************************************************************************/
export const initializeList = ({ user } = {}) => get(
stringifyUrl(urls.init, { [apiKeys.user]: user }),
stringifyUrl(urls.getInitApi(), { [apiKeys.user]: user }),
);

export const updateEntitlementEnrollment = ({ uuid, courseId }) => post(
Expand Down
2 changes: 1 addition & 1 deletion src/data/services/lms/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('lms api methods', () => {
[apiKeys.user]: testUser,
};
expect(api.initializeList(userArg)).toEqual(
utils.get(utils.stringifyUrl(urls.init, userArg)),
utils.get(utils.stringifyUrl(urls.getInitApi(), userArg)),
);
});
});
Expand Down
19 changes: 11 additions & 8 deletions src/data/services/lms/urls.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { StrictDict } from 'utils';
import { configuration } from 'config';

import { getConfig } from '@edx/frontend-platform';

const baseUrl = `${configuration.LMS_BASE_URL}`;
export const ecommerceUrl = `${configuration.ECOMMERCE_PUBLIC_URL_ROOT}`;

export const api = `${baseUrl}/api`;
export const getApi = () => ( `${getConfig().LMS_BASE_URL}/api`);

// const init = `${api}learner_home/mock/init`; // mock endpoint for testing
const init = `${api}/learner_home/init`;
// const getInitApi = `${getApi()}learner_home/mock/init`; // mock endpoint for testing
const getInitApi = () => (`${getApi()}/learner_home/init`);

const event = `${baseUrl}/event`;
const courseUnenroll = `${baseUrl}/change_enrollment`;
const updateEmailSettings = `${api}/change_email_settings`;
const entitlementEnrollment = (uuid) => `${api}/entitlements/v1/entitlements/${uuid}/enrollments`;
const updateEmailSettings = `${getApi()}/change_email_settings`;
const entitlementEnrollment = (uuid) => `${getApi()}/entitlements/v1/entitlements/${uuid}/enrollments`;

// if url is null or absolute, return it as is
const updateUrl = (base, url) => ((url == null || url.startsWith('http://') || url.startsWith('https://')) ? url : `${base}${url}`);
Expand All @@ -24,18 +26,19 @@ export const learningMfeUrl = (url) => updateUrl(configuration.LEARNING_BASE_URL
const programsUrl = baseAppUrl('/dashboard/programs');

export const creditPurchaseUrl = (courseId) => `${ecommerceUrl}/credit/checkout/${courseId}/`;
export const creditRequestUrl = (providerId) => `${api}/credit/v1/providers/${providerId}/request/`;
export const creditRequestUrl = (providerId) => `${getApi()}/credit/v1/providers/${providerId}/request/`;

export default StrictDict({
api,
getApi,
baseAppUrl,
courseUnenroll,
creditPurchaseUrl,
creditRequestUrl,
entitlementEnrollment,
event,
init,
getInitApi,
learningMfeUrl,
programsUrl,
updateEmailSettings,
});

2 changes: 1 addition & 1 deletion src/data/services/lms/urls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('urls', () => {
it('builds from api url and loads providerId', () => {
const providerId = 'test-provider-id';
const url = urls.creditRequestUrl(providerId);
expect(url.startsWith(urls.api)).toEqual(true);
expect(url.startsWith(urls.getApi())).toEqual(true);
expect(url).toEqual(expect.stringContaining(providerId));
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/RecommendationsPanel/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { StrictDict } from 'utils';
import { get, stringifyUrl } from 'data/services/lms/utils';
import urls from 'data/services/lms/urls';

export const fetchUrl = `${urls.api}/learner_recommendations/courses/`;
export const getFetchUrl = ()=> (`${urls.getApi()}/learner_recommendations/courses/`);
export const apiKeys = StrictDict({ user: 'user' });

const fetchRecommendedCourses = () => get(stringifyUrl(fetchUrl));
const fetchRecommendedCourses = () => get(stringifyUrl(getFetchUrl()));

export default {
fetchRecommendedCourses,
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/RecommendationsPanel/api.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { get, stringifyUrl } from 'data/services/lms/utils';
import api, { fetchUrl } from './api';
import api, { getFetchUrl } from './api';

jest.mock('data/services/lms/utils', () => ({
stringifyUrl: (...args) => ({ stringifyUrl: args }),
Expand All @@ -10,7 +10,7 @@ describe('recommendedCourses api', () => {
describe('fetchRecommendedCourses', () => {
it('calls get with the correct recommendation courses URL and user', () => {
expect(api.fetchRecommendedCourses()).toEqual(
get(stringifyUrl(fetchUrl)),
get(stringifyUrl(getFetchUrl())),
);
});
});
Expand Down

0 comments on commit 68cd4d9

Please sign in to comment.