save progress in local storage

This commit is contained in:
Alexander Bentkamp
2023-01-19 16:10:08 +01:00
parent 8dd3c09339
commit 206152a07c
6 changed files with 40 additions and 3 deletions
+12 -1
View File
@@ -3,13 +3,15 @@ import { connection } from '../connection'
import thunkMiddleware from 'redux-thunk'
import { apiSlice } from './api'
import { progressSlice } from './progress'
import { saveState } from "./localStorage";
import { debounce } from "debounce";
export const store = configureStore({
reducer: {
[apiSlice.reducerPath]: apiSlice.reducer,
[progressSlice.name]: progressSlice.reducer,
},
// Make connection available in thunks:
middleware: getDefaultMiddleware =>
getDefaultMiddleware({
thunk: {
@@ -18,6 +20,15 @@ export const store = configureStore({
}).concat(apiSlice.middleware),
});
// Save progress in local storage
store.subscribe(
// we use debounce to save the state once each 800ms
// for better performances in case multiple changes occur in a short time
debounce(() => {
saveState(store.getState()[progressSlice.name]);
}, 800)
);
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}