2021-11-09 12:21:36 +01:00
|
|
|
import { Dispatch } from 'redux';
|
|
|
|
import { SetThemeAction } from '../actions/theme';
|
|
|
|
import { ActionType } from '../action-types';
|
|
|
|
import { Theme } from '../../interfaces/Theme';
|
2021-11-18 13:47:27 +01:00
|
|
|
import { themes } from '../../components/Settings/Themer/themes.json';
|
2021-11-09 12:21:36 +01:00
|
|
|
|
|
|
|
export const setTheme =
|
2021-11-19 13:24:07 +01:00
|
|
|
(name: string, remeberTheme: boolean = true) =>
|
|
|
|
(dispatch: Dispatch<SetThemeAction>) => {
|
2021-11-09 12:21:36 +01:00
|
|
|
const theme = themes.find((theme) => theme.name === name);
|
|
|
|
|
|
|
|
if (theme) {
|
2021-11-19 13:24:07 +01:00
|
|
|
if (remeberTheme) {
|
|
|
|
localStorage.setItem('theme', name);
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:21:36 +01:00
|
|
|
loadTheme(theme);
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ActionType.setTheme,
|
|
|
|
payload: theme,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const loadTheme = (theme: Theme): void => {
|
|
|
|
for (const [key, value] of Object.entries(theme.colors)) {
|
|
|
|
document.body.style.setProperty(`--color-${key}`, value);
|
|
|
|
}
|
|
|
|
};
|