client/src/store/configureStore.js
author ymh <ymh.work@gmail.com>
Tue, 01 Aug 2017 12:20:14 +0200
changeset 132 906a6c7c7943
parent 129 d48946d164c6
child 134 be36eed5e6e0
permissions -rw-r--r--
add group to sync + create groups + various component cleaning
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';
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
     4
import rootSyncSaga from '../sagas/syncSaga';
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
     5
import networkSaga from '../sagas/networkSaga';
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
     6
import { compose, createStore, applyMiddleware } from 'redux';
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
     7
import { routerMiddleware } from 'react-router-redux';
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
     8
import createSagaMiddleware from 'redux-saga'
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import Immutable from 'immutable';
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    10
import {persistStore, autoRehydrate} from 'redux-persist-immutable'
62
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';
132
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    16
import GroupRecord from './groupRecord';
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    17
import APIClient from '../api/APIClient';
55
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    18
import config from '../config';
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
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    21
const composeEnhancers = (process.env.NODE_ENV !== 'production' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ?
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    22
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    23
        shouldHotReload: false,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    24
    }) : compose;
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    25
47
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    26
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    27
const defaultState = {
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    28
  sessions: Immutable.List([]),
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    29
  notes: Immutable.List([]),
100
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    30
  groups: Immutable.List([]),
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    31
  status: Immutable.Map({
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    32
    isSynchronizing: false,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    33
    online: false
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    34
  }),
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    35
  authStatus: Immutable.Map({
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    36
    token: '',
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    37
    isAuthenticated: false,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    38
    clientId: null,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    39
    lastSync: 0,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    40
    currentUser: null,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    41
  }),
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 68
diff changeset
    42
  autoSubmit: false,
91
Alexandre Segura <mex.zktk@gmail.com>
parents: 90
diff changeset
    43
  login: asyncRequest,
Alexandre Segura <mex.zktk@gmail.com>
parents: 90
diff changeset
    44
  register: asyncRequest,
100
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    45
  createGroup: asyncRequest
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
};
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    47
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    48
const immutableTransformConfig = {
132
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    49
  records: [NoteRecord, SessionRecord, UserRecord, GroupRecord],
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    50
  whitelist: ['sessions', 'notes', 'authStatus', 'groups']
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 persistOptions = {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    54
  storage: localForage,
132
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    55
  records: [NoteRecord, SessionRecord, UserRecord, GroupRecord],
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    56
  transforms: [immutableTransform(immutableTransformConfig)],
132
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    57
  whitelist: ['sessions', 'notes', 'autoSubmit', 'authStatus', 'groups']
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    58
}
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
const apiClient = new APIClient(config.apiRootUrl);
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    61
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    62
const storeInitialState = Immutable.Map({ ...defaultState });
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    63
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    64
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    65
export default (history, initialState = storeInitialState) => {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    66
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    67
  const router = routerMiddleware(history);
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    68
  const saga = createSagaMiddleware();
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    69
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    70
  const store = createStore(rootReducer, initialState, composeEnhancers(
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    71
    applyMiddleware(router, saga),
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    72
    autoRehydrate()
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
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    82
  persistStore(store, persistOptions);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    83
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    84
  saga.run(rootAuthSaga, context);
100
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    85
  saga.run(rootGroupSaga, context);
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    86
  saga.run(rootSyncSaga, context);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    87
  saga.run(networkSaga, context);
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    88
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    89
  return store;
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    90
};