2021-11-09 12:21:36 +01:00
|
|
|
import { Dispatch } from 'redux';
|
2022-03-23 14:49:35 +01:00
|
|
|
import { FetchThemesAction, SetThemeAction } from '../actions/theme';
|
2021-11-09 12:21:36 +01:00
|
|
|
import { ActionType } from '../action-types';
|
2022-03-23 14:49:35 +01:00
|
|
|
import { Theme, ApiResponse, ThemeColors } from '../../interfaces';
|
|
|
|
import { parseThemeToPAB } from '../../utility';
|
|
|
|
import axios from 'axios';
|
2021-11-09 12:21:36 +01:00
|
|
|
|
|
|
|
export const setTheme =
|
2022-03-23 14:49:35 +01:00
|
|
|
(colors: ThemeColors, remeberTheme: boolean = true) =>
|
2021-11-19 13:24:07 +01:00
|
|
|
(dispatch: Dispatch<SetThemeAction>) => {
|
2022-03-23 14:49:35 +01:00
|
|
|
if (remeberTheme) {
|
|
|
|
localStorage.setItem('theme', parseThemeToPAB(colors));
|
|
|
|
}
|
2021-11-09 12:21:36 +01:00
|
|
|
|
2022-03-23 14:49:35 +01:00
|
|
|
for (const [key, value] of Object.entries(colors)) {
|
|
|
|
document.body.style.setProperty(`--color-${key}`, value);
|
|
|
|
}
|
|
|
|
};
|
2021-11-19 13:24:07 +01:00
|
|
|
|
2022-03-23 14:49:35 +01:00
|
|
|
export const fetchThemes =
|
|
|
|
() => async (dispatch: Dispatch<FetchThemesAction>) => {
|
|
|
|
try {
|
|
|
|
const res = await axios.get<ApiResponse<Theme[]>>('/api/themes');
|
2021-11-09 12:21:36 +01:00
|
|
|
|
|
|
|
dispatch({
|
2022-03-23 14:49:35 +01:00
|
|
|
type: ActionType.fetchThemes,
|
|
|
|
payload: res.data.data,
|
2021-11-09 12:21:36 +01:00
|
|
|
});
|
2022-03-23 14:49:35 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
2021-11-09 12:21:36 +01:00
|
|
|
}
|
|
|
|
};
|