Skip to content

Commit

Permalink
Store setting in localStorage to retain across refresh (#42)
Browse files Browse the repository at this point in the history
* Store setting in localStorage to retain across refresh

Set the value of the darkMode setting in localStorage using hooks when the
value changes. Also, added functionality to read the stored value when the
settings load when the app loads
  • Loading branch information
burtonr authored Oct 13, 2023
1 parent 41aaa95 commit 07cdb5d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const App = () => {
})
}

// TODO: Store mode in local storage per user
const theme = useMemo(() => createTheme(getTheme()), [isDarkMode]);

return (
Expand Down
8 changes: 8 additions & 0 deletions src/features/app/appSlice.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createSlice } from "@reduxjs/toolkit"
import { apiSlice } from "../api/apiSlice"

const storageNames = {
darkMode: 'darkMode'
}

const appSlice = createSlice({
name: 'app',
initialState: {
Expand All @@ -17,8 +21,12 @@ const appSlice = createSlice({
}
},
extraReducers(builder) {
builder.addMatcher(appSlice.actions.toggleDarkMode.match, (state) => {
localStorage.setItem(storageNames.darkMode, state.darkMode)
})
builder.addMatcher(apiSlice.endpoints.getSettings.matchFulfilled, (state, { payload }) => {
state.authDisabled = payload.authDisabled
state.darkMode = localStorage.getItem(storageNames.darkMode)
})
}
})
Expand Down

0 comments on commit 07cdb5d

Please sign in to comment.