Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BD-46] feat: add ability to dynamically load theme overrides #435

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=
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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: correctly handle dynamic THEME_OVERRIDE_URL change (e.g., don't create new link). Although I don't think it can change during runtime, should still consider such case.

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) {
Copy link
Contributor Author

@viktorrusakov viktorrusakov Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't show anything until theme override loaded to avoid showing content with default theme, before the overrides have been applied

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