UNPKG

lore-hook-connect

Version:

A lore hook that generates dialogs using Bootstrap

166 lines (127 loc) 5.47 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); var _loreUtils = require('lore-utils'); var _InvalidReducerKeyError = require('./errors/InvalidReducerKeyError'); var _InvalidReducerKeyError2 = _interopRequireDefault(_InvalidReducerKeyError); var _InvalidActionKeyError = require('./errors/InvalidActionKeyError'); var _InvalidActionKeyError2 = _interopRequireDefault(_InvalidActionKeyError); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Connection class object. The thing all reducer-action maps are eventually instantiated as. * * @param {Object} definition : describes the connections methods and behavior * @param {Object} options : parameters to individualize the behavior of this connection * @constructor */ var Connection = function () { function Connection(definition, options) { _classCallCheck(this, Connection); /** * Default parameters. User params will be merged with these to * create the full set. */ this.defaults = {}; // apply definition overrides to class _lodash2.default.merge(this, definition || {}); _lodash2.default.bindAll(this); /** * String declaring the action to invoke if there is no data in the * reducer state */ this.actionKey = options.action || ''; /** * String declaring the reducer state that should be retrieved */ this.reducerKey = options.reducer || ''; this.actions = options.actions; } /** * Verify whether the parameters provided to the getState call are valid * @param {Object} params */ _createClass(Connection, [{ key: 'verifyParams', value: function verifyParams(params) {} // no op /** * Get the action that should be invoked if the data does not exist * @param {Array} actions : array of all registered action creators * @returns {Function} action to invoke */ }, { key: 'getAction', value: function getAction(actions) { var key = this.actionKey; var action = _lodash2.default.get(actions, key); if (!action) { throw (0, _InvalidActionKeyError2.default)(key); } return action; } /** * Get the subset of state associated with the reducer we're interested in * @param {Object} storeState : the state of the Redux Store * @returns {Object} the piece of state owned by the reducer */ }, { key: 'getReducerState', value: function getReducerState(storeState) { var key = this.reducerKey; var reducerState = _lodash2.default.get(storeState, key); if (!reducerState) { throw (0, _InvalidReducerKeyError2.default)(key); } return reducerState; } /** * Extract the data we care about from the reducer's state * @param {Object} reducerState : subset of state owned by the reducer we care about * @param {Object} params : parameters provided to the getState call */ }, { key: 'getPayload', value: function getPayload(reducerState, params, reducer) {} // no op /** * Call the action responsible for retrieving the data we want * @param action : the action that can retrieve the data we want * @param params : parameters provided to the getState call */ }, { key: 'callAction', value: function callAction(action, params) {} // no op /** * This is the main function for connections, and orchestrates the logic to * validate paramters, extract data from the Redux store, and call the action * to retrieve the data if it doesn't exist. * * @param {Object} state : the state of the Redux Store * @param {Object} userParams : parameters passed to the getState function * @returns {Object} payload sent to the Redux Store */ }, { key: 'getState', value: function getState(state, userParams) { var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var params = _lodash2.default.defaultsDeep({}, userParams, this.defaults); this.verifyParams(params); var action = this.getAction(this.actions); var reducerState = this.getReducerState(state); var payload = this.getPayload(reducerState, params, _lodash2.default.get(state, this.reducerKey.split('.')[0])); if (!payload || payload.state === _loreUtils.PayloadStates.INITIAL_STATE || options.force) { payload = this.callAction(action, params) || payload; } return payload; } }]); return Connection; }(); exports.default = Connection; module.exports = exports['default'];