2024-08-15 23:07:19 +02:00
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
|
|
import { configureStore } from "@reduxjs/toolkit";
|
|
|
|
import { rememberReducer, rememberEnhancer } from "redux-remember";
|
|
|
|
import reducers, { DEFAULT_PRELOADED_STATE } from "@/features/product/productSlice";
|
|
|
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
2024-06-27 16:06:39 +02:00
|
|
|
|
2024-08-19 16:08:54 +02:00
|
|
|
|
|
|
|
// thanks to https://github.com/rt2zz/redux-persist/issues/1208#issuecomment-658695446
|
|
|
|
// for the workaround
|
|
|
|
const createNoopStorage = () => {
|
|
|
|
return {
|
|
|
|
getItem(_key : any) {
|
|
|
|
return Promise.resolve(null);
|
|
|
|
},
|
|
|
|
setItem(_key : any, value : any) {
|
|
|
|
return Promise.resolve(value);
|
|
|
|
},
|
|
|
|
removeItem(_key : any) {
|
|
|
|
return Promise.resolve();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const storage =
|
|
|
|
typeof window === "undefined" ? createNoopStorage() : AsyncStorage;
|
|
|
|
|
|
|
|
export default storage;
|
|
|
|
|
2024-08-15 23:07:19 +02:00
|
|
|
const PERSIST_WHOLE_STORE =
|
|
|
|
new Boolean(process.env.PERSIST_WHOLE_STORE).valueOf() || false;
|
|
|
|
|
|
|
|
const rememberedKeys = ["products"];
|
2024-06-27 16:06:39 +02:00
|
|
|
|
|
|
|
const rootReducer = reducers;
|
|
|
|
|
2024-08-15 23:07:19 +02:00
|
|
|
// const isBrowser = typeof window !== "undefined";
|
|
|
|
|
|
|
|
|
|
|
|
export function setupStore(preloadedState = DEFAULT_PRELOADED_STATE) {
|
|
|
|
return configureStore({
|
|
|
|
reducer: rememberReducer(reducers),
|
|
|
|
preloadedState,
|
|
|
|
enhancers: (getDefaultEnhancers) =>
|
|
|
|
getDefaultEnhancers().concat(
|
2024-08-19 16:08:54 +02:00
|
|
|
rememberEnhancer(storage, rememberedKeys, {
|
2024-08-15 23:07:19 +02:00
|
|
|
persistWholeStore: PERSIST_WHOLE_STORE,
|
|
|
|
})
|
|
|
|
),
|
|
|
|
});
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export type RootState = ReturnType<typeof rootReducer>;
|
|
|
|
export type AppStore = ReturnType<typeof setupStore>;
|
2024-08-15 23:07:19 +02:00
|
|
|
export type AppDispatch = AppStore["dispatch"];
|
2024-06-27 23:31:59 +02:00
|
|
|
|
|
|
|
export const useAppDispatch = useDispatch.withTypes<AppDispatch>();
|
|
|
|
export const useAppSelector = useSelector.withTypes<RootState>();
|