2021-05-08 18:49:08 +02:00
|
|
|
import { Dispatch } from 'redux';
|
|
|
|
import { themes } from '../../components/Themer/themes.json';
|
|
|
|
import { Theme } from '../../interfaces/Theme';
|
2021-05-11 16:44:05 +02:00
|
|
|
import { ActionTypes } from './actionTypes';
|
|
|
|
|
2021-05-14 18:51:56 +02:00
|
|
|
export interface SetThemeAction {
|
2021-05-11 16:44:05 +02:00
|
|
|
type: ActionTypes.setTheme,
|
2021-05-14 18:51:56 +02:00
|
|
|
payload: Theme
|
2021-05-11 16:44:05 +02:00
|
|
|
}
|
2021-05-08 18:49:08 +02:00
|
|
|
|
|
|
|
export const setTheme = (themeName: string) => (dispatch: Dispatch) => {
|
|
|
|
const theme = themes.find((theme: Theme) => theme.name === themeName);
|
|
|
|
|
|
|
|
if (theme) {
|
|
|
|
localStorage.setItem('theme', themeName);
|
|
|
|
loadTheme(theme);
|
|
|
|
|
2021-05-14 18:51:56 +02:00
|
|
|
dispatch<SetThemeAction>({
|
2021-05-11 16:44:05 +02:00
|
|
|
type: ActionTypes.setTheme,
|
2021-05-14 18:51:56 +02:00
|
|
|
payload: theme
|
2021-05-08 18:49:08 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:51:56 +02:00
|
|
|
export const loadTheme = (theme: Theme): void => {
|
2021-05-08 18:49:08 +02:00
|
|
|
for (const [key, value] of Object.entries(theme.colors)) {
|
|
|
|
document.body.style.setProperty(`--color-${key}`, value);
|
|
|
|
}
|
|
|
|
}
|