From 4039b79e7d9484d7cb77f84bb9b155afdf9a82ca Mon Sep 17 00:00:00 2001 From: Brandon Baker Date: Fri, 13 Mar 2020 09:14:03 -0400 Subject: [PATCH] feat: use frontend-enterprise for ent. links (#80) switch to using the new version of frontend-enterprise which is compatible with both frontend-platform and frontend-auth. Also removes the code that was duplicated from a prior version of frontend-enterprise. ENT-2648 --- package-lock.json | 6 ++ package.json | 2 + src/Header.jsx | 6 +- src/enterprise/getLearnerPortalLinks.js | 82 ------------------------- 4 files changed, 12 insertions(+), 84 deletions(-) delete mode 100644 src/enterprise/getLearnerPortalLinks.js diff --git a/package-lock.json b/package-lock.json index b3f9a1bc..9b091706 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1313,6 +1313,12 @@ } } }, + "@edx/frontend-enterprise": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@edx/frontend-enterprise/-/frontend-enterprise-4.0.0.tgz", + "integrity": "sha512-2FxQsnWf4Aw3As0Xh4ARXcDBPvAfppmMI8KnDHywLgQLZ6at1ceHE4rkOrHOT9QXsMoTvmY+uItAwpPnyXcSOw==", + "dev": true + }, "@edx/frontend-platform": { "version": "1.1.14", "resolved": "https://registry.npmjs.org/@edx/frontend-platform/-/frontend-platform-1.1.14.tgz", diff --git a/package.json b/package.json index d9fbd858..8eccc71c 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "@commitlint/prompt": "8.2.0", "@commitlint/prompt-cli": "8.2.0", "@edx/frontend-build": "^2.0.4", + "@edx/frontend-enterprise": "^4.0.0", "@edx/frontend-platform": "1.1.14", "@edx/paragon": "7.1.5", "codecov": "3.6.5", @@ -61,6 +62,7 @@ "react-transition-group": "4.3.0" }, "peerDependencies": { + "@edx/frontend-enterprise": "^4.0.0", "@edx/frontend-platform": "^1.1.4", "@edx/paragon": "^7.0.0", "prop-types": "^15.5.10", diff --git a/src/Header.jsx b/src/Header.jsx index 8e19428f..147613de 100644 --- a/src/Header.jsx +++ b/src/Header.jsx @@ -1,7 +1,9 @@ import React, { useContext, useEffect, useState } from 'react'; import Responsive from 'react-responsive'; +import { getLearnerPortalLinks } from '@edx/frontend-enterprise'; import { injectIntl, intlShape } from '@edx/frontend-platform/i18n'; import { sendTrackEvent } from '@edx/frontend-platform/analytics'; +import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; import { AppContext } from '@edx/frontend-platform/react'; import { APP_CONFIG_INITIALIZED, @@ -13,7 +15,6 @@ import { import DesktopHeader from './DesktopHeader'; import MobileHeader from './MobileHeader'; -import getLearnerPortalLinks from './enterprise/getLearnerPortalLinks'; import LogoSVG from './logo.svg'; @@ -37,7 +38,8 @@ function Header({ intl }) { const { authenticatedUser, config } = useContext(AppContext); const [enterpriseLearnerPortalLinks, setEnterpriseLearnerPortalLinks] = useState([]); useEffect(() => { - getLearnerPortalLinks().then((learnerPortalLinks) => { + const httpClient = getAuthenticatedHttpClient(); + getLearnerPortalLinks(httpClient, authenticatedUser).then((learnerPortalLinks) => { const links = learnerPortalLinks.map(({ url, title }) => ({ type: 'item', href: url, diff --git a/src/enterprise/getLearnerPortalLinks.js b/src/enterprise/getLearnerPortalLinks.js deleted file mode 100644 index 8d84fdaa..00000000 --- a/src/enterprise/getLearnerPortalLinks.js +++ /dev/null @@ -1,82 +0,0 @@ -import { getAuthenticatedUser, getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; -import { getConfig } from '@edx/frontend-platform/config'; - -const isEnterpriseLearner = (user = {}) => { - const { roles = [] } = user; - return roles.some(role => role.includes('enterprise_learner')); -}; - -const getCacheKey = userId => `learnerPortalLinks:${userId}`; - -const cacheLinks = (userId, links) => { - // Set one hour expiration - const expiration = Date.now() + (1 * 60 * 60 * 1000); - const cacheKey = getCacheKey(userId); - const cacheValue = JSON.stringify({ expiration, links }); - sessionStorage.setItem(cacheKey, cacheValue); -}; - -const fetchLearnerPortalLinks = async (userId) => { - const httpClient = getAuthenticatedHttpClient(); - const enterpriseApiUrl = `${getConfig().LMS_BASE_URL}/enterprise/api/v1/enterprise-customer/`; - const enterpriseLearnerPortalHostname = process.env.ENTERPRISE_LEARNER_PORTAL_HOSTNAME; - if (!enterpriseLearnerPortalHostname) { - return []; - } - - try { - const { data } = await httpClient.get(enterpriseApiUrl); - const enterpriseCustomers = data.results.map(customer => ({ - name: customer.name, - isEnabled: customer.enable_learner_portal, - slug: customer.slug, - })); - - const links = enterpriseCustomers - .filter(({ isEnabled, slug }) => isEnabled && slug) - .map(({ name, slug }) => ({ - title: name, - url: `https://${enterpriseLearnerPortalHostname}/${slug}`, - })); - - cacheLinks(userId, links); - - return links; - } catch (e) { - return []; - } -}; - -const getCachedLearnerPortalLinks = (userId) => { - const cacheKey = getCacheKey(userId); - const cachedValue = sessionStorage.getItem(cacheKey); - - if (cachedValue) { - const cachedLinks = JSON.parse(cachedValue); - // Check cache expiration - if (cachedLinks.expiration <= Date.now()) { - sessionStorage.removeItem(cacheKey); - } else { - return cachedLinks.links; - } - } - - return null; -}; - -export default async function getLearnerPortalLinks() { - const authenticatedUser = await getAuthenticatedUser(); - - if (isEnterpriseLearner(authenticatedUser)) { - const { userId } = authenticatedUser; - const cachedLinks = getCachedLearnerPortalLinks(userId); - - if (cachedLinks != null) { - return cachedLinks; - } - - return fetchLearnerPortalLinks(userId); - } - - return []; -}