Skip to content

Commit

Permalink
feat(admin-ui): user lang and theme config for each user
Browse files Browse the repository at this point in the history
  • Loading branch information
harryandriyan committed Aug 28, 2022
1 parent 03bcb48 commit d6ca354
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
30 changes: 21 additions & 9 deletions admin-ui/app/components/ThemeSetting/ThemeSettings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext } from 'react'
import React, { useContext, useEffect } from 'react'
import clsx from 'clsx'
import Drawer from '@material-ui/core/Drawer'
import Button from '@material-ui/core/Button'
Expand All @@ -11,22 +11,34 @@ import lightBlueThumbnail from 'Images/theme-thumbnail/lightBlue.jpg'
import lightGreenThumbnail from 'Images/theme-thumbnail/lightGreen.jpg'
import styles from './styles'

export function ThemeSettings() {
export function ThemeSettings({ userInfo }) {
const classes = styles()
const [open, setOpen] = React.useState(false)
const theme = useContext(ThemeContext)

const onChangeTheme = (value) => {
theme.dispatch({ type: value })
return
}
const themeContext = useContext(ThemeContext)

const themeList = [
{ value: 'darkBlack', thumbnail: darkBlackThumbnail, text: 'Dark Black' },
{ value: 'darkBlue', thumbnail: darkBlueThumbnail, text: 'Dark Blue' },
{ value: 'lightBlue', thumbnail: lightBlueThumbnail, text: 'Light Blue' },
{ value: 'lightGreen', thumbnail: lightGreenThumbnail, text: 'Light Green' },
]
const existingConfig = JSON.parse(localStorage.getItem('userConfig'))
const lang = existingConfig?.lang || {}
const theme = existingConfig?.theme || {}

const onChangeTheme = (value) => {
const { inum } = userInfo

if (inum) {
theme[inum] = value
}

const newConfig = { lang, theme }
localStorage.setItem('userConfig', JSON.stringify(newConfig))

themeContext.dispatch({ type: value })
return
}

const toggleDrawer = (open) => (event) => {
if (event.type === 'keydown' && (event.key === 'Tab' || event.key === 'Shift')) {
Expand All @@ -50,7 +62,7 @@ export function ThemeSettings() {
{themeList.map(text => (
<Box
className={clsx(classes.selectItem, {
[classes.selectedItem]: theme.state.theme === text.value
[classes.selectedItem]: themeContext.state.theme === text.value
})}
onClick={() => onChangeTheme(text.value)}
key={text.value}
Expand Down
2 changes: 2 additions & 0 deletions admin-ui/app/redux/reducers/LogoutReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ export default function logoutReducer(state = INIT_STATE, action) {
if (action.type === USER_LOGGED_OUT) {
const initTheme = localStorage.getItem('initTheme')
const initLang = localStorage.getItem('initLang')
const userConfig = localStorage.getItem('userConfig')
localStorage.clear()
localStorage.setItem('initTheme', initTheme)
localStorage.setItem('initLang', initLang)
localStorage.setItem('userConfig', userConfig)
}
return state
}
Expand Down
4 changes: 2 additions & 2 deletions admin-ui/app/routes/Apps/Gluu/GluuNavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ function GluuNavBar({ userinfo }) {
{/*<NavbarActivityFeed />*/}
<NavSearch />
<Notifications />
<LanguageMenu />
<ThemeSetting />
<LanguageMenu userInfo={userInfo} />
<ThemeSetting userInfo={userInfo} />
<UncontrolledDropdown nav inNavbar>
<DropdownToggle nav>
<Avatar.Image
Expand Down
22 changes: 17 additions & 5 deletions admin-ui/app/routes/Apps/Gluu/LanguageMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,36 @@ import {
ButtonDropdown,
} from 'Components'

const LanguageMenu = () => {
const LanguageMenu = ({ userInfo }) => {
const [isOpen, setOpen] = useState(false)
const initLang = localStorage.getItem('initLang')
const userConfig = JSON.parse(localStorage.getItem('userConfig'))
const userConfigLang = userConfig?.lang || {}
const [lang, setLang] = useState('en')
const { t, i18n } = useTranslation()
const { inum } = userInfo
const toggle = () => setOpen(!isOpen)

function changeLanguage(code) {
i18n.changeLanguage(code)
setLang(code)
localStorage.setItem('initLang', code)

let lang = { ...userConfigLang }
const theme = userConfig?.theme || {}

if (inum) {
lang = { [inum]: code }
}

const newConfig = { lang, theme }
localStorage.setItem('userConfig', JSON.stringify(newConfig))
}

useEffect(() => {
i18n.changeLanguage(initLang)
const currentLang = initLang ? initLang : 'en'
const currentLang = userConfigLang[inum] ? userConfigLang[inum] : initLang
i18n.changeLanguage(currentLang)
setLang(currentLang)
}, [initLang])
}, [initLang, userConfigLang])

return (
<ButtonDropdown isOpen={isOpen} toggle={toggle}>
Expand Down

0 comments on commit d6ca354

Please sign in to comment.