From 76cedc05f54c5986400bfa67983cdb310292964f Mon Sep 17 00:00:00 2001 From: GreenWizard2015 Date: Wed, 3 Jan 2024 12:24:38 +0000 Subject: [PATCH] store UI in redux-persist --- ui/package-lock.json | 9 +++++++++ ui/package.json | 1 + ui/src/store/index.js | 26 +++++++++++++++++++++----- ui/src/store/slices/UI.js | 2 +- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/ui/package-lock.json b/ui/package-lock.json index 57b7539..56aeacb 100644 --- a/ui/package-lock.json +++ b/ui/package-lock.json @@ -19,6 +19,7 @@ "react-dom": "^18.2.0", "react-redux": "^9.0.4", "react-scripts": "5.0.1", + "redux-persist": "^6.0.0", "web-vitals": "^2.1.4" }, "devDependencies": { @@ -15360,6 +15361,14 @@ "resolved": "https://registry.npmjs.org/redux/-/redux-5.0.1.tgz", "integrity": "sha512-M9/ELqF6fy8FwmkpnF0S3YKOqMyoWJ4+CS5Efg2ct3oY9daQvd/Pc71FpGZsVsbl3Cpb+IIcjBDUnnyBdQbq4w==" }, + "node_modules/redux-persist": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/redux-persist/-/redux-persist-6.0.0.tgz", + "integrity": "sha512-71LLMbUq2r02ng2We9S215LtPu3fY0KgaGE0k8WRgl6RkqxtGfl7HUozz1Dftwsb0D/5mZ8dwAaPbtnzfvbEwQ==", + "peerDependencies": { + "redux": ">4.0.0" + } + }, "node_modules/redux-thunk": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/redux-thunk/-/redux-thunk-3.1.0.tgz", diff --git a/ui/package.json b/ui/package.json index dea39f4..bac5489 100644 --- a/ui/package.json +++ b/ui/package.json @@ -14,6 +14,7 @@ "react-dom": "^18.2.0", "react-redux": "^9.0.4", "react-scripts": "5.0.1", + "redux-persist": "^6.0.0", "web-vitals": "^2.1.4" }, "scripts": { diff --git a/ui/src/store/index.js b/ui/src/store/index.js index 7ae8a6f..a585188 100644 --- a/ui/src/store/index.js +++ b/ui/src/store/index.js @@ -1,8 +1,11 @@ -import { configureStore } from "@reduxjs/toolkit"; +import { combineReducers, configureStore } from "@reduxjs/toolkit"; import { Provider } from "react-redux"; +import { persistStore, persistReducer } from 'redux-persist'; +import storage from 'redux-persist/lib/storage'; // defaults to localStorage for web // slices import { ALL_APP_SLICES } from "./slices"; +import { PersistGate } from "redux-persist/integration/react"; // listeners // import { eventsListener } from "./listeners"; @@ -34,18 +37,31 @@ function buildAppStore(preloadedState) { // preloadedState is an optional parameter that allows you to pass in an initial state for the store. const AppStore = ({ children, preloadedState = {}, returnStore = false }) => { const { reducers, state } = buildAppStore(preloadedState); + // create a persisted reducer + const persistedReducer = persistReducer( + { + key: 'root', + storage, + whitelist: ['UI'] + }, + combineReducers(reducers) + ); + const store = configureStore({ - reducer: reducers, + reducer: persistedReducer, preloadedState: state, - // middleware: (getDefaultMiddleware) => getDefaultMiddleware().prepend(eventsListener.middleware), }); + const persistor = persistStore(store); + const provider = ( - {children} + + {children} + ); - if (returnStore) return { store, provider }; + if (returnStore) return { store, provider, persistor }; return provider; }; diff --git a/ui/src/store/slices/UI.js b/ui/src/store/slices/UI.js index d383fbe..6a158d1 100644 --- a/ui/src/store/slices/UI.js +++ b/ui/src/store/slices/UI.js @@ -1,7 +1,7 @@ import { createSlice } from '@reduxjs/toolkit'; const INITIAL_STATE = { - pouringTime: 0, + pouringTime: 1000, }; // slice for system status export const UISlice = createSlice({