adds middleware to sync redux with local storage also adds ununsed

import error rule to linter
This commit is contained in:
ajaythapliyal
2023-02-24 18:46:46 +05:30
parent d98b558ab0
commit b662c30508
8 changed files with 81 additions and 5922 deletions

View File

@@ -1,6 +1,10 @@
import { createSlice } from '@reduxjs/toolkit';
import { Doc } from './preferenceApi';
import store from '../store';
import {
createListenerMiddleware,
createSlice,
isAnyOf,
} from '@reduxjs/toolkit';
import { Doc, setLocalApiKey, setLocalRecentDocs } from './preferenceApi';
import { RootState } from '../store';
interface Preference {
apiKey: string;
@@ -33,7 +37,23 @@ export const prefSlice = createSlice({
export const { setApiKey, setSelectedDocs, setSourceDocs } = prefSlice.actions;
export default prefSlice.reducer;
type RootState = ReturnType<typeof store.getState>;
export const prefListenerMiddleware = createListenerMiddleware();
prefListenerMiddleware.startListening({
matcher: isAnyOf(setApiKey),
effect: (action, listenerApi) => {
setLocalApiKey((listenerApi.getState() as RootState).preference.apiKey);
},
});
prefListenerMiddleware.startListening({
matcher: isAnyOf(setSelectedDocs),
effect: (action, listenerApi) => {
setLocalRecentDocs(
(listenerApi.getState() as RootState).preference.selectedDocs ??
([] as unknown as Doc),
);
},
});
export const selectApiKey = (state: RootState) => state.preference.apiKey;
export const selectApiKeyStatus = (state: RootState) =>