From 102662748135f5111b0a346fa00776c1c4548d84 Mon Sep 17 00:00:00 2001 From: dubisdev Date: Sat, 6 Jan 2024 20:20:11 +0100 Subject: [PATCH] refactor(store): move update store from redux to redux-toolkit --- app/renderer/src/components/Updater.tsx | 8 ++--- app/renderer/src/store/config/index.ts | 32 ++++++++--------- app/renderer/src/store/settings/index.ts | 44 ++++++++++-------------- app/renderer/src/store/store.ts | 2 +- app/renderer/src/store/update/actions.ts | 24 ------------- app/renderer/src/store/update/index.ts | 43 +++++++++++++++++++++-- app/renderer/src/store/update/reducer.ts | 38 -------------------- app/renderer/src/store/update/types.ts | 13 ------- 8 files changed, 79 insertions(+), 125 deletions(-) delete mode 100644 app/renderer/src/store/update/actions.ts delete mode 100644 app/renderer/src/store/update/reducer.ts delete mode 100644 app/renderer/src/store/update/types.ts diff --git a/app/renderer/src/components/Updater.tsx b/app/renderer/src/components/Updater.tsx index 8a162db6..c01b3ee2 100644 --- a/app/renderer/src/components/Updater.tsx +++ b/app/renderer/src/components/Updater.tsx @@ -2,7 +2,7 @@ import React from "react"; import Header from "./Header"; import styled from "styled-components/macro"; import ReactMarkdown from "react-markdown"; -import { useDispatch, useSelector } from "react-redux"; +import { useAppDispatch, useAppSelector } from "hooks/storeHooks"; import { AppStateTypes, setIgnoreUpdate, SettingTypes } from "../store"; import { setUpdateBody, @@ -51,14 +51,14 @@ const IgnoreVersion = styled.div` `; const Updater: React.FC = () => { - const settings: SettingTypes = useSelector( + const settings: SettingTypes = useAppSelector( (state: AppStateTypes) => state.settings ); - const update: UpdateTypes = useSelector( + const update: UpdateTypes = useAppSelector( (state: AppStateTypes) => state.update ); - const dispatch = useDispatch(); + const dispatch = useAppDispatch(); return ( diff --git a/app/renderer/src/store/config/index.ts b/app/renderer/src/store/config/index.ts index 50451792..6ba1a3e3 100644 --- a/app/renderer/src/store/config/index.ts +++ b/app/renderer/src/store/config/index.ts @@ -3,6 +3,10 @@ import { getFromStorage } from "utils"; import { ConfigTypes } from "./types"; import { defaultConfig } from "./defaultConfig"; +type ConfigPayload = PayloadAction< + ConfigTypes[T] +>; + const config = (getFromStorage("state") && getFromStorage("state").config) || defaultConfig; @@ -13,51 +17,47 @@ const configSlice = createSlice({ name: "config", initialState, reducers: { - setStayFocus( - state, - action: PayloadAction - ) { + setStayFocus(state, action: ConfigPayload<"stayFocus">) { state.stayFocus = action.payload; }, - setShorBreak( - state, - action: PayloadAction - ) { + + setShorBreak(state, action: ConfigPayload<"shortBreak">) { state.shortBreak = action.payload; }, - setLongBreak( - state, - action: PayloadAction - ) { + + setLongBreak(state, action: ConfigPayload<"longBreak">) { state.longBreak = action.payload; }, - setSessionRounds( - state, - action: PayloadAction - ) { + + setSessionRounds(state, action: ConfigPayload<"sessionRounds">) { state.sessionRounds = action.payload; }, + restoreDefaultConfig() { return defaultConfig; }, + setFirstSpecialBreak( state, action: PayloadAction ) { state.specialBreaks.firstBreak = action.payload; }, + setSecondSpecialBreak( state, action: PayloadAction ) { state.specialBreaks.secondBreak = action.payload; }, + setThirdSpecialBreak( state, action: PayloadAction ) { state.specialBreaks.thirdBreak = action.payload; }, + setFourthSpecialBreak( state, action: PayloadAction diff --git a/app/renderer/src/store/settings/index.ts b/app/renderer/src/store/settings/index.ts index 2ae78e93..4d67fa9d 100644 --- a/app/renderer/src/store/settings/index.ts +++ b/app/renderer/src/store/settings/index.ts @@ -5,6 +5,10 @@ import { defaultSettings } from "./defaultSettings"; export type { SettingTypes }; +type SettingsPayload = PayloadAction< + SettingTypes[T] +>; + const settings = (getFromStorage("state") && getFromStorage("state").settings) || defaultSettings; @@ -15,17 +19,11 @@ const settingsSlice = createSlice({ name: "settings", initialState, reducers: { - setIgnoreUpdate( - state, - action: PayloadAction - ) { + setIgnoreUpdate(state, action: SettingsPayload<"ignoreUpdate">) { state.ignoreUpdate = action.payload; }, - setAlwaysOnTop( - state, - action: PayloadAction - ) { + setAlwaysOnTop(state, action: SettingsPayload<"alwaysOnTop">) { state.alwaysOnTop = action.payload; }, @@ -35,85 +33,79 @@ const settingsSlice = createSlice({ setEnableDarkTheme( state, - action: PayloadAction + action: SettingsPayload<"enableDarkTheme"> ) { state.enableDarkTheme = action.payload; }, setEnableCompactMode( state, - action: PayloadAction + action: SettingsPayload<"compactMode"> ) { state.compactMode = action.payload; }, setEnableFullscreenBreak( state, - action: PayloadAction + action: SettingsPayload<"enableFullscreenBreak"> ) { state.enableFullscreenBreak = action.payload; }, setEnableStrictMode( state, - action: PayloadAction + action: SettingsPayload<"enableStrictMode"> ) { state.enableStrictMode = action.payload; }, setEnableProgressAnimation( state, - action: PayloadAction + action: SettingsPayload<"enableProgressAnimation"> ) { state.enableProgressAnimation = action.payload; }, setEnableVoiceAssistance( state, - action: PayloadAction + action: SettingsPayload<"enableVoiceAssistance"> ) { state.enableVoiceAssistance = action.payload; }, setUseNativeTitlebar( state, - action: PayloadAction + action: SettingsPayload<"useNativeTitlebar"> ) { state.useNativeTitlebar = action.payload; }, setNotificationType( state, - action: PayloadAction + action: SettingsPayload<"notificationType"> ) { state.notificationType = action.payload; }, - setCloseToTray( - state, - action: PayloadAction - ) { + setCloseToTray(state, action: SettingsPayload<"closeToTray">) { state.closeToTray = action.payload; }, setMinimizeToTray( state, - action: PayloadAction + action: SettingsPayload<"minimizeToTray"> ) { state.minimizeToTray = action.payload; }, setAutoStartWorkTime( state, - action: PayloadAction + action: SettingsPayload<"autoStartWorkTime"> ) { state.autoStartWorkTime = action.payload; }, - setOpenAtLogin( - state, - action: PayloadAction - ) { + setOpenAtLogin(state, action: SettingsPayload<"openAtLogin">) { state.openAtLogin = action.payload; }, diff --git a/app/renderer/src/store/store.ts b/app/renderer/src/store/store.ts index 1497c2d9..021fc68c 100644 --- a/app/renderer/src/store/store.ts +++ b/app/renderer/src/store/store.ts @@ -6,7 +6,7 @@ import configReducer from "./config"; import settingReducer from "./settings"; import { timerReducer } from "./timer"; import { undoableTasksReducer } from "./tasks"; -import { updateReducer } from "./update"; +import updateReducer from "./update"; export type AppStateTypes = ReturnType; export type AppDispatchTypes = typeof store.dispatch; diff --git a/app/renderer/src/store/update/actions.ts b/app/renderer/src/store/update/actions.ts deleted file mode 100644 index 0eaeb36c..00000000 --- a/app/renderer/src/store/update/actions.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { - UpdateTypes, - UpdateActionTypes, - UPDATE_BODY, - UPDATE_VERSION, -} from "./types"; - -export const setUpdateBody = ( - updateBody: UpdateTypes["updateBody"] -): UpdateActionTypes => { - return { - type: UPDATE_BODY, - payload: updateBody, - }; -}; - -export const setUpdateVersion = ( - updateVersion: UpdateTypes["updateVersion"] -): UpdateActionTypes => { - return { - type: UPDATE_VERSION, - payload: updateVersion, - }; -}; diff --git a/app/renderer/src/store/update/index.ts b/app/renderer/src/store/update/index.ts index 6d30c416..18aae3ff 100644 --- a/app/renderer/src/store/update/index.ts +++ b/app/renderer/src/store/update/index.ts @@ -1,3 +1,40 @@ -export * from "./actions"; -export * from "./reducer"; -export * from "./types"; +import { createSlice, PayloadAction } from "@reduxjs/toolkit"; +import { getFromStorage } from "utils"; + +export type UpdateTypes = { + updateVersion: string; + updateBody: string | undefined; +}; + +type UploadPayload = PayloadAction< + UpdateTypes[T] +>; + +const defaultUpdateStatus: Readonly = Object.freeze({ + updateBody: undefined, + updateVersion: "", +}); + +const updateStatus = + (getFromStorage("state") && getFromStorage("state").update) || + defaultUpdateStatus; + +const initialState: UpdateTypes = updateStatus; + +const updateSlice = createSlice({ + name: "update", + initialState, + reducers: { + setUpdateBody(state, action: UploadPayload<"updateBody">) { + state.updateBody = action.payload; + }, + + setUpdateVersion(state, action: UploadPayload<"updateVersion">) { + state.updateVersion = action.payload; + }, + }, +}); + +export const { setUpdateBody, setUpdateVersion } = updateSlice.actions; + +export default updateSlice.reducer; diff --git a/app/renderer/src/store/update/reducer.ts b/app/renderer/src/store/update/reducer.ts deleted file mode 100644 index b762586e..00000000 --- a/app/renderer/src/store/update/reducer.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { getFromStorage, isPreferredDark, detectOS } from "utils"; -import { - UpdateTypes, - UpdateActionTypes, - UPDATE_BODY, - UPDATE_VERSION, -} from "./types"; - -const defaultSettings: UpdateTypes = { - updateBody: undefined, - updateVersion: "", -}; - -const settings = - (getFromStorage("state") && getFromStorage("state").settings) || - defaultSettings; - -const initialState: UpdateTypes = settings; - -export const updateReducer = ( - state = initialState, - action: UpdateActionTypes -) => { - switch (action.type) { - case UPDATE_BODY: - return { - ...state, - updateBody: action.payload, - }; - case UPDATE_VERSION: - return { - ...state, - updateVersion: action.payload, - }; - default: - return state; - } -}; diff --git a/app/renderer/src/store/update/types.ts b/app/renderer/src/store/update/types.ts deleted file mode 100644 index 214c0232..00000000 --- a/app/renderer/src/store/update/types.ts +++ /dev/null @@ -1,13 +0,0 @@ -const update = "[update]"; - -export type UpdateTypes = { - updateVersion: string; - updateBody: string | undefined; -}; -export const UPDATE_BODY = `${update} UPDATE_BODY`; -export const UPDATE_VERSION = `${update} UPDATE_VERSION`; - -export type UpdateActionTypes = { - type: string; - payload: any; -};