All files / stores store.js

85.42% Statements 41/48
65% Branches 13/20
60% Functions 3/5
85.11% Lines 40/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 1101x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x         1x   1x 12x           14x 14x     14x 13x 13x         13x 1x           13x                         13x             14x 13x 13x     14x 12x                       14x 1x 1x 1x           1x   1x     1x     1x     14x     1x  
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import reducer from 'modules';
import toNS from 'mongodb-ns';
import { namespaceChanged } from 'modules/namespace';
import { dataServiceConnected } from 'modules/data-service';
import { fieldsChanged } from 'modules/fields';
import { serverVersionChanged } from 'modules/server-version';
import { fetchValidation, activateValidation } from 'modules/validation';
import { editModeChanged } from 'modules/edit-mode';
import { changeZeroState } from 'modules/zero-state';
import {
  localAppRegistryActivated,
  globalAppRegistryActivated
} from 'mongodb-redux-common/app-registry';
import semver from 'semver';
 
/**
 * The lowest supported version.
 */
const MIN_VERSION = '3.2.0';
 
export const setDataProvider = (store, error, dataProvider) => {
  store.dispatch(dataServiceConnected(error, dataProvider));
};
 
/**
 * The store has a combined pipeline reducer plus the thunk middleware.
 */
const configureStore = (options = {}) => {
  const store = createStore(reducer, applyMiddleware(thunk));
 
  // Set the app registry if preset. This must happen first.
  if (options.localAppRegistry) {
    const localAppRegistry = options.localAppRegistry;
    store.dispatch(localAppRegistryActivated(localAppRegistry));
 
    /**
     * When the collection is changed, update the store.
     */
    localAppRegistry.on('fields-changed', (fields) => {
      store.dispatch(fieldsChanged(fields.fields));
    });
 
    /**
     * Refresh on query change.
     */
    localAppRegistry.on('server-version-changed', (version) => {
      store.dispatch(serverVersionChanged(version));
      if (version) {
        const editMode = { oldServerReadOnly: semver.gte(MIN_VERSION, version) };
        store.dispatch(editModeChanged(editMode));
      }
    });
 
    /**
     * When the Schema Validation is an active tab, send 'activated' metric.
     *
     * @param {String} tabName - The name of active tab.
     */
    localAppRegistry.on('subtab-changed', (tabName) => {
      if (tabName === 'Validation') {
        store.dispatch(activateValidation());
      }
    });
  }
 
  if (options.globalAppRegistry) {
    const globalAppRegistry = options.globalAppRegistry;
    store.dispatch(globalAppRegistryActivated(globalAppRegistry));
  }
 
  if (options.dataProvider) {
    setDataProvider(
      store,
      options.dataProvider.error,
      options.dataProvider.dataProvider
    );
  }
 
  /**
   * When the collection is changed, update the store.
   *
   * @param {String} ns - The full namespace.
   */
  if (options.namespace) {
    const namespace = toNS(options.namespace);
    const WriteStateStore = options.globalAppRegistry.getStore('DeploymentAwareness.WriteStateStore');
    const editMode = {
      collectionReadOnly: options.isReadonly ? true : false,
      hardonReadOnly: (process.env.HADRON_READONLY === 'true'),
      writeStateStoreReadOnly: !WriteStateStore.state.isWritable
    };
 
    store.dispatch(namespaceChanged(namespace));
 
    Iif (editMode.collectionReadOnly) {
      store.dispatch(changeZeroState(true));
    } else {
      store.dispatch(fetchValidation(namespace));
    }
 
    store.dispatch(editModeChanged(editMode));
  }
 
  return store;
};
 
export default configureStore;