Skip to content

Commit d6ca354

Browse files
committed
feat(admin-ui): user lang and theme config for each user
1 parent 03bcb48 commit d6ca354

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

admin-ui/app/components/ThemeSetting/ThemeSettings.js

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useContext } from 'react'
1+
import React, { useContext, useEffect } from 'react'
22
import clsx from 'clsx'
33
import Drawer from '@material-ui/core/Drawer'
44
import Button from '@material-ui/core/Button'
@@ -11,22 +11,34 @@ import lightBlueThumbnail from 'Images/theme-thumbnail/lightBlue.jpg'
1111
import lightGreenThumbnail from 'Images/theme-thumbnail/lightGreen.jpg'
1212
import styles from './styles'
1313

14-
export function ThemeSettings() {
14+
export function ThemeSettings({ userInfo }) {
1515
const classes = styles()
1616
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)
2318

2419
const themeList = [
2520
{ value: 'darkBlack', thumbnail: darkBlackThumbnail, text: 'Dark Black' },
2621
{ value: 'darkBlue', thumbnail: darkBlueThumbnail, text: 'Dark Blue' },
2722
{ value: 'lightBlue', thumbnail: lightBlueThumbnail, text: 'Light Blue' },
2823
{ value: 'lightGreen', thumbnail: lightGreenThumbnail, text: 'Light Green' },
2924
]
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+
}
3042

3143
const toggleDrawer = (open) => (event) => {
3244
if (event.type === 'keydown' && (event.key === 'Tab' || event.key === 'Shift')) {
@@ -50,7 +62,7 @@ export function ThemeSettings() {
5062
{themeList.map(text => (
5163
<Box
5264
className={clsx(classes.selectItem, {
53-
[classes.selectedItem]: theme.state.theme === text.value
65+
[classes.selectedItem]: themeContext.state.theme === text.value
5466
})}
5567
onClick={() => onChangeTheme(text.value)}
5668
key={text.value}

admin-ui/app/redux/reducers/LogoutReducer.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ export default function logoutReducer(state = INIT_STATE, action) {
99
if (action.type === USER_LOGGED_OUT) {
1010
const initTheme = localStorage.getItem('initTheme')
1111
const initLang = localStorage.getItem('initLang')
12+
const userConfig = localStorage.getItem('userConfig')
1213
localStorage.clear()
1314
localStorage.setItem('initTheme', initTheme)
1415
localStorage.setItem('initLang', initLang)
16+
localStorage.setItem('userConfig', userConfig)
1517
}
1618
return state
1719
}

admin-ui/app/routes/Apps/Gluu/GluuNavBar.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ function GluuNavBar({ userinfo }) {
4949
{/*<NavbarActivityFeed />*/}
5050
<NavSearch />
5151
<Notifications />
52-
<LanguageMenu />
53-
<ThemeSetting />
52+
<LanguageMenu userInfo={userInfo} />
53+
<ThemeSetting userInfo={userInfo} />
5454
<UncontrolledDropdown nav inNavbar>
5555
<DropdownToggle nav>
5656
<Avatar.Image

admin-ui/app/routes/Apps/Gluu/LanguageMenu.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,36 @@ import {
77
ButtonDropdown,
88
} from 'Components'
99

10-
const LanguageMenu = () => {
10+
const LanguageMenu = ({ userInfo }) => {
1111
const [isOpen, setOpen] = useState(false)
1212
const initLang = localStorage.getItem('initLang')
13+
const userConfig = JSON.parse(localStorage.getItem('userConfig'))
14+
const userConfigLang = userConfig?.lang || {}
1315
const [lang, setLang] = useState('en')
1416
const { t, i18n } = useTranslation()
17+
const { inum } = userInfo
1518
const toggle = () => setOpen(!isOpen)
1619

1720
function changeLanguage(code) {
1821
i18n.changeLanguage(code)
1922
setLang(code)
20-
localStorage.setItem('initLang', code)
23+
24+
let lang = { ...userConfigLang }
25+
const theme = userConfig?.theme || {}
26+
27+
if (inum) {
28+
lang = { [inum]: code }
29+
}
30+
31+
const newConfig = { lang, theme }
32+
localStorage.setItem('userConfig', JSON.stringify(newConfig))
2133
}
2234

2335
useEffect(() => {
24-
i18n.changeLanguage(initLang)
25-
const currentLang = initLang ? initLang : 'en'
36+
const currentLang = userConfigLang[inum] ? userConfigLang[inum] : initLang
37+
i18n.changeLanguage(currentLang)
2638
setLang(currentLang)
27-
}, [initLang])
39+
}, [initLang, userConfigLang])
2840

2941
return (
3042
<ButtonDropdown isOpen={isOpen} toggle={toggle}>

0 commit comments

Comments
 (0)