@@ -4,36 +4,43 @@ import Cookies from 'universal-cookie';
44import { Icon } from '@openedx/paragon' ;
55import { WbSunny , Nightlight } from '@openedx/paragon/icons' ;
66
7+
8+ let themeCookie = 'indigo-toggle-dark' ;
9+ let themeCookieExpiry = 90 ; // days
10+
711const ThemeToggleButton = ( ) => {
812 const cookies = new Cookies ( ) ;
9- const themeCookieName = getConfig ( ) . THEME_COOKIE_NAME ? getConfig ( ) . THEME_COOKIE_NAME : null ;
13+ const isThemeToggleEnabled = getConfig ( ) . INDIGO_ENABLE_DARK_TOGGLE ;
1014
11- const getNextWeek = ( ) => {
15+ const getCookieExpiry = ( ) => {
1216 const today = new Date ( ) ;
13- return new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) + 7 ) ;
17+ return new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) + themeCookieExpiry ) ;
18+ } ;
19+
20+ const getCookieOptions = ( serverURL ) => {
21+ return { domain : serverURL . hostname , path : '/' , expires : getCookieExpiry ( ) } ;
1422 } ;
1523
1624 const onToggleTheme = ( ) => {
1725 const serverURL = new URL ( getConfig ( ) . LMS_BASE_URL ) ;
18- const options = { domain : serverURL . hostname , path : '/' , expires : getNextWeek ( ) } ;
19- let themeName = '' ;
26+ let theme = '' ;
2027
21- if ( cookies . get ( themeCookieName ) === 'dark' ) {
28+ if ( cookies . get ( themeCookie ) === 'dark' ) {
2229 document . body . classList . remove ( 'indigo-dark-theme' ) ;
23- themeName = 'light' ;
30+ theme = 'light' ;
2431 } else {
2532 document . body . classList . add ( 'indigo-dark-theme' ) ;
26- themeName = 'dark' ;
33+ theme = 'dark' ;
2734 }
28- cookies . set ( themeCookieName , themeName , options ) ;
35+ cookies . set ( themeCookie , theme , getCookieOptions ( serverURL ) ) ;
2936
3037 const learningMFEUnitIframe = document . getElementById ( 'unit-iframe' ) ;
3138 if ( learningMFEUnitIframe ) {
32- learningMFEUnitIframe . contentWindow . postMessage ( { 'indigo-toggle-dark' : themeName } , serverURL . origin ) ;
39+ learningMFEUnitIframe . contentWindow . postMessage ( { 'indigo-toggle-dark' : theme } , serverURL . origin ) ;
3340 }
3441 } ;
3542
36- if ( ! themeCookieName ) {
43+ if ( ! isThemeToggleEnabled ) {
3744 return < div /> ;
3845 }
3946
@@ -42,7 +49,7 @@ const ThemeToggleButton = () => {
4249 < div className = "light-theme-icon" > < Icon src = { WbSunny } /> </ div >
4350 < div className = "toggle-switch" >
4451 < label htmlFor = "theme-toggle-checkbox" className = "switch" >
45- < input id = "theme-toggle-checkbox" defaultChecked = { cookies . get ( themeCookieName ) === 'dark' } onChange = { onToggleTheme } type = "checkbox" />
52+ < input id = "theme-toggle-checkbox" defaultChecked = { cookies . get ( themeCookie ) === 'dark' } onChange = { onToggleTheme } type = "checkbox" />
4653 < span className = "slider round" />
4754 </ label >
4855 </ div >
0 commit comments