From 05244c4a6619a105098327ba795c72e7a42de1aa Mon Sep 17 00:00:00 2001 From: felinae98 <731499577@qq.com> Date: Wed, 10 Aug 2022 22:11:10 +0800 Subject: [PATCH] add redux-persist --- admin-frontend/package.json | 4 ++- admin-frontend/src/app/store.ts | 45 +++++++++++++++++++++++++++------ admin-frontend/src/index.tsx | 11 +++++--- admin-frontend/yarn.lock | 12 +++++++++ 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/admin-frontend/package.json b/admin-frontend/package.json index c8967d4..32f7b0b 100644 --- a/admin-frontend/package.json +++ b/admin-frontend/package.json @@ -13,11 +13,13 @@ "@types/node": "^17.0.25", "@types/react": "^18.0.6", "@types/react-dom": "^18.0.2", + "@types/redux-persist": "^4.3.1", "react": "^18.2.0", "react-dom": "^18.2.0", "react-redux": "^8.0.1", "react-router-dom": "^6.3.0", "react-scripts": "5.0.1", + "redux-persist": "^6.0.0", "typescript": "^4.6.0", "web-vitals": "^2.1.0" }, @@ -47,9 +49,9 @@ ] }, "devDependencies": { - "eslint": "^7.32.0 || ^8.2.0", "@typescript-eslint/eslint-plugin": "^5.31.0", "@typescript-eslint/parser": "^5.31.0", + "eslint": "^7.32.0 || ^8.2.0", "eslint-config-airbnb": "^19.0.4", "eslint-config-airbnb-typescript": "^17.0.0", "eslint-import-resolver-typescript": "^3.3.0", diff --git a/admin-frontend/src/app/store.ts b/admin-frontend/src/app/store.ts index 94a3072..09e59fc 100644 --- a/admin-frontend/src/app/store.ts +++ b/admin-frontend/src/app/store.ts @@ -1,21 +1,50 @@ -import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit'; +import { + Action, combineReducers, configureStore, ThunkAction, +} from '@reduxjs/toolkit'; +import { + persistStore, + persistReducer, + FLUSH, + REHYDRATE, + PAUSE, + PERSIST, + PURGE, + REGISTER, +} from 'redux-persist'; +import storage from 'redux-persist/lib/storage'; import authReducer from '../features/auth/authSlice'; import globalConfReducer from '../features/globalConf/globalConfSlice'; import { subscribeApi } from '../features/subsribeConfigManager/subscribeConfigSlice'; import { weightApi } from '../features/weightConfig/weightConfigSlice'; +const rootReducer = combineReducers({ + auth: authReducer, + globalConf: globalConfReducer, + [subscribeApi.reducerPath]: subscribeApi.reducer, + [weightApi.reducerPath]: weightApi.reducer, +}); + +const persistConfig = { + key: 'root', + storage, + whitelist: ['auth'], +}; + +const persistedReducer = persistReducer(persistConfig, rootReducer); + export const store = configureStore({ - reducer: { - auth: authReducer, - globalConf: globalConfReducer, - [subscribeApi.reducerPath]: subscribeApi.reducer, - [weightApi.reducerPath]: weightApi.reducer, - }, - middleware: (getDefaultMiddleware) => getDefaultMiddleware() + reducer: persistedReducer, + middleware: (getDefaultMiddleware) => getDefaultMiddleware({ + serializableCheck: { + ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER], + }, + }) .concat(subscribeApi.middleware) .concat(weightApi.middleware), }); +export const persistor = persistStore(store); + export type AppDispatch = typeof store.dispatch; export type RootState = ReturnType; export type AppThunk = ThunkAction< diff --git a/admin-frontend/src/index.tsx b/admin-frontend/src/index.tsx index 8c5dd16..26d5f6b 100644 --- a/admin-frontend/src/index.tsx +++ b/admin-frontend/src/index.tsx @@ -2,8 +2,9 @@ import React from 'react'; import { createRoot } from 'react-dom/client'; import { Provider } from 'react-redux'; import { BrowserRouter } from 'react-router-dom'; +import { PersistGate } from 'redux-persist/integration/react'; import App from './App'; -import { store } from './app/store'; +import { persistor, store } from './app/store'; import './index.css'; import reportWebVitals from './reportWebVitals'; @@ -13,9 +14,11 @@ const root = createRoot(container); root.render( - - - + + + + + , ); diff --git a/admin-frontend/yarn.lock b/admin-frontend/yarn.lock index edc59fd..9b44c52 100644 --- a/admin-frontend/yarn.lock +++ b/admin-frontend/yarn.lock @@ -2085,6 +2085,13 @@ "@types/scheduler" "*" csstype "^3.0.2" +"@types/redux-persist@^4.3.1": + version "4.3.1" + resolved "https://registry.npmmirror.com/@types/redux-persist/-/redux-persist-4.3.1.tgz#aa4c876859e0bea5155e5f7980e5b8c4699dc2e6" + integrity sha512-YkMnMUk+4//wPtiSTMfsxST/F9Gh9sPWX0LVxHuOidGjojHtMdpep2cYvQgfiDMnj34orXyZI+QJCQMZDlafKA== + dependencies: + redux-persist "*" + "@types/resolve@1.17.1": version "1.17.1" resolved "https://registry.npmmirror.com/@types/resolve/-/resolve-1.17.1.tgz#3afd6ad8967c77e4376c598a82ddd58f46ec45d6" @@ -7592,6 +7599,11 @@ redent@^3.0.0: indent-string "^4.0.0" strip-indent "^3.0.0" +redux-persist@*, redux-persist@^6.0.0: + version "6.0.0" + resolved "https://registry.npmmirror.com/redux-persist/-/redux-persist-6.0.0.tgz#b4d2972f9859597c130d40d4b146fecdab51b3a8" + integrity sha512-71LLMbUq2r02ng2We9S215LtPu3fY0KgaGE0k8WRgl6RkqxtGfl7HUozz1Dftwsb0D/5mZ8dwAaPbtnzfvbEwQ== + redux-thunk@^2.4.1: version "2.4.1" resolved "https://registry.npmmirror.com/redux-thunk/-/redux-thunk-2.4.1.tgz#0dd8042cf47868f4b29699941de03c9301a75714"