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);
|
|
|
|
}
|
2022-03-24 14:56:36 +01:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ActionType.setTheme,
|
|
|
|
payload: colors,
|
|
|
|
});
|
2022-03-23 14:49:35 +01:00
|
|
|
};
|
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
|
|
|
}
|
|
|
|
};
|
2022-03-24 14:56:36 +01:00
|
|
|
|
|
|
|
// export const addTheme = () => async (dispatch: Dispatch<>) => {
|
|
|
|
// try {
|
|
|
|
// // const res = await axios.post<>('/api/themes')
|
|
|
|
// } catch (err) {}
|
|
|
|
// };
|
|
|
|
|
|
|
|
// export const addQuery =
|
|
|
|
// (query: Query) => async (dispatch: Dispatch<AddQueryAction>) => {
|
|
|
|
// try {
|
|
|
|
// const res = await axios.post<ApiResponse<Query>>('/api/queries', query, {
|
|
|
|
// headers: applyAuth(),
|
|
|
|
// });
|
|
|
|
|
|
|
|
// dispatch({
|
|
|
|
// type: ActionType.addQuery,
|
|
|
|
// payload: res.data.data,
|
|
|
|
// });
|
|
|
|
// } catch (err) {
|
|
|
|
// const error = err as AxiosError<{ error: string }>;
|
|
|
|
|
|
|
|
// dispatch<any>({
|
|
|
|
// type: ActionType.createNotification,
|
|
|
|
// payload: {
|
|
|
|
// title: 'Error',
|
|
|
|
// message: error.response?.data.error,
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// };
|