1
- import React , { useContext } from 'react'
1
+ import React , { useContext , useEffect } from 'react'
2
2
import clsx from 'clsx'
3
3
import Drawer from '@material-ui/core/Drawer'
4
4
import Button from '@material-ui/core/Button'
@@ -11,22 +11,34 @@ import lightBlueThumbnail from 'Images/theme-thumbnail/lightBlue.jpg'
11
11
import lightGreenThumbnail from 'Images/theme-thumbnail/lightGreen.jpg'
12
12
import styles from './styles'
13
13
14
- export function ThemeSettings ( ) {
14
+ export function ThemeSettings ( { userInfo } ) {
15
15
const classes = styles ( )
16
16
const [ open , setOpen ] = React . useState ( false )
17
- const theme = useContext ( ThemeContext )
18
-
19
- const onChangeTheme = ( value ) => {
20
- theme . dispatch ( { type : value } )
21
- return
22
- }
17
+ const themeContext = useContext ( ThemeContext )
23
18
24
19
const themeList = [
25
20
{ value : 'darkBlack' , thumbnail : darkBlackThumbnail , text : 'Dark Black' } ,
26
21
{ value : 'darkBlue' , thumbnail : darkBlueThumbnail , text : 'Dark Blue' } ,
27
22
{ value : 'lightBlue' , thumbnail : lightBlueThumbnail , text : 'Light Blue' } ,
28
23
{ value : 'lightGreen' , thumbnail : lightGreenThumbnail , text : 'Light Green' } ,
29
24
]
25
+ const existingConfig = JSON . parse ( localStorage . getItem ( 'userConfig' ) )
26
+ const lang = existingConfig ?. lang || { }
27
+ const theme = existingConfig ?. theme || { }
28
+
29
+ const onChangeTheme = ( value ) => {
30
+ const { inum } = userInfo
31
+
32
+ if ( inum ) {
33
+ theme [ inum ] = value
34
+ }
35
+
36
+ const newConfig = { lang, theme }
37
+ localStorage . setItem ( 'userConfig' , JSON . stringify ( newConfig ) )
38
+
39
+ themeContext . dispatch ( { type : value } )
40
+ return
41
+ }
30
42
31
43
const toggleDrawer = ( open ) => ( event ) => {
32
44
if ( event . type === 'keydown' && ( event . key === 'Tab' || event . key === 'Shift' ) ) {
@@ -50,7 +62,7 @@ export function ThemeSettings() {
50
62
{ themeList . map ( text => (
51
63
< Box
52
64
className = { clsx ( classes . selectItem , {
53
- [ classes . selectedItem ] : theme . state . theme === text . value
65
+ [ classes . selectedItem ] : themeContext . state . theme === text . value
54
66
} ) }
55
67
onClick = { ( ) => onChangeTheme ( text . value ) }
56
68
key = { text . value }
0 commit comments