client/src/store/configureStore.js
author ymh <ymh.work@gmail.com>
Mon, 08 Oct 2018 18:35:47 +0200
changeset 168 ea92f4fe783d
parent 134 be36eed5e6e0
child 172 4b780ebbedc6
permissions -rw-r--r--
- move SlateEditor and dependencies to its own folder - remove Immutable - remove redux-persist-immutable - remobe redux-immutable - update libraries - added tests on store manipulations (accessor and reducers)
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';
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
     7
import { connectRouter, routerMiddleware } from 'connected-react-router'
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
     8
import createSagaMiddleware from 'redux-saga';
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
     9
import { persistStore, persistReducer } from 'redux-persist';
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    10
import localForage from 'localforage';
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    11
import APIClient from '../api/APIClient';
55
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    12
import config from '../config';
91
Alexandre Segura <mex.zktk@gmail.com>
parents: 90
diff changeset
    13
import asyncRequest from '../constants/asyncRequest';
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    15
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
    16
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    17
        shouldHotReload: false,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    18
    }) : compose;
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    19
47
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    20
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    21
const defaultState = {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    22
  sessions: [],
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    23
  notes: [],
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    24
  groups: [],
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    25
  status: {
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    26
    isSynchronizing: false,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    27
    online: false
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    28
  },
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    29
  authStatus: {
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    30
    token: '',
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    31
    isAuthenticated: false,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    32
    clientId: null,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    33
    lastSync: 0,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    34
    currentUser: null,
134
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    35
    currentGroup: null
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    36
  },
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 68
diff changeset
    37
  autoSubmit: false,
91
Alexandre Segura <mex.zktk@gmail.com>
parents: 90
diff changeset
    38
  login: asyncRequest,
Alexandre Segura <mex.zktk@gmail.com>
parents: 90
diff changeset
    39
  register: asyncRequest,
100
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    40
  createGroup: asyncRequest
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
};
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    42
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    43
const persistOptions = {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    44
  key: 'root',
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    45
  storage: localForage,
132
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    46
  whitelist: ['sessions', 'notes', 'autoSubmit', 'authStatus', 'groups']
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    47
}
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
const apiClient = new APIClient(config.apiRootUrl);
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    50
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    51
export default (history, initialState = defaultState) => {
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    52
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    53
  const persistedReducer = connectRouter(history)(persistReducer(persistOptions, rootReducer));
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    54
  const router = routerMiddleware(history);
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    55
  const saga = createSagaMiddleware();
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    56
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    57
  const store = createStore(persistedReducer, initialState, composeEnhancers(
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    58
    applyMiddleware(router, saga),
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 29
diff changeset
    59
  ));
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    60
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    61
  apiClient.setStore(store);
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    62
55
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    63
  const context = {
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    64
    client: apiClient,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    65
    history
55
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    66
  }
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    67
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    68
  const persistor = persistStore(store, null, () => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    69
    saga.run(rootAuthSaga, context);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    70
    saga.run(rootGroupSaga, context);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    71
    saga.run(rootSyncSaga, context);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    72
    saga.run(networkSaga, context);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    73
  });
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 101
diff changeset
    74
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    75
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    76
  return { store, persistor };
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    77
};