2020-03-16 03:23:03 +01:00
|
|
|
import { createStore } from "redux";
|
2020-03-21 15:52:08 +01:00
|
|
|
import { persistStore, persistReducer, createTransform } from "redux-persist";
|
2020-03-16 03:23:03 +01:00
|
|
|
import AsyncStorage from "@react-native-community/async-storage";
|
|
|
|
import rootReducer from "./reducers";
|
|
|
|
|
2020-03-21 15:52:08 +01:00
|
|
|
function decode(toRehydrate) {
|
|
|
|
return JSON.parse(toRehydrate, (_, value) => {
|
|
|
|
if (typeof value === "string" && value.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/))
|
|
|
|
return new Date(value);
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-16 03:23:03 +01:00
|
|
|
const persistConfig = {
|
|
|
|
key: "root",
|
|
|
|
storage: AsyncStorage,
|
2020-03-21 15:52:08 +01:00
|
|
|
transforms: [createTransform(JSON.stringify, decode)],
|
2020-03-16 03:23:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const persistedReducer = persistReducer(persistConfig, rootReducer);
|
|
|
|
|
|
|
|
export default () => {
|
|
|
|
const store = createStore(persistedReducer);
|
|
|
|
const persistor = persistStore(store);
|
|
|
|
return { store, persistor };
|
|
|
|
};
|