client/src/store/configureStore.js
changeset 129 d48946d164c6
parent 101 e165aa89ac82
child 132 906a6c7c7943
--- a/client/src/store/configureStore.js	Tue Jul 25 19:11:26 2017 +0200
+++ b/client/src/store/configureStore.js	Fri Jul 28 19:40:35 2017 +0200
@@ -1,39 +1,43 @@
 import rootReducer from '../reducers';
 import rootAuthSaga from '../sagas/authSaga';
 import rootGroupSaga from '../sagas/groupSaga';
+import rootSyncSaga from '../sagas/syncSaga';
 import networkSaga from '../sagas/networkSaga';
 import { compose, createStore, applyMiddleware } from 'redux';
 import { routerMiddleware } from 'react-router-redux';
 import createSagaMiddleware from 'redux-saga'
 import Immutable from 'immutable';
-import { offline } from 'redux-offline';
-import offlineDefaultConfig from 'redux-offline/lib/defaults';
+import {persistStore, autoRehydrate} from 'redux-persist-immutable'
 import localForage from 'localforage';
 import immutableTransform from 'redux-persist-transform-immutable';
 import NoteRecord from './noteRecord';
 import SessionRecord from './sessionRecord';
 import UserRecord from './userRecord';
 import APIClient from '../api/APIClient';
-import createEffect from '../api';
 import config from '../config';
-import { offlineConfigInitialized } from '../actions/networkActions';
 import asyncRequest from '../constants/asyncRequest';
-import * as types from '../constants/actionTypes';
 
-// const composeEnhancers = (process.env.NODE_ENV !== 'production' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ?
-//     window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
-//         shouldHotReload: false,
-//     }) : compose;
-const composeEnhancers = compose;
+const composeEnhancers = (process.env.NODE_ENV !== 'production' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ?
+    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
+        shouldHotReload: false,
+    }) : compose;
 
 
 const defaultState = {
   sessions: Immutable.List([]),
   notes: Immutable.List([]),
   groups: Immutable.List([]),
-  isAuthenticated: false,
-  currentUser: null,
-  token: '',
+  status: Immutable.Map({
+    isSynchronizing: false,
+    online: false
+  }),
+  authStatus: Immutable.Map({
+    token: '',
+    isAuthenticated: false,
+    clientId: null,
+    lastSync: 0,
+    currentUser: null,
+  }),
   autoSubmit: false,
   login: asyncRequest,
   register: asyncRequest,
@@ -42,24 +46,19 @@
 
 const immutableTransformConfig = {
   records: [NoteRecord, SessionRecord, UserRecord],
-  whitelist: ['sessions', 'notes', 'currentUser']
+  whitelist: ['sessions', 'notes', 'authStatus']
 }
 
 const persistOptions = {
   storage: localForage,
+  records: [NoteRecord, SessionRecord, UserRecord],
   transforms: [immutableTransform(immutableTransformConfig)],
-  whitelist: ['sessions', 'notes', 'isAuthenticated', 'currentUser', 'token', 'offline', 'autoSubmit']
+  whitelist: ['sessions', 'notes', 'autoSubmit', 'authStatus']
 }
 
 const apiClient = new APIClient(config.apiRootUrl);
 
-const offlineConfig = {
-  ...offlineDefaultConfig,
-  persistOptions,
-  effect: createEffect(apiClient),
-}
-
-const storeInitialState = { ...defaultState };
+const storeInitialState = Immutable.Map({ ...defaultState });
 
 
 export default (history, initialState = storeInitialState) => {
@@ -67,10 +66,9 @@
   const router = routerMiddleware(history);
   const saga = createSagaMiddleware();
 
-  offlineConfig.detectNetwork = callback => { saga.run(networkSaga, { callback }); };
-
-  const store = offline(offlineConfig)(createStore)(rootReducer, initialState, composeEnhancers(
-    applyMiddleware(router, saga)
+  const store = createStore(rootReducer, initialState, composeEnhancers(
+    applyMiddleware(router, saga),
+    autoRehydrate()
   ));
 
   apiClient.setStore(store);
@@ -80,11 +78,12 @@
     history
   }
 
+  persistStore(store, persistOptions);
+
   saga.run(rootAuthSaga, context);
   saga.run(rootGroupSaga, context);
-
-  store.dispatch(offlineConfigInitialized({ client: apiClient }))
-  store.dispatch({ type: types.GROUP_LOAD_ASYNC })
+  saga.run(rootSyncSaga, context);
+  saga.run(networkSaga, context);
 
   return store;
 };