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