Skip to content

TagSpaces is an offline, open source, document manager with tagging support

redux-persist v5 (#779)

Stanimir073ded9ee310
diff --git a/app/app.html b/app/app.html
index 1b1987f8b..f764a9601 100644
--- a/app/app.html
+++ b/app/app.html
@@ -6,6 +6,20 @@
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
+ <style type="text/css">
+ body {
+ overflow: hidden; margin: 0; padding: 0;
+ }
+ #loadingLogo {
+ top:45%; width: 100%; text-align: center; position:absolute;
+ }
+ #loading {
+ text-align: center; width: 100%; height: 100%;
+ font-size: 1.4em; position:absolute; z-index: 999999;
+ background-color: #F5F5F5;
+ user-select: none; -webkit-user-select: none; -moz-user-select: none;
+ }
+ </style>
<script>
(function() {
if (!process.env.HOT) {
@@ -18,7 +32,29 @@
</script>
</head>
<body>
- <div id="root"></div>
+ <div id="root">
+ <div id="loading" class="appBackgroundTile" style="-webkit-app-region: drag">
+ <div id="loadingLogo">
+ <svg style="vertical-align: middle" xmlns="http://www.w3.org/2000/svg" version="1.1" width="100" height="100">
+ <g id="layer1" transform="matrix(0.33346217,0,0,0.33346217,-0.01300084,-250.91)">
+ <rect style="fill:none;fill-opacity:1;stroke:none" id="rect3052-8-1" width="299.42227" height="299.42227" x="-0.019505024" y="752.95935" />
+ <path style="fill:#ffffff;fill-opacity:1;stroke:none" d="m 181.90177,787.75394 70.88695,43.53834 C 180.29225,977.67059 187.41436,950.59754 124.58989,989.154 L 65.05006,905.90077 Z" id="rect3052-8-2-2" />
+ <ellipse style="fill:#616577;fill-opacity:1;stroke:none" id="path4875-4-7-5-6-2" cx="192.4187" cy="851.11481" rx="15.970615" ry="15.782593" />
+ <path style="fill:none;stroke:#a466aa;stroke-width:27.50978279;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
+ d="M 159.73623,964.91671 134.54529,941.61879" id="path3108-5-39-8" />
+ <path style="fill:none;stroke:#ffffff;stroke-width:34.19813538;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
+ d="m 162.16161,795.17204 90.03628,0.34544 0,86.1724 -115.50748,117.79375 -88.457192,-81.6665 z" id="path3923-1-1-4-9-8" />
+ <path style="fill:#f7901e;fill-opacity:1;stroke:#33b5be;stroke-width:27.50978279;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
+ d="m 204.81469,910.87834 -50.38182,-45.3016" id="path3108-9-1-76-7" />
+ <path style="fill:#616577;fill-opacity:1;stroke:#f7901e;stroke-width:27.50978279;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
+ d="M 182.93838,938.7066 144.48907,904.40668" id="path3108-0-7-9-8" />
+ <path style="fill:none;stroke:#616577;stroke-width:31.96374702;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
+ d="m 164.34385,800.70161 83.03644,0 0,79.10209 -110.49206,113.42316 -82.031957,-75.83333 z" id="path3923-1-1-4-9" />
+ </g>
+ </svg>
+ </div>
+ </div>
+ </div>
<script>
{
const scripts = [];
diff --git a/app/components/LoadingScreen.js b/app/components/LoadingScreen.js
new file mode 100644
index 000000000..d47c6d1cd
--- /dev/null
+++ b/app/components/LoadingScreen.js
@@ -0,0 +1,9 @@
+import React from 'react';
+
+export default class LoadingScreen extends React.Component {
+ render() {
+ return (
+ <div id="loadingLogo">Loading ...</div>
+ );
+ }
+}
diff --git a/app/containers/Root.js b/app/containers/Root.js
index 08e839120..9a47e754f 100644
--- a/app/containers/Root.js
+++ b/app/containers/Root.js
@@ -19,20 +19,68 @@
import React from 'react';
import { Provider } from 'react-redux';
+import { PersistGate } from 'redux-persist/integration/react';
import { ConnectedRouter } from 'react-router-redux';
import Routes from '../routes';
+import LoadingScreen from '../components/LoadingScreen';
+import { actions as AppActions } from '../reducers/app';
+import {
+ actions as SettingsActions,
+ getCheckForUpdateOnStartup,
+ isGlobalKeyBindingEnabled
+} from '../reducers/settings';
+import { getDefaultLocationId } from '../reducers/locations';
+import PlatformIO from '../services/platform-io';
+// import MainPage from './MainPage';
+import { getURLParameter } from '../utils/misc';
type RootType = {
store: {},
+ persistor: {},
history: {}
};
-export default function Root({ store, history }: RootType) {
+function onBeforeLift(store) {
+ store.dispatch(SettingsActions.setZoomRestoreApp());
+ store.dispatch(SettingsActions.upgradeSettings()); // TODO call this only on app version update
+ const state = store.getState();
+ const defaultLocationId = getDefaultLocationId(state);
+ if (defaultLocationId && defaultLocationId.length > 0) {
+ store.dispatch(AppActions.openLocation(defaultLocationId));
+ }
+ if (getCheckForUpdateOnStartup(state)) {
+ store.dispatch(SettingsActions.checkForUpdate());
+ }
+ PlatformIO.setGlobalShortcuts(isGlobalKeyBindingEnabled(state));
+ const langURLParam = getURLParameter('locale');
+ if (
+ langURLParam &&
+ langURLParam.length > 1 &&
+ /^[a-zA-Z\-_]+$/.test('langURLParam')
+ ) {
+ store.dispatch(SettingsActions.setLanguage(langURLParam));
+ }
+}
+
+export default function Root({ store, persistor, history }: RootType) {
return (
<Provider store={store}>
- <ConnectedRouter history={history}>
- <Routes />
- </ConnectedRouter>
+ {/**
+ * PersistGate delays the rendering of the app's UI until the persisted state has been retrieved
+ * and saved to redux.
+ * The `loading` prop can be `null` or any react instance to show during loading (e.g. a splash screen),
+ * for example `loading={<SplashScreen />}`.
+ * @see https://github.com/rt2zz/redux-persist/blob/master/docs/PersistGate.md
+ */}
+ <PersistGate
+ loading={<LoadingScreen />}
+ onBeforeLift={() => onBeforeLift(store)}
+ persistor={persistor}
+ >
+ <ConnectedRouter history={history}>
+ <Routes />
+ </ConnectedRouter>
+ </PersistGate>
</Provider>
);
}
diff --git a/app/index.js b/app/index.js
index 8aaf9a8d1..35dc05774 100644
--- a/app/index.js
+++ b/app/index.js
@@ -24,13 +24,8 @@ import { AppContainer } from 'react-hot-loader';
import Root from './containers/Root';
import { configureStore, history } from './store/configureStore';
import './app.global.css';
-import { actions as AppActions } from './reducers/app';
-import { getDefaultLocationId } from './reducers/locations';
-import { actions as SettingsActions, getCheckForUpdateOnStartup, isGlobalKeyBindingEnabled } from './reducers/settings';
-import PlatformIO from './services/platform-io';
-import { getURLParameter } from './utils/misc';
-const store = configureStore();
+const { store, persistor } = configureStore();
document.addEventListener('contextmenu', event => event.preventDefault());
@@ -64,7 +59,7 @@ document.addEventListener('dragleave', (event) => {
// event.preventDefault();
}); */
-document.addEventListener('storeLoaded', () => {
+/* document.addEventListener('storeLoaded', () => {
checkIsFirstRun();
store.dispatch(SettingsActions.setZoomRestoreApp());
store.dispatch(SettingsActions.upgradeSettings()); // TODO call this only on app version update
@@ -86,23 +81,24 @@ document.addEventListener('storeLoaded', () => {
function checkIsFirstRun() {
// history.push('/login');
-}
+ console.log('');
+} */
render(
<AppContainer>
- <Root store={store} history={history} />
+ <Root store={store} persistor={persistor} history={history} />
</AppContainer>,
document.getElementById('root')
);
-if (module.hot) {
+/* if (module.hot) {
module.hot.accept('./containers/Root', () => {
const NextRoot = require('./containers/Root'); // eslint-disable-line global-require
render(
<AppContainer>
- <NextRoot store={store} history={history} />
+ <NextRoot store={store} persistor={persistor} history={history} />
</AppContainer>,
document.getElementById('root')
);
});
-}
+} */
diff --git a/app/reducers/index.js b/app/reducers/index.js
index e8fd68417..a320b8d0f 100644
--- a/app/reducers/index.js
+++ b/app/reducers/index.js
@@ -17,15 +17,38 @@
* @flow
*/
-import { combineReducers } from 'redux';
+import { persistCombineReducers } from 'redux-persist';
import { routerReducer as router } from 'react-router-redux';
+import getStoredStateMigrateV4 from 'redux-persist/lib/integration/getStoredStateMigrateV4';
+// import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
+import storage from 'redux-persist/lib/storage';
import settings from './settings';
import app from './app';
import locations from './locations';
import taglibrary from './taglibrary';
import locationIndex from './location-index';
-const rootReducer = combineReducers({
+
+const rootPersistConfig = {
+ key: 'root',
+ getStoredState: getStoredStateMigrateV4({ blacklist: ['app', 'locationIndex'] }),
+ storage,
+ version: 1,
+ blacklist: ['app', 'locationIndex'],
+ debug: true,
+ // https://github.com/rt2zz/redux-persist/blob/b6a60bd653d59c4fe462e2e0ea827fd76eb190e1/README.md#state-reconciler
+ // stateReconciler: autoMergeLevel2,
+};
+
+const rootReducer = persistCombineReducers(rootPersistConfig, {
+ /* settings: persistReducer(
+ {
+ key: 'settings',
+ getStoredState: getStoredStateMigrateV4({ keyPrefix: 'reduxPersist' }),
+ storage
+ },
+ settings,
+ ), */
settings,
app,
locations,
diff --git a/app/store/configureStore.dev.js b/app/store/configureStore.dev.js
index d8f75042e..63671d329 100644
--- a/app/store/configureStore.dev.js
+++ b/app/store/configureStore.dev.js
@@ -18,10 +18,10 @@
*/
import { createStore, applyMiddleware, compose } from 'redux';
-import { persistStore, autoRehydrate } from 'redux-persist';
+import { persistStore } from 'redux-persist';
import thunk from 'redux-thunk';
import { createHashHistory } from 'history';
-import { routerMiddleware, routerActions, push } from 'react-router-redux';
+import { routerMiddleware, routerActions } from 'react-router-redux';
import { createLogger } from 'redux-logger';
import rootReducer from '../reducers';
import onlineListener from '../services/onlineListener';
@@ -63,7 +63,7 @@ const configureStore = (initialState) => {
// Apply Middleware & Compose Enhancers
enhancers.push(applyMiddleware(...middleware));
- enhancers.push(autoRehydrate());
+ // enhancers.push(autoRehydrate()); removed in v5
const enhancer = composeEnhancers(...enhancers);
// Create Store
@@ -71,11 +71,11 @@ const configureStore = (initialState) => {
onlineListener(store.dispatch);
- persistStore(store, { blacklist: ['app', 'locationIndex'] }, () => {
+ const persistor = persistStore(store);/* , null, () => {
document.dispatchEvent(new Event('storeLoaded'));
// store.dispatch(push('/main'));
console.log('Store rehydrated.');
- });
+ }); */
if (module.hot) {
module.hot.accept('../reducers', () =>
@@ -83,7 +83,7 @@ const configureStore = (initialState) => {
);
}
- return store;
+ return { store, persistor };
};
export default { configureStore, history };
diff --git a/app/store/configureStore.prod.js b/app/store/configureStore.prod.js
index 3ab7b611d..e78bf9a7c 100644
--- a/app/store/configureStore.prod.js
+++ b/app/store/configureStore.prod.js
@@ -18,7 +18,7 @@
*/
import { compose, createStore, applyMiddleware } from 'redux';
-import { persistStore, autoRehydrate } from 'redux-persist';
+import { persistStore } from 'redux-persist';
import thunk from 'redux-thunk';
import { createBrowserHistory } from 'history';
import { routerMiddleware } from 'react-router-redux';
@@ -28,17 +28,17 @@ import onlineListener from '../services/onlineListener';
const history = createBrowserHistory();
const router = routerMiddleware(history);
const enhancer = compose(
- applyMiddleware(thunk, router),
- autoRehydrate()
+ applyMiddleware(thunk, router)
+ // autoRehydrate()
);
function configureStore(initialState) {
const store = createStore(rootReducer, initialState, enhancer);
onlineListener(store.dispatch);
- persistStore(store, { blacklist: ['app', 'locationIndex'] }, () => {
+ const persistor = persistStore(store); /* , null, () => {
document.dispatchEvent(new Event('storeLoaded'));
- });
- return store;
+ }); */
+ return { store, persistor };
}
export default { configureStore, history };
diff --git a/package.json b/package.json
index 36d30d13b..2f914be5f 100644
--- a/package.json
+++ b/package.json
@@ -228,7 +228,7 @@
"react-split-pane": "^0.1.84",
"react-swipeable-views": "^0.13.0",
"redux": "^3.7.2",
- "redux-persist": "^4.8.2",
+ "redux-persist": "^5.10.0",
"redux-thunk": "^2.2.0",
"remove-markdown": "^0.2.2",
"semver": "^5.5.0",
diff --git a/yarn.lock b/yarn.lock
index 213cf6c6e..273a17e16 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -7472,7 +7472,7 @@ json-stable-stringify@^1.0.1:
dependencies:
jsonify "~0.0.0"
-json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1:
+json-stringify-safe@~5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=
@@ -7831,7 +7831,7 @@ locate-path@^3.0.0:
p-locate "^3.0.0"
path-exists "^3.0.0"
-lodash-es@^4.17.4, lodash-es@^4.2.1:
+lodash-es@^4.2.1:
version "4.17.11"
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.11.tgz#145ab4a7ac5c5e52a3531fb4f310255a152b4be0"
integrity sha512-DHb1ub+rMjjrxqlB3H56/6MXtm1lSksDp2rA2cNWjG8mlDUYFhUj3Di2Zn5IwSU87xLv8tNIQ7sSwE/YOX/D/Q==
@@ -10964,14 +10964,10 @@ redux-logger@^3.0.6:
dependencies:
deep-diff "^0.3.5"
-redux-persist@^4.8.2:
- version "4.10.2"
- resolved "https://registry.yarnpkg.com/redux-persist/-/redux-persist-4.10.2.tgz#8efdb16cfe882c521a78a6d0bfdfef2437f49f96"
- integrity sha512-U+e0ieMGC69Zr72929iJW40dEld7Mflh6mu0eJtVMLGfMq/aJqjxUM1hzyUWMR1VUyAEEdPHuQmeq5ti9krIgg==
- dependencies:
- json-stringify-safe "^5.0.1"
- lodash "^4.17.4"
- lodash-es "^4.17.4"
+redux-persist@^5.10.0:
+ version "5.10.0"
+ resolved "https://registry.yarnpkg.com/redux-persist/-/redux-persist-5.10.0.tgz#5d8d802c5571e55924efc1c3a9b23575283be62b"
+ integrity sha512-sSJAzNq7zka3qVHKce1hbvqf0Vf5DuTVm7dr4GtsqQVOexnrvbV47RWFiPxQ8fscnyiuWyD2O92DOxPl0tGCRg==
redux-thunk@^2.2.0:
version "2.3.0"