Move theme setting to settings store

This commit is contained in:
Thomas Nordquist
2019-04-03 02:14:09 +02:00
parent 6f86a8d471
commit b9eb54dd20
5 changed files with 31 additions and 23 deletions

View File

@@ -11,6 +11,7 @@ import {
ActionTypes, ActionTypes,
SettingsState, SettingsState,
TopicOrder, TopicOrder,
Action,
} from '../reducers/Settings' } from '../reducers/Settings'
import { Base64Message } from '../../../backend/src/Model/Base64Message'; import { Base64Message } from '../../../backend/src/Model/Base64Message';
@@ -152,3 +153,10 @@ function autoExpandLimitForTree(tree: q.Tree<TopicViewModel>) {
return closestExistingLimit(calculatedLimit) return closestExistingLimit(calculatedLimit)
} }
export const toggleTheme = () => (dispatch: Dispatch<any>, getState: () => AppState) => {
dispatch({
type: getState().settings.theme === 'light' ? ActionTypes.SETTINGS_SET_THEME_DARK : ActionTypes.SETTINGS_SET_THEME_LIGHT,
})
dispatch(storeSettings())
}

View File

@@ -4,7 +4,7 @@ import ChevronRight from '@material-ui/icons/ChevronRight'
import { AppState } from '../reducers' import { AppState } from '../reducers'
import { bindActionCreators } from 'redux' import { bindActionCreators } from 'redux'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { settingsActions, globalActions } from '../actions' import { settingsActions } from '../actions'
import { shell } from 'electron' import { shell } from 'electron'
import { StyleRulesCallback, withStyles } from '@material-ui/core/styles' import { StyleRulesCallback, withStyles } from '@material-ui/core/styles'
import { TopicOrder } from '../reducers/Settings' import { TopicOrder } from '../reducers/Settings'
@@ -70,7 +70,6 @@ const styles: StyleRulesCallback = theme => ({
interface Props { interface Props {
actions: typeof settingsActions actions: typeof settingsActions
globalActions: typeof globalActions
autoExpandLimit: number autoExpandLimit: number
classes: any classes: any
highlightTopicUpdates: boolean highlightTopicUpdates: boolean
@@ -177,9 +176,9 @@ class Settings extends React.Component<Props, {}> {
} }
private toggleTheme() { private toggleTheme() {
const { globalActions, theme } = this.props const { actions, theme } = this.props
return this.renderSwitch('Theme', theme === 'light', globalActions.toggleTheme, 'Select a theme') return this.renderSwitch('Theme', theme === 'light', actions.toggleTheme, 'Select a theme')
} }
private renderAutoExpand() { private renderAutoExpand() {
@@ -242,14 +241,13 @@ const mapStateToProps = (state: AppState) => {
visible: state.settings.visible, visible: state.settings.visible,
highlightTopicUpdates: state.settings.highlightTopicUpdates, highlightTopicUpdates: state.settings.highlightTopicUpdates,
selectTopicWithMouseOver: state.settings.selectTopicWithMouseOver, selectTopicWithMouseOver: state.settings.selectTopicWithMouseOver,
theme: state.globalState.theme, theme: state.settings.theme,
} }
} }
const mapDispatchToProps = (dispatch: any) => { const mapDispatchToProps = (dispatch: any) => {
return { return {
actions: bindActionCreators(settingsActions, dispatch), actions: bindActionCreators(settingsActions, dispatch),
globalActions: bindActionCreators(globalActions, dispatch),
} }
} }

View File

@@ -56,7 +56,7 @@ function ApplicationRenderer(props: {theme: 'light' | 'dark'}) {
const mapStateToProps = (state: AppState) => { const mapStateToProps = (state: AppState) => {
return { return {
theme: state.globalState.theme, theme: state.settings.theme,
} }
} }

View File

@@ -18,9 +18,10 @@ export interface SettingsState {
highlightTopicUpdates: boolean highlightTopicUpdates: boolean
valueRendererDisplayMode: ValueRendererDisplayMode valueRendererDisplayMode: ValueRendererDisplayMode
selectTopicWithMouseOver: boolean selectTopicWithMouseOver: boolean
theme: 'light' | 'dark'
} }
export type Action = SetAutoExpandLimit | ToggleVisibility | SetTopicOrder | FilterTopics | TogglehighlightTopicUpdates | SetValueRendererDisplayMode export type Action = SetAutoExpandLimit | ToggleVisibility | SetTopicOrder | FilterTopics | TogglehighlightTopicUpdates | SetValueRendererDisplayMode | SetTheme
export enum ActionTypes { export enum ActionTypes {
SETTINGS_SET_AUTO_EXPAND_LIMIT = 'SETTINGS_SET_AUTO_EXPAND_LIMIT', SETTINGS_SET_AUTO_EXPAND_LIMIT = 'SETTINGS_SET_AUTO_EXPAND_LIMIT',
@@ -31,7 +32,8 @@ export enum ActionTypes {
SETTINGS_DID_LOAD_SETTINGS = 'SETTINGS_DID_LOAD_SETTINGS', SETTINGS_DID_LOAD_SETTINGS = 'SETTINGS_DID_LOAD_SETTINGS',
SETTINGS_SET_VALUE_RENDERER_DISPLAY_MODE = 'SETTINGS_SET_VALUE_RENDERER_DISPLAY_MODE', SETTINGS_SET_VALUE_RENDERER_DISPLAY_MODE = 'SETTINGS_SET_VALUE_RENDERER_DISPLAY_MODE',
SETTINGS_SET_SELECT_TOPIC_WITH_MOUSE_OVER = 'SETTINGS_SET_SELECT_TOPIC_WITH_MOUSE_OVER', SETTINGS_SET_SELECT_TOPIC_WITH_MOUSE_OVER = 'SETTINGS_SET_SELECT_TOPIC_WITH_MOUSE_OVER',
SETTINGS_SET_THEME_LIGHT = 'SETTINGS_SET_THEME_LIGHT',
SETTINGS_SET_THEME_DARK = 'SETTINGS_SET_THEME_DARK',
} }
const initialState: SettingsState = { const initialState: SettingsState = {
@@ -41,6 +43,14 @@ const initialState: SettingsState = {
highlightTopicUpdates: true, highlightTopicUpdates: true,
valueRendererDisplayMode: 'diff', valueRendererDisplayMode: 'diff',
selectTopicWithMouseOver: false, selectTopicWithMouseOver: false,
theme: 'dark',
}
const setTheme = (theme: 'light' | 'dark') => (state: SettingsState) => {
return {
...state,
theme,
}
} }
export const settingsReducer = createReducer(initialState, { export const settingsReducer = createReducer(initialState, {
@@ -52,8 +62,14 @@ export const settingsReducer = createReducer(initialState, {
SETTINGS_DID_LOAD_SETTINGS: didLoadSettings, SETTINGS_DID_LOAD_SETTINGS: didLoadSettings,
SETTINGS_SET_VALUE_RENDERER_DISPLAY_MODE: setValueRendererDisplayMode, SETTINGS_SET_VALUE_RENDERER_DISPLAY_MODE: setValueRendererDisplayMode,
SETTINGS_SET_SELECT_TOPIC_WITH_MOUSE_OVER: setSelectTopicWithMouseOver, SETTINGS_SET_SELECT_TOPIC_WITH_MOUSE_OVER: setSelectTopicWithMouseOver,
SETTINGS_SET_THEME_LIGHT: setTheme('light'),
SETTINGS_SET_THEME_DARK: setTheme('dark'),
}) })
export interface SetTheme {
type: ActionTypes.SETTINGS_SET_THEME_LIGHT | ActionTypes.SETTINGS_SET_THEME_DARK
}
export interface DidLoadSettings { export interface DidLoadSettings {
type: ActionTypes.SETTINGS_DID_LOAD_SETTINGS type: ActionTypes.SETTINGS_DID_LOAD_SETTINGS
settings: Partial<SettingsState> settings: Partial<SettingsState>

View File

@@ -34,12 +34,10 @@ export interface GlobalState {
showUpdateNotification?: boolean showUpdateNotification?: boolean
showUpdateDetails: boolean showUpdateDetails: boolean
error?: string error?: string
theme: 'light' | 'dark'
} }
const initialGlobalState: GlobalState = { const initialGlobalState: GlobalState = {
showUpdateDetails: false, showUpdateDetails: false,
theme: 'dark',
} }
const globalState: Reducer<GlobalState | undefined, CustomAction> = (state = initialGlobalState, action) => { const globalState: Reducer<GlobalState | undefined, CustomAction> = (state = initialGlobalState, action) => {
@@ -61,18 +59,6 @@ const globalState: Reducer<GlobalState | undefined, CustomAction> = (state = ini
error: action.error, error: action.error,
} }
case ActionTypes.setDarkTheme:
return {
...state,
theme: 'dark',
}
case ActionTypes.setLightTheme:
return {
...state,
theme: 'light',
}
case ActionTypes.showUpdateDetails: case ActionTypes.showUpdateDetails:
if (action.showUpdateDetails === undefined) { if (action.showUpdateDetails === undefined) {
return state return state