-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(darkMode): store darkMode in Redux store
- Loading branch information
Showing
12 changed files
with
78 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,7 @@ | ||
import { useTheme } from '@mui/material'; | ||
import { useEffect, useState } from 'react'; | ||
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'; | ||
|
||
import type { AppDispatch, RootState } from '../store/store'; | ||
|
||
// Use throughout your app instead of plain `useDispatch` and `useSelector` | ||
export const useAppDispatch = () => useDispatch<AppDispatch>(); | ||
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector; | ||
|
||
export const useThemeMode = () => { | ||
const [themeMode, setThemeMode] = useState('light'); | ||
const theme = useTheme(); | ||
useEffect(() => { | ||
setThemeMode(theme.palette.mode); | ||
}, [theme]); | ||
return themeMode; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
enum ThemeMode { | ||
DARK = 'dark', | ||
LIGHT = 'light' | ||
} | ||
|
||
export default ThemeMode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { combineReducers } from '@reduxjs/toolkit'; | ||
|
||
import themeReducer from './theme/themeSlice'; | ||
import trackersReducer from './trackers/trackersSlice'; | ||
|
||
const rootReducer = combineReducers({ | ||
trackers: trackersReducer | ||
trackers: trackersReducer, | ||
theme: themeReducer | ||
}); | ||
|
||
export default rootReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { RootState } from '../store'; | ||
|
||
const selectThemeMode = (state: RootState) => { | ||
return state.theme.themeMode; | ||
}; | ||
|
||
export { selectThemeMode }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { PayloadAction, createSlice } from '@reduxjs/toolkit'; | ||
|
||
import ThemeMode from '../../models/ThemeMode'; | ||
|
||
export interface ThemeState { | ||
themeMode: ThemeMode; | ||
} | ||
|
||
const initialState: ThemeState = { | ||
themeMode: ThemeMode.LIGHT | ||
}; | ||
|
||
export const themeSlice = createSlice({ | ||
name: 'theme', | ||
initialState, | ||
reducers: { | ||
setThemeMode: (state, action: PayloadAction<ThemeMode>) => { | ||
state.themeMode = action.payload; | ||
}, | ||
toggleThemeMode: (state) => { | ||
const { themeMode } = state; | ||
state.themeMode = themeMode === ThemeMode.LIGHT ? ThemeMode.DARK : ThemeMode.LIGHT; | ||
} | ||
} | ||
}); | ||
|
||
export const { setThemeMode, toggleThemeMode } = themeSlice.actions; | ||
|
||
export default themeSlice.reducer; |