Skip to content

Commit

Permalink
feat: add ability to dynamically load theme overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorrusakov committed Feb 2, 2023
1 parent de22bf4 commit 251f96d
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ FAVICON_URL=https://edx-cdn.org/v3/default/favicon.ico
IGNORED_ERROR_REGEX=
MFE_CONFIG_API_URL=
APP_ID=
SUPPORT_URL=https://support.edx.org
SUPPORT_URL=https://support.edx.org
THEME_OVERRIDE_URL=
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ IGNORED_ERROR_REGEX=
MFE_CONFIG_API_URL=
APP_ID=
SUPPORT_URL=https://support.edx.org
THEME_OVERRIDE_URL=
46 changes: 42 additions & 4 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"lodash.merge": "4.6.2",
"lodash.snakecase": "4.1.1",
"pubsub-js": "1.9.4",
"react-helmet": "^6.1.0",
"react-intl": "^5.25.0",
"universal-cookie": "4.0.4"
},
Expand Down
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ let config = {
MFE_CONFIG_API_URL: process.env.MFE_CONFIG_API_URL,
APP_ID: process.env.APP_ID,
SUPPORT_URL: process.env.SUPPORT_URL,
THEME_OVERRIDE_URL: process.env.THEME_OVERRIDE_URL,
};

/**
Expand Down Expand Up @@ -203,4 +204,5 @@ export function ensureConfig(keys, requester = 'unspecified application code') {
* @property {string} MFE_CONFIG_API_URL
* @property {string} APP_ID
* @property {string} SUPPORT_URL
* @property {string} THEME_OVERRIDE_URL
*/
26 changes: 23 additions & 3 deletions src/react/AppProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useMemo } from 'react';
import React, { useState, useMemo, useEffect } from 'react';
import PropTypes from 'prop-types';
import { Router } from 'react-router-dom';

Expand Down Expand Up @@ -48,6 +48,7 @@ export default function AppProvider({ store, children }) {
const [config, setConfig] = useState(getConfig());
const [authenticatedUser, setAuthenticatedUser] = useState(getAuthenticatedUser());
const [locale, setLocale] = useState(getLocale());
const [themeLoaded, setThemeLoaded] = useState(false);

useAppEvent(AUTHENTICATED_USER_CHANGED, () => {
setAuthenticatedUser(getAuthenticatedUser());
Expand All @@ -61,8 +62,28 @@ export default function AppProvider({ store, children }) {
setLocale(getLocale());
});

useEffect(() => {
if (config.THEME_OVERRIDE_URL) {
const themeLink = document.createElement('link');
themeLink.href = config.THEME_OVERRIDE_URL;
themeLink.rel = 'stylesheet';
themeLink.type = 'text/css';
themeLink.onload = () => setThemeLoaded(true);
themeLink.onerror = () => setThemeLoaded(true);

document.head.appendChild(themeLink);

return () => document.head.removeChild(themeLink);
}
setThemeLoaded(true);
}, [config.THEME_OVERRIDE_URL]);

const appContextValue = useMemo(() => ({ authenticatedUser, config, locale }), [authenticatedUser, config, locale]);

if (!themeLoaded) {
return null;
}

return (
<IntlProvider locale={locale} messages={getMessages()}>
<ErrorBoundary>
Expand All @@ -81,8 +102,7 @@ export default function AppProvider({ store, children }) {
}

AppProvider.propTypes = {
// eslint-disable-next-line react/forbid-prop-types
store: PropTypes.object,
store: PropTypes.shape({}),
children: PropTypes.node.isRequired,
};

Expand Down

0 comments on commit 251f96d

Please sign in to comment.