Merge pull request #13229 from Bouncey/fix/nightModeOnPageLoad

Persist night mode on page load
pull/14480/head
Berkeley Martinez 2017-04-18 11:06:19 -07:00 committed by GitHub
commit 4942156c31
2 changed files with 21 additions and 0 deletions

View File

@ -1,4 +1,6 @@
import { Observable } from 'rx'; import { Observable } from 'rx';
import store from 'store';
import { postJSON$ } from '../../common/utils/ajax-stream'; import { postJSON$ } from '../../common/utils/ajax-stream';
import types from '../../common/app/redux/types'; import types from '../../common/app/redux/types';
import { import {
@ -7,6 +9,10 @@ import {
createErrorObservable createErrorObservable
} from '../../common/app/redux/actions'; } from '../../common/app/redux/actions';
function persistTheme(theme) {
store.set('fcc-theme', theme);
}
export default function nightModeSaga( export default function nightModeSaga(
actions, actions,
getState, getState,
@ -17,6 +23,8 @@ export default function nightModeSaga(
.doOnNext(({ payload: theme }) => { .doOnNext(({ payload: theme }) => {
if (theme === 'night') { if (theme === 'night') {
body.classList.add('night'); body.classList.add('night');
// catch existing night mode users
persistTheme(theme);
} else { } else {
body.classList.remove('night'); body.classList.remove('night');
} }
@ -29,6 +37,7 @@ export default function nightModeSaga(
.flatMap(() => { .flatMap(() => {
const { app: { theme } } = getState(); const { app: { theme } } = getState();
const newTheme = !theme || theme === 'default' ? 'night' : 'default'; const newTheme = !theme || theme === 'default' ? 'night' : 'default';
persistTheme(newTheme);
return Observable.of( return Observable.of(
updateTheme(newTheme), updateTheme(newTheme),
addThemeToBody(newTheme) addThemeToBody(newTheme)

View File

@ -11,5 +11,17 @@ html(lang='en')
script!= state script!= state
script. script.
window.webpackManifest = !{JSON.stringify(chunkManifest || {})}; window.webpackManifest = !{JSON.stringify(chunkManifest || {})};
(function setTheme() {
let fccTheme;
try {
fccTheme = JSON.parse(localStorage.getItem('fcc-theme'));
if (fccTheme && fccTheme === 'night') {
document.body.classList.add('night');
}
}
catch(e) {
fccTheme = null;
}
})();
script(src=rev('/js', 'vendor-challenges.js')) script(src=rev('/js', 'vendor-challenges.js'))
script(src=rev('/js', 'bundle.js')) script(src=rev('/js', 'bundle.js'))