feat : add redux
This commit is contained in:
26
src/redux/slices/token.ts
Normal file
26
src/redux/slices/token.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
// src/slices/token.ts
|
||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||
|
||||
// Initial type for the token state
|
||||
interface TokenState {
|
||||
token: string;
|
||||
}
|
||||
|
||||
// Initial value for the token state
|
||||
const initialState: TokenState = {
|
||||
token: "",
|
||||
};
|
||||
|
||||
const tokenSlice = createSlice({
|
||||
name: "token",
|
||||
initialState,
|
||||
reducers: {
|
||||
setToken: (state, action: PayloadAction<string>) => {
|
||||
state.token = action.payload;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const { setToken } = tokenSlice.actions;
|
||||
|
||||
export default tokenSlice.reducer;
|
||||
61
src/redux/store.ts
Normal file
61
src/redux/store.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
// src/store.ts
|
||||
import { configureStore } from "@reduxjs/toolkit";
|
||||
import { encryptTransform } from "redux-persist-transform-encrypt";
|
||||
import storage from "redux-persist/lib/storage";
|
||||
import {
|
||||
persistStore,
|
||||
persistReducer,
|
||||
PersistConfig,
|
||||
FLUSH,
|
||||
REHYDRATE,
|
||||
PAUSE,
|
||||
PERSIST,
|
||||
PURGE,
|
||||
REGISTER,
|
||||
} from "redux-persist";
|
||||
import tokenReducer from "./slices/token";
|
||||
|
||||
// Encryption settings
|
||||
const secretKey = "token-encrypt-api";
|
||||
const encryptor = encryptTransform({
|
||||
secretKey,
|
||||
onError: (error) => {
|
||||
console.log(error);
|
||||
},
|
||||
});
|
||||
|
||||
// Type definition for the token state
|
||||
interface TokenState {
|
||||
token: string;
|
||||
}
|
||||
|
||||
// Persist configuration for the token
|
||||
const persistConfigToken: PersistConfig<TokenState> = {
|
||||
key: "token",
|
||||
storage,
|
||||
transforms: [encryptor], // Correct the typo (transform -> transforms)
|
||||
};
|
||||
|
||||
// Use persistReducer with the configuration and token reducer
|
||||
const persistedTokenReducer = persistReducer(persistConfigToken, tokenReducer);
|
||||
|
||||
// Store configuration
|
||||
const store = configureStore({
|
||||
reducer: {
|
||||
token: persistedTokenReducer,
|
||||
},
|
||||
middleware: (getDefaultMiddleware) =>
|
||||
getDefaultMiddleware({
|
||||
serializableCheck: {
|
||||
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
// Types for AppDispatch and RootState
|
||||
export type AppDispatch = typeof store.dispatch;
|
||||
export type RootState = ReturnType<typeof store.getState>;
|
||||
|
||||
export const persistor = persistStore(store);
|
||||
|
||||
export default store;
|
||||
Reference in New Issue
Block a user