client/src/store/configureStore.js
author ymh <ymh.work@gmail.com>
Mon, 19 Jun 2017 21:37:33 +0200
changeset 58 f16a080e0bc4
parent 56 96543c395baa
child 59 1eb52770eefa
permissions -rw-r--r--
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint

import rootReducer from '../reducers';
import rootSaga from '../sagas';
import { loadSessions } from '../actions/sessionsActions';
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 { 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 APIClient from '../APIClient';
import config from '../config';

const token = localStorage.getItem('token');
const currentUser = localStorage.getItem('currentUser');

const defaultState = {
//  currentSession: null,
  sessions: Immutable.List([]),
  notes: Immutable.List([]),
  isAuthenticated: token !== null,
  currentUser: currentUser ? JSON.parse(currentUser) : null,
  token: token,
  login: {
    loading: false,
    success: false,
    error: false,
  }
};

const immutableTransformConfig = {
    records: [NoteRecord],
    whitelist: ['sessions', 'notes']
}

const offlineConfig = {
  ...offlineDefaultConfig,
  persistOptions: {
    storage: localForage,
    transforms: [immutableTransform(immutableTransformConfig)]
  },
  detectNetwork: callback => callback(true),
}

//const storeInitialState = Immutable.Map(defaultState);
//const storeInitialState = new Map(defaultState);
const storeInitialState = defaultState;

export default (history, initialState = storeInitialState) => {

  const router = routerMiddleware(history);
  const saga = createSagaMiddleware();
  const transitions = handleTransitions(history);

  const store = offline(offlineConfig)(createStore)(rootReducer, initialState, compose(
    applyMiddleware(router, saga),
    transitions
  ));

  const context = {
      client: new APIClient(config.apiRootUrl, store)
  }

  saga.run(rootSaga, context);

  //store.dispatch(loadSessions());

  return store;
};