client/src/store/configureStore.js
author Alexandre Segura <mex.zktk@gmail.com>
Mon, 26 Jun 2017 16:43:22 +0200
changeset 90 990f2c928b15
parent 89 06f609adfbf8
child 91 143ff08ec2cc
permissions -rw-r--r--
Improve login errors.
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';
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    20
// 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
    21
//     window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    22
//         shouldHotReload: false,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    23
//     }) : compose;
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    24
const composeEnhancers = compose;
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([]),
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    30
  isAuthenticated: false,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    31
  currentUser: null,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    32
  token: '',
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 68
diff changeset
    33
  autoSubmit: false,
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    34
  login: Immutable.Map({
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 29
diff changeset
    35
    loading: false,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 29
diff changeset
    36
    success: false,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 29
diff changeset
    37
    error: false,
90
990f2c928b15 Improve login errors.
Alexandre Segura <mex.zktk@gmail.com>
parents: 89
diff changeset
    38
    errorMessages: Immutable.Map({})
89
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    39
  }),
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    40
  register: Immutable.Map({
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    41
    loading: false,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    42
    success: false,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    43
    error: false,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    44
    errorMessages: Immutable.Map({})
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    45
  }),
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 = {
67
9206af01f5e5 Change to UserRecord
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    49
  records: [NoteRecord, SessionRecord, UserRecord],
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    50
  whitelist: ['sessions', 'notes', 'currentUser']
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,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    55
  transforms: [immutableTransform(immutableTransformConfig)],
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 68
diff changeset
    56
  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
    57
}
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
const apiClient = new APIClient(config.apiRootUrl);
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 offlineConfig = {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    62
  ...offlineDefaultConfig,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    63
  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
    64
  effect: createEffect(apiClient),
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    65
}
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    66
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    67
const storeInitialState = { ...defaultState };
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    68
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    69
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    70
export default (history, initialState = storeInitialState) => {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    71
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    72
  const router = routerMiddleware(history);
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    73
  const saga = createSagaMiddleware();
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    74
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    75
  offlineConfig.detectNetwork = callback => { saga.run(networkSaga, { callback }); };
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    76
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    77
  const store = offline(offlineConfig)(createStore)(rootReducer, initialState, composeEnhancers(
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    78
    applyMiddleware(router, saga)
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 29
diff changeset
    79
  ));
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    80
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    81
  apiClient.setStore(store);
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    82
55
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    83
  const context = {
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    84
    client: apiClient,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    85
    history
55
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    86
  }
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    87
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    88
  saga.run(rootAuthSaga, context);
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    89
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    90
  store.dispatch(offlineConfigInitialized({ client: apiClient }))
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 14
diff changeset
    91
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    92
  return store;
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 3
diff changeset
    93
};