client/src/store/configureStore.js
author Alexandre Segura <mex.zktk@gmail.com>
Mon, 26 Jun 2017 16:48:52 +0200
changeset 91 143ff08ec2cc
parent 90 990f2c928b15
child 100 6fd752d98933
permissions -rw-r--r--
DRY
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import rootReducer from '../reducers';
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
     2
import rootAuthSaga from '../sagas/authSaga';
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
     3
import networkSaga from '../sagas/networkSaga';
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
     4
import { compose, createStore, applyMiddleware } from 'redux';
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
     5
import { routerMiddleware } from 'react-router-redux';
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
     6
import createSagaMiddleware from 'redux-saga'
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
import Immutable from 'immutable';
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
     8
import { offline } from 'redux-offline';
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
     9
import offlineDefaultConfig from 'redux-offline/lib/defaults';
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    10
import localForage from 'localforage';
67
9206af01f5e5 Change to UserRecord
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    11
import immutableTransform from 'redux-persist-transform-immutable';
9206af01f5e5 Change to UserRecord
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    12
import NoteRecord from './noteRecord';
9206af01f5e5 Change to UserRecord
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    13
import SessionRecord from './sessionRecord';
9206af01f5e5 Change to UserRecord
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    14
import UserRecord from './userRecord';
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    15
import APIClient from '../api/APIClient';
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    16
import createEffect from '../api';
55
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    17
import config from '../config';
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    18
import { offlineConfigInitialized } from '../actions/networkActions';
91
Alexandre Segura <mex.zktk@gmail.com>
parents: 90
diff changeset
    19
import asyncRequest from '../constants/asyncRequest';
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    21
// const composeEnhancers = (process.env.NODE_ENV !== 'production' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ?
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    22
//     window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    23
//         shouldHotReload: false,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    24
//     }) : compose;
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    25
const composeEnhancers = compose;
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    26
47
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    27
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    28
const defaultState = {
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    29
  sessions: Immutable.List([]),
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    30
  notes: Immutable.List([]),
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    31
  isAuthenticated: false,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    32
  currentUser: null,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    33
  token: '',
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 68
diff changeset
    34
  autoSubmit: false,
91
Alexandre Segura <mex.zktk@gmail.com>
parents: 90
diff changeset
    35
  login: asyncRequest,
Alexandre Segura <mex.zktk@gmail.com>
parents: 90
diff changeset
    36
  register: asyncRequest,
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
};
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    38
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    39
const immutableTransformConfig = {
67
9206af01f5e5 Change to UserRecord
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    40
  records: [NoteRecord, SessionRecord, UserRecord],
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    41
  whitelist: ['sessions', 'notes', 'currentUser']
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    42
}
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    43
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    44
const persistOptions = {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    45
  storage: localForage,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    46
  transforms: [immutableTransform(immutableTransformConfig)],
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 68
diff changeset
    47
  whitelist: ['sessions', 'notes', 'isAuthenticated', 'currentUser', 'token', 'offline', 'autoSubmit']
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    48
}
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    49
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    50
const apiClient = new APIClient(config.apiRootUrl);
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    51
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    52
const offlineConfig = {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    53
  ...offlineDefaultConfig,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    54
  persistOptions,
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 72
diff changeset
    55
  effect: createEffect(apiClient),
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    56
}
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    57
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    58
const storeInitialState = { ...defaultState };
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    59
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    60
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    61
export default (history, initialState = storeInitialState) => {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    62
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    63
  const router = routerMiddleware(history);
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    64
  const saga = createSagaMiddleware();
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    65
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    66
  offlineConfig.detectNetwork = callback => { saga.run(networkSaga, { callback }); };
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    67
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    68
  const store = offline(offlineConfig)(createStore)(rootReducer, initialState, composeEnhancers(
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    69
    applyMiddleware(router, saga)
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 29
diff changeset
    70
  ));
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    71
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    72
  apiClient.setStore(store);
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    73
55
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    74
  const context = {
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    75
    client: apiClient,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    76
    history
55
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    77
  }
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    78
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    79
  saga.run(rootAuthSaga, context);
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    80
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    81
  store.dispatch(offlineConfigInitialized({ client: apiClient }))
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    82
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    83
  return store;
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    84
};