From 378dd8e36df8e753682d647938c3eacce326b0e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Malak?= Date: Thu, 24 Mar 2022 14:56:36 +0100 Subject: [PATCH] Fixed color of weather icon when changing theme --- .../UI/Icons/WeatherIcon/WeatherIcon.tsx | 6 ++-- client/src/store/action-creators/config.ts | 12 +++++-- client/src/store/action-creators/theme.ts | 35 +++++++++++++++++++ client/src/store/actions/theme.ts | 3 +- client/src/store/reducers/theme.ts | 28 +++++++++++++-- 5 files changed, 76 insertions(+), 8 deletions(-) diff --git a/client/src/components/UI/Icons/WeatherIcon/WeatherIcon.tsx b/client/src/components/UI/Icons/WeatherIcon/WeatherIcon.tsx index 2664b47..d28aff4 100644 --- a/client/src/components/UI/Icons/WeatherIcon/WeatherIcon.tsx +++ b/client/src/components/UI/Icons/WeatherIcon/WeatherIcon.tsx @@ -10,7 +10,7 @@ interface Props { } export const WeatherIcon = (props: Props): JSX.Element => { - const { theme } = useSelector((state: State) => state.theme); + const { activeTheme } = useSelector((state: State) => state.theme); const icon = props.isDay ? new IconMapping().mapIcon(props.weatherStatusCode, TimeOfDay.day) @@ -18,7 +18,7 @@ export const WeatherIcon = (props: Props): JSX.Element => { useEffect(() => { const delay = setTimeout(() => { - const skycons = new Skycons({ color: theme.colors.accent }); + const skycons = new Skycons({ color: activeTheme.colors.accent }); skycons.add(`weather-icon`, icon); skycons.play(); }, 1); @@ -26,7 +26,7 @@ export const WeatherIcon = (props: Props): JSX.Element => { return () => { clearTimeout(delay); }; - }, [props.weatherStatusCode, icon, theme.colors.accent]); + }, [props.weatherStatusCode, icon, activeTheme.colors.accent]); return ; }; diff --git a/client/src/store/action-creators/config.ts b/client/src/store/action-creators/config.ts index 6b516f7..d019876 100644 --- a/client/src/store/action-creators/config.ts +++ b/client/src/store/action-creators/config.ts @@ -7,7 +7,7 @@ import { UpdateConfigAction, UpdateQueryAction, } from '../actions/config'; -import axios from 'axios'; +import axios, { AxiosError } from 'axios'; import { ApiResponse, Config, Query } from '../../interfaces'; import { ActionType } from '../action-types'; import { storeUIConfig, applyAuth } from '../../utility'; @@ -103,7 +103,15 @@ export const addQuery = payload: res.data.data, }); } catch (err) { - console.log(err); + const error = err as AxiosError<{ error: string }>; + + dispatch({ + type: ActionType.createNotification, + payload: { + title: 'Error', + message: error.response?.data.error, + }, + }); } }; diff --git a/client/src/store/action-creators/theme.ts b/client/src/store/action-creators/theme.ts index 87bf184..2870b6c 100644 --- a/client/src/store/action-creators/theme.ts +++ b/client/src/store/action-creators/theme.ts @@ -15,6 +15,11 @@ export const setTheme = for (const [key, value] of Object.entries(colors)) { document.body.style.setProperty(`--color-${key}`, value); } + + dispatch({ + type: ActionType.setTheme, + payload: colors, + }); }; export const fetchThemes = @@ -30,3 +35,33 @@ export const fetchThemes = console.log(err); } }; + +// export const addTheme = () => async (dispatch: Dispatch<>) => { +// try { +// // const res = await axios.post<>('/api/themes') +// } catch (err) {} +// }; + +// export const addQuery = +// (query: Query) => async (dispatch: Dispatch) => { +// try { +// const res = await axios.post>('/api/queries', query, { +// headers: applyAuth(), +// }); + +// dispatch({ +// type: ActionType.addQuery, +// payload: res.data.data, +// }); +// } catch (err) { +// const error = err as AxiosError<{ error: string }>; + +// dispatch({ +// type: ActionType.createNotification, +// payload: { +// title: 'Error', +// message: error.response?.data.error, +// }, +// }); +// } +// }; diff --git a/client/src/store/actions/theme.ts b/client/src/store/actions/theme.ts index 6d40d21..b3d3e9d 100644 --- a/client/src/store/actions/theme.ts +++ b/client/src/store/actions/theme.ts @@ -1,8 +1,9 @@ import { ActionType } from '../action-types'; -import { Theme } from '../../interfaces'; +import { Theme, ThemeColors } from '../../interfaces'; export interface SetThemeAction { type: ActionType.setTheme; + payload: ThemeColors; } export interface FetchThemesAction { diff --git a/client/src/store/reducers/theme.ts b/client/src/store/reducers/theme.ts index 7738a3b..62ccf6b 100644 --- a/client/src/store/reducers/theme.ts +++ b/client/src/store/reducers/theme.ts @@ -1,14 +1,28 @@ import { Action } from '../actions'; import { ActionType } from '../action-types'; -import { Theme } from '../../interfaces/Theme'; -import { arrayPartition } from '../../utility'; +import { Theme, ThemeColors } from '../../interfaces/Theme'; +import { arrayPartition, parsePABToTheme } from '../../utility'; interface ThemeState { + activeTheme: Theme; themes: Theme[]; userThemes: Theme[]; } +const savedTheme: ThemeColors = parsePABToTheme(localStorage.theme) || { + primary: '#effbff', + accent: '#6ee2ff', + background: '#242b33', +}; + const initialState: ThemeState = { + activeTheme: { + name: 'main', + isCustom: false, + colors: { + ...savedTheme, + }, + }, themes: [], userThemes: [], }; @@ -18,6 +32,16 @@ export const themeReducer = ( action: Action ): ThemeState => { switch (action.type) { + case ActionType.setTheme: { + return { + ...state, + activeTheme: { + ...state.activeTheme, + colors: action.payload, + }, + }; + } + case ActionType.fetchThemes: { const [themes, userThemes] = arrayPartition( action.payload,