Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: persisted store, account state resets to initial state after an… #3

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"react-redux": "^8.1.3",
"redux": "^4.2.1",
"redux-persist": "^6.0.0",
"redux-persist-expire": "^1.1.1",
"ts-unused-exports": "^10.0.1",
"vitest": "^0.34.6"
},
Expand Down
19 changes: 16 additions & 3 deletions src/app/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { combineReducers, configureStore } from "@reduxjs/toolkit";
import { accountSlice } from "@store/slices/account/account.slice";
import {
accountSlice,
initialAccountState,
} from "@store/slices/account/account.slice";
import { modalSlice } from "@store/slices/modal/modal.slice";
import { vaultSlice } from "@store/slices/vault/vault.slice";
import {
Expand All @@ -11,7 +14,9 @@ import {
REGISTER,
REHYDRATE,
persistReducer,
persistStore,
} from "redux-persist";
import expireReducer from "redux-persist-expire";
import storage from "redux-persist/lib/storage";

export interface RootState {
Expand All @@ -28,9 +33,15 @@ const rootReducer = combineReducers({

const persistConfig: PersistConfig<RootState> = {
key: "root",
version: 1,
storage,
storage: storage,
whitelist: ["account", "vault"],
transforms: [
expireReducer("account", {
persistedAtKey: "loadedAt",
expireSeconds: 3600,
expiredState: initialAccountState,
}),
],
};

const persistedReducer = persistReducer(persistConfig, rootReducer);
Expand All @@ -44,3 +55,5 @@ export const store = configureStore({
},
}),
});

export const persistor = persistStore(store);
10 changes: 6 additions & 4 deletions src/app/store/slices/account/account.slice.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import { createSlice } from "@reduxjs/toolkit";

import { Network } from "@models/network";
import { WalletType } from "@models/wallet";
import { createSlice } from "@reduxjs/toolkit";

export interface AccountState {
address: string | undefined;
walletType: WalletType | undefined;
network: Network | undefined;
loadedAt: string | undefined;
}

const initialState: AccountState = {
export const initialAccountState: AccountState = {
address: undefined,
walletType: undefined,
network: undefined,
loadedAt: undefined,
};

export const accountSlice = createSlice({
name: "account",
initialState: initialState,
initialState: initialAccountState,
reducers: {
login: (state, action) => {
state.address = action.payload.address;
state.walletType = action.payload.walletType;
state.network = action.payload.network;
state.loadedAt = new Date().toJSON();
},
logout: (state) => {
state.address = undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/app/store/slices/modal/modal.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ interface ModalState {
isSelectWalletModalOpen: boolean;
}

const initialState: ModalState = {
const initialModalState: ModalState = {
isSelectWalletModalOpen: false,
};

export const modalSlice = createSlice({
name: "modal",
initialState: initialState,
initialState: initialModalState,
reducers: {
toggleSelectWalletModalVisibility: (state) => {
state.isSelectWalletModalOpen = !state.isSelectWalletModalOpen;
Expand Down
4 changes: 2 additions & 2 deletions src/app/store/slices/vault/vault.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ interface VaultState {
error: string | null;
}

const initialState: VaultState = {
const initialVaultState: VaultState = {
vaults: [],
status: "idle",
error: null,
};

export const vaultSlice = createSlice({
name: "vault",
initialState: initialState,
initialState: initialVaultState,
reducers: {},
});
13 changes: 8 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import ReactDOM from "react-dom/client";
import { Provider as ReduxProvider } from "react-redux";
import "@fontsource/poppins";

import { ChakraProvider } from "@chakra-ui/react";
import "@fontsource/poppins";
import { PersistGate } from "redux-persist/integration/react";

import { App } from "./app/app";
import { store } from "./app/store";
import { persistor, store } from "./app/store";
import { appTheme } from "./styles/app-theme";

ReactDOM.createRoot(document.getElementById("root")!).render(
<ReduxProvider store={store}>
<ChakraProvider resetCSS theme={appTheme}>
<App />
</ChakraProvider>
<PersistGate persistor={persistor} loading={null}>
<ChakraProvider resetCSS theme={appTheme}>
<App />
</ChakraProvider>
</PersistGate>
</ReduxProvider>,
);
Loading