Skip to content

Commit

Permalink
Add fetch dark mode system preference #354
Browse files Browse the repository at this point in the history
  • Loading branch information
tswinb committed Aug 18, 2020
1 parent c784381 commit 404fab2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
18 changes: 10 additions & 8 deletions src/state/actions/scigateway.actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ export const initialiseAnalytics = (): Action => ({

export const configureSite = (): ThunkResult<Promise<void>> => {
return async (dispatch, getState) => {
// load dark mode preference from local storage into store
// otherwise, fetch system preference
const darkModeLocalStorage = localStorage.getItem('darkMode');
if (darkModeLocalStorage) {
const preference = darkModeLocalStorage === 'true' ? true : false;
dispatch(loadDarkModePreference(preference));
} else {
const mq = window.matchMedia('(prefers-color-scheme: dark)');
dispatch(loadDarkModePreference(mq.matches));
}
await axios
.get(`/settings.json`)
.then((res) => {
Expand Down Expand Up @@ -213,14 +223,6 @@ export const configureSite = (): ThunkResult<Promise<void>> => {
const loadingPlugins = loadMicroFrontends.init(settings.plugins);
loadingPromises.push(loadingPlugins);

// load dark mode preference from local storage into store
// otherwise, intial state is false
const darkModeLocalStorage = localStorage.getItem('darkMode');
if (darkModeLocalStorage) {
const preference = darkModeLocalStorage === 'true' ? true : false;
dispatch(loadDarkModePreference(preference));
}

return Promise.all(loadingPromises).then(() => {
dispatch(siteLoadingUpdate(false));
});
Expand Down
2 changes: 1 addition & 1 deletion src/state/middleware/scigateway.middleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('scigateway middleware', () => {
const loadDarkModePreferenceAction = {
type: LoadDarkModePreferenceType,
payload: {
preference: false,
darkMode: false,
},
};

Expand Down
14 changes: 8 additions & 6 deletions src/state/middleware/scigateway.middleware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ export const listenToPlugins = (
dispatch: Dispatch,
getState: () => StateType
): void => {
let darkModePreference;
const darkModeLocalStorage = localStorage.getItem('darkMode');
const darkModePreference = darkModeLocalStorage === 'true' ? true : false;
if (darkModeLocalStorage) {
darkModePreference = darkModeLocalStorage === 'true' ? true : false;
} else {
const mq = window.matchMedia('(prefers-color-scheme: dark)');
darkModePreference = mq.matches;
}
const theme = buildTheme(darkModePreference);

document.addEventListener(microFrontendMessageId, (event) => {
Expand Down Expand Up @@ -152,11 +158,7 @@ const ScigatewayMiddleware: Middleware = ((

if (action.type === LoadDarkModePreferenceType) {
next(action);

const darkModeLocalStorage = localStorage.getItem('darkMode');
const darkModePreference = darkModeLocalStorage === 'true' ? true : false;
const theme = buildTheme(darkModePreference);

const theme = buildTheme(action.payload.darkMode);
store.dispatch(sendThemeOptions(theme));
return store.dispatch(requestPluginRerender());
}
Expand Down

0 comments on commit 404fab2

Please sign in to comment.