forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request freeCodeCamp#10102 from BerkeleyTrue/feature/add-r…
…edux-night-mode Feature(theme): add nightmode react logic
- Loading branch information
Showing
8 changed files
with
106 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Observable } from 'rx'; | ||
import { postJSON$ } from '../../common/utils/ajax-stream'; | ||
import types from '../../common/app/redux/types'; | ||
import { | ||
addThemeToBody, | ||
updateTheme, | ||
createErrorObservable | ||
} from '../../common/app/redux/actions'; | ||
|
||
export default function nightModeSaga( | ||
actions, | ||
getState, | ||
{ document: { body } } | ||
) { | ||
const toggleBodyClass = actions | ||
.filter(({ type }) => types.addThemeToBody === type) | ||
.doOnNext(({ payload: theme }) => { | ||
if (theme === 'night') { | ||
body.classList.add('night'); | ||
} else { | ||
body.classList.remove('night'); | ||
} | ||
}) | ||
.filter(() => false); | ||
const toggle = actions | ||
.filter(({ type }) => types.toggleNightMode === type); | ||
|
||
const optimistic = toggle | ||
.flatMap(() => { | ||
const { app: { theme } } = getState(); | ||
const newTheme = !theme || theme === 'default' ? 'night' : 'default'; | ||
return Observable.of( | ||
updateTheme(newTheme), | ||
addThemeToBody(newTheme) | ||
); | ||
}); | ||
|
||
const ajax = toggle | ||
.debounce(250) | ||
.flatMapLatest(() => { | ||
const { app: { theme, csrfToken: _csrf } } = getState(); | ||
return postJSON$('/update-my-theme', { _csrf, theme }) | ||
.catch(createErrorObservable); | ||
}); | ||
|
||
return Observable.merge(optimistic, toggleBodyClass, ajax); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters