Skip to content

Commit

Permalink
feat: load themes dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Mar 31, 2020
1 parent d223a4f commit eb621be
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 44 deletions.
38 changes: 17 additions & 21 deletions ui/src/configuration/Configuration.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React from 'react'
import { useSelector, useDispatch } from 'react-redux'
import Card from '@material-ui/core/Card'
import CardContent from '@material-ui/core/CardContent'
import Button from '@material-ui/core/Button'
import { useTranslate, Title } from 'react-admin'
import { useDispatch, useSelector } from 'react-redux'
import { Card, CardContent, MenuItem, Select } from '@material-ui/core'
import { Title, useTranslate } from 'react-admin'
import { makeStyles } from '@material-ui/core/styles'
import { changeTheme } from './actions'
import themes from '../themes'

const useStyles = makeStyles({
label: { width: '10em', display: 'inline-block' },
Expand All @@ -17,27 +16,24 @@ const Configuration = () => {
const classes = useStyles()
const theme = useSelector((state) => state.theme)
const dispatch = useDispatch()
const themeNames = Object.keys(themes).sort()

return (
<Card>
<Title title={translate('menu.configuration')} />
<CardContent>
<div className={classes.label}>{translate('menu.theme.name')}</div>
<Button
variant="contained"
className={classes.button}
color={theme === 'light' ? 'primary' : 'default'}
onClick={() => dispatch(changeTheme('light'))}
>
{translate('theme.light')}
</Button>
<Button
variant="contained"
className={classes.button}
color={theme === 'dark' ? 'primary' : 'default'}
onClick={() => dispatch(changeTheme('dark'))}
<div className={classes.label}>{translate('menu.theme')}</div>
<Select
value={theme}
variant="filled"
onChange={(event) => {
dispatch(changeTheme(event.target.value))
}}
>
{translate('theme.dark')}
</Button>
{themeNames.map((t) => (
<MenuItem value={t}>{themes[t].name}</MenuItem>
))}
</Select>
</CardContent>
</Card>
)
Expand Down
2 changes: 1 addition & 1 deletion ui/src/configuration/themeReducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CHANGE_THEME } from './actions'

export default (previousState = 'dark', { type, payload }) => {
export default (previousState = 'DarkTheme', { type, payload }) => {
if (type === CHANGE_THEME) {
return payload
}
Expand Down
8 changes: 1 addition & 7 deletions ui/src/i18n/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ export default deepmerge(englishMessages, {
settings: 'Settings',
configuration: 'Configuration',
version: 'Version %{version}',
theme: {
name: 'Theme'
}
},
theme: {
dark: 'Dark',
light: 'Light'
theme: 'Theme'
},
player: {
panelTitle: 'Play Queue',
Expand Down
22 changes: 9 additions & 13 deletions ui/src/layout/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@ import { Layout } from 'react-admin'
import { makeStyles } from '@material-ui/core/styles'
import Menu from './Menu'
import AppBar from './AppBar'
import { DarkTheme, LightTheme } from '../themes'
import themes from '../themes'

const useStyles = makeStyles({
root: { paddingBottom: '80px' }
})

export default (props) => {
const classes = useStyles()
const theme = useSelector((state) =>
state.theme === 'dark' ? DarkTheme : LightTheme
)
const theme = useSelector((state) => themes[state.theme] || themes.DarkTheme)

return (
<>
<Layout
{...props}
className={classes.root}
menu={Menu}
appBar={AppBar}
theme={theme}
/>
</>
<Layout
{...props}
className={classes.root}
menu={Menu}
appBar={AppBar}
theme={theme}
/>
)
}
2 changes: 1 addition & 1 deletion ui/src/layout/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import LockIcon from '@material-ui/icons/Lock'

import { Notification, useLogin, useNotify, useTranslate } from 'react-admin'

import { LightTheme } from '../themes'
import LightTheme from '../themes/light'

const useStyles = makeStyles((theme) => ({
main: {
Expand Down
1 change: 1 addition & 0 deletions ui/src/themes/dark.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import blue from '@material-ui/core/colors/blue'

export default {
name: 'Dark (default)',
palette: {
primary: {
main: '#90caf9'
Expand Down
2 changes: 1 addition & 1 deletion ui/src/themes/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LightTheme from './light'
import DarkTheme from './dark'

export { LightTheme, DarkTheme }
export default { LightTheme, DarkTheme }
1 change: 1 addition & 0 deletions ui/src/themes/light.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default {
name: 'Light',
palette: {
secondary: {
light: '#5f5fc4',
Expand Down

0 comments on commit eb621be

Please sign in to comment.