Skip to content

Commit

Permalink
fix: only fetch user's preferred enterprise metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz authored and iloveagent57 committed Sep 16, 2020
1 parent c02b125 commit 87b3e5e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
23 changes: 21 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,8 @@
"peerDependencies": {
"react": "^16.13.1",
"@edx/frontend-platform": "^1.5.2"
},
"dependencies": {
"query-string": "^6.13.2"
}
}
6 changes: 2 additions & 4 deletions src/hooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';

import getLearnerPortalLinks from './learnerPortalLinks';
import { getSelectedEnterpriseUUID } from './utils';

export default function useEnterpriseConfig(authenticatedUser, learnerPortalHostname, lmsBaseUrl) {
const [enterpriseLearnerPortalLink, setEnterpriseLearnerPortalLink] = useState();
Expand All @@ -16,9 +16,7 @@ export default function useEnterpriseConfig(authenticatedUser, learnerPortalHost
learnerPortalHostname,
lmsBaseUrl,
).then((learnerPortalLinks) => {
const preferredUUID = getSelectedEnterpriseUUID(authenticatedUser);
const preferredLearnerPortalLink = learnerPortalLinks.find(learnerPortalLink =>
learnerPortalLink.uuid === preferredUUID);
const preferredLearnerPortalLink = learnerPortalLinks.pop();
if (preferredLearnerPortalLink) {
const config = {
logoAltText: preferredLearnerPortalLink.title,
Expand Down
9 changes: 6 additions & 3 deletions src/learnerPortalLinks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fetchEnterpriseCustomers } from './service';
import { isEnterpriseUser } from './utils';
import { isEnterpriseUser, getSelectedEnterpriseUUID } from './utils';

function getCacheKey(userId) {
return `learnerPortalLinks:${userId}`;
Expand All @@ -19,14 +19,15 @@ function cacheLinks(userId, links) {
async function fetchLearnerPortalLinks(
apiClient,
userId,
preferredEnterpriseUUID,
learnerPortalHostname = process.env.ENTERPRISE_LEARNER_PORTAL_HOSTNAME,
lmsBaseUrl = process.env.LMS_BASE_URL,
) {
const learnerPortalLinks = [];
if (!learnerPortalHostname) {
return learnerPortalLinks;
}
const response = await fetchEnterpriseCustomers(apiClient, lmsBaseUrl);
const response = await fetchEnterpriseCustomers(apiClient, lmsBaseUrl, preferredEnterpriseUUID);
const enterpriseCustomers = response.data.results;
try {
for (let i = 0; i < enterpriseCustomers.length; i += 1) {
Expand Down Expand Up @@ -88,12 +89,14 @@ export default async function getLearnerPortalLinks(
const { userId } = authenticatedUser;
const cachedLinks = getCachedLearnerPortalLinks(userId);

if (cachedLinks != null) {
if (cachedLinks) {
learnerPortalLinks = learnerPortalLinks.concat(cachedLinks);
} else {
const preferredEnterpriseUUID = getSelectedEnterpriseUUID(authenticatedUser);
const links = await fetchLearnerPortalLinks(
apiClient,
userId,
preferredEnterpriseUUID,
learnerPortalHostname,
lmsBaseUrl,
);
Expand Down
17 changes: 16 additions & 1 deletion src/service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
const fetchEnterpriseCustomers = (apiClient, lmsBaseUrl = process.env.LMS_BASE_URL) => apiClient.get(`${lmsBaseUrl}/enterprise/api/v1/enterprise-customer/`);
import qs from 'querystring';

const fetchEnterpriseCustomers = (
apiClient,
lmsBaseUrl = process.env.LMS_BASE_URL,
preferredEnterpriseUUID = null,
) => {
let url = `${lmsBaseUrl}/enterprise/api/v1/enterprise-customer/`;
if (preferredEnterpriseUUID !== null) {
const queryParams = qs.stringify({
uuid: preferredEnterpriseUUID,
});
url += `?${queryParams}`;
}
return apiClient.get(url);
};

// eslint-disable-next-line import/prefer-default-export
export { fetchEnterpriseCustomers };

0 comments on commit 87b3e5e

Please sign in to comment.