client/src/store/configureStore.js
changeset 62 b2514a9bcd49
parent 59 1eb52770eefa
child 67 9206af01f5e5
--- a/client/src/store/configureStore.js	Tue Jun 20 12:11:57 2017 +0200
+++ b/client/src/store/configureStore.js	Tue Jun 20 14:13:15 2017 +0200
@@ -1,51 +1,79 @@
 import rootReducer from '../reducers';
 import rootSaga from '../sagas';
-import { loadSessions } from '../actions/sessionsActions';
-import { createStore, applyMiddleware, compose } from 'redux';
+import { compose, createStore, applyMiddleware } from 'redux';
 import { routerMiddleware } from 'react-router-redux';
-import handleTransitions from 'redux-history-transitions';
 import createSagaMiddleware from 'redux-saga'
 import Immutable from 'immutable';
-import APIClient from '../APIClient';
+import { offline } from 'redux-offline';
+import offlineDefaultConfig from 'redux-offline/lib/defaults';
+import localForage from 'localforage';
+import immutableTransform from 'redux-persist-transform-immutable'
+import NoteRecord from './noteRecord'
+import SessionRecord from './sessionRecord'
+import APIClient from '../api/APIClient';
+import createEffect from '../api';
 import config from '../config';
 
-const token = localStorage.getItem('token');
-const currentUser = localStorage.getItem('currentUser');
+// const composeEnhancers = (process.env.NODE_ENV !== 'production' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ?
+//     window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
+//         shouldHotReload: false,
+//     }) : compose;
+const composeEnhancers = compose;
+
 
 const defaultState = {
-  currentSession: null,
   sessions: Immutable.List([]),
   notes: Immutable.List([]),
-  isAuthenticated: token !== null,
-  currentUser: currentUser ? JSON.parse(currentUser) : null,
-  token: token,
-  login: {
+  isAuthenticated: false,
+  currentUser: null,
+  token: '',
+  login: Immutable.Map({
     loading: false,
     success: false,
     error: false,
-  }
+  })
 };
 
-const storeInitialState = Immutable.Map(defaultState);
+const immutableTransformConfig = {
+  records: [NoteRecord, SessionRecord],
+  whitelist: ['sessions', 'notes', 'currentUser']
+}
+
+const persistOptions = {
+  storage: localForage,
+  transforms: [immutableTransform(immutableTransformConfig)],
+  whitelist: ['sessions', 'notes', 'isAuthenticated', 'currentUser', 'token']
+}
+
+const apiClient = new APIClient(config.apiRootUrl);
+
+const offlineConfig = {
+  ...offlineDefaultConfig,
+  persistOptions,
+  effect: createEffect(apiClient)
+//  detectNetwork: callback => callback(true),
+}
+
+const storeInitialState = { ...defaultState };
+
 
 export default (history, initialState = storeInitialState) => {
 
   const router = routerMiddleware(history);
   const saga = createSagaMiddleware();
-  const transitions = handleTransitions(history);
 
-  const store = createStore(rootReducer, initialState, compose(
-    applyMiddleware(router, saga),
-    transitions
+  const store = offline(offlineConfig)(createStore)(rootReducer, initialState, composeEnhancers(
+    applyMiddleware(router, saga)
   ));
 
+  apiClient.setStore(store);
+
   const context = {
-      client: new APIClient(config.apiRootUrl, store)
+    client: apiClient,
+    history
   }
 
   saga.run(rootSaga, context);
 
-  store.dispatch(loadSessions());
-
   return store;
 };