-
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(favorites): added persistence for favorites store
- Loading branch information
1 parent
83a9f4d
commit 2ab7f4f
Showing
4 changed files
with
40 additions
and
17 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,33 +1,47 @@ | ||
import { configureStore } from '@reduxjs/toolkit'; | ||
import { combineReducers, configureStore } from '@reduxjs/toolkit'; | ||
import { setupListeners } from '@rtk-incubator/rtk-query'; | ||
import { persistStore, persistReducer, FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER } from 'redux-persist'; | ||
import storage from 'redux-persist/lib/storage'; | ||
import { charactersApi } from '../services/characters'; | ||
import { episodesApi } from '../services/episodes'; | ||
import { locationsApi } from '../services/locations'; | ||
import { charactersDialogSlice } from './charactersDialog'; | ||
import { favoritesSlice } from './favorites'; | ||
import { headerTitleSlice } from './headerTitle'; | ||
|
||
// persist reducer | ||
const persistConfig = { | ||
key: 'favorites', | ||
version: 1, | ||
storage, | ||
whitelist: ['favorites'] | ||
}; | ||
|
||
// reducer | ||
const rootReducer = combineReducers({ | ||
[charactersApi.reducerPath]: charactersApi.reducer, | ||
[locationsApi.reducerPath]: locationsApi.reducer, | ||
[episodesApi.reducerPath]: episodesApi.reducer, | ||
headerTitle: headerTitleSlice.reducer, | ||
charactersDialog: charactersDialogSlice.reducer, | ||
favorites: favoritesSlice.reducer | ||
}); | ||
|
||
const store = configureStore({ | ||
reducer: { | ||
// Add the generated reducer as a specific top-level slice | ||
[charactersApi.reducerPath]: charactersApi.reducer, | ||
[locationsApi.reducerPath]: locationsApi.reducer, | ||
[episodesApi.reducerPath]: episodesApi.reducer, | ||
headerTitle: headerTitleSlice.reducer, | ||
charactersDialog: charactersDialogSlice.reducer, | ||
favorites: favoritesSlice.reducer | ||
}, | ||
// Adding the api middleware enables caching, invalidation, polling, | ||
// and other useful features of `rtk-query`. | ||
reducer: persistReducer(persistConfig, rootReducer), | ||
middleware: (getDefaultMiddleware) => | ||
getDefaultMiddleware() | ||
getDefaultMiddleware({ | ||
serializableCheck: { | ||
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER] | ||
} | ||
}) | ||
.concat(charactersApi.middleware) | ||
.concat(locationsApi.middleware) | ||
.concat(episodesApi.middleware) | ||
}); | ||
|
||
export const persistor = persistStore(store); | ||
|
||
export default store; | ||
|
||
// optional, but required for refetchOnFocus/refetchOnReconnect behaviors | ||
// see `setupListeners` docs - takes an optional callback as the 2nd arg for customization | ||
setupListeners(store.dispatch); |
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