mirror of
https://github.com/pawelmalak/flame.git
synced 2025-08-08 04:15:18 +02:00
Added redux. Moved theme loading/setting to redux actions. Added automatic theme loading based on localStorage value.
This commit is contained in:
parent
765418d3d2
commit
7199e296b8
14 changed files with 203 additions and 39 deletions
1
client/src/store/actions/actionTypes.ts
Normal file
1
client/src/store/actions/actionTypes.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export const SET_THEME = 'SET_THEME';
|
2
client/src/store/actions/index.ts
Normal file
2
client/src/store/actions/index.ts
Normal file
|
@ -0,0 +1,2 @@
|
|||
export * from './theme';
|
||||
export * from './actionTypes';
|
24
client/src/store/actions/theme.ts
Normal file
24
client/src/store/actions/theme.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import { Dispatch } from 'redux';
|
||||
import { themes } from '../../components/Themer/themes.json';
|
||||
import { Theme } from '../../interfaces/Theme';
|
||||
import { SET_THEME } from './actionTypes';
|
||||
|
||||
export const setTheme = (themeName: string) => (dispatch: Dispatch) => {
|
||||
const theme = themes.find((theme: Theme) => theme.name === themeName);
|
||||
|
||||
if (theme) {
|
||||
localStorage.setItem('theme', themeName);
|
||||
loadTheme(theme);
|
||||
|
||||
dispatch({
|
||||
type: SET_THEME,
|
||||
payload: themeName
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const loadTheme = (theme: Theme) => {
|
||||
for (const [key, value] of Object.entries(theme.colors)) {
|
||||
document.body.style.setProperty(`--color-${key}`, value);
|
||||
}
|
||||
}
|
9
client/src/store/reducers/index.ts
Normal file
9
client/src/store/reducers/index.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { combineReducers } from 'redux';
|
||||
|
||||
import themeReducer from './theme';
|
||||
|
||||
const rootReducer = combineReducers({
|
||||
theme: themeReducer
|
||||
})
|
||||
|
||||
export default rootReducer;
|
18
client/src/store/reducers/theme.ts
Normal file
18
client/src/store/reducers/theme.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import { SET_THEME } from '../actions/actionTypes';
|
||||
|
||||
const initialState = {
|
||||
theme: 'blues'
|
||||
}
|
||||
|
||||
const themeReducer = (state = initialState, action: any) => {
|
||||
switch (action.type) {
|
||||
case SET_THEME:
|
||||
return {
|
||||
theme: action.payload
|
||||
};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
export default themeReducer;
|
9
client/src/store/store.ts
Normal file
9
client/src/store/store.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { createStore, applyMiddleware } from 'redux';
|
||||
import { composeWithDevTools } from 'redux-devtools-extension';
|
||||
import thunk from 'redux-thunk';
|
||||
import rootReducer from './reducers';
|
||||
const initialState = {};
|
||||
|
||||
const store = createStore(rootReducer, initialState, composeWithDevTools(applyMiddleware(thunk)));
|
||||
|
||||
export default store;
|
Loading…
Add table
Add a link
Reference in a new issue