redux-boost
Version:
125 lines (90 loc) • 4.12 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.restQuery = undefined;
var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
var _defineProperty3 = _interopRequireDefault(_defineProperty2);
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _QueryContainer = require('./QueryContainer');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var restQuery = exports.restQuery = function restQuery(config) {
return function (WrappedComponent) {
var lastResultProps = void 0;
var name = config.name,
method = config.method,
serialize = config.serialize,
executor = config.executor,
prepareExecutor = config.prepareExecutor,
query = config.query,
payload = config.payload,
options = config.options,
skip = config.skip,
Placeholder = config.placeholder;
var RestQueryHoc = function (_Component) {
(0, _inherits3.default)(RestQueryHoc, _Component);
function RestQueryHoc() {
var _ref;
var _temp, _this, _ret;
(0, _classCallCheck3.default)(this, RestQueryHoc);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = RestQueryHoc.__proto__ || Object.getPrototypeOf(RestQueryHoc)).call.apply(_ref, [this].concat(args))), _this), _this.applyProps = function (fn) {
if (typeof fn === 'function') {
return fn(_this.props);
}
return fn;
}, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
}
(0, _createClass3.default)(RestQueryHoc, [{
key: 'render',
value: function render() {
var _this2 = this;
config.query = (0, _extends3.default)({
name: name,
method: method,
payload: payload,
serialize: serialize,
executor: executor,
prepareExecutor: prepareExecutor
}, query, this.applyProps(options));
if (skip) {
config.shouldSkip = this.applyProps(skip);
}
return _react2.default.createElement(
_QueryContainer.RestQuery,
(0, _extends3.default)({}, config, this.props),
function (result) {
var propName = config.name || 'data';
var childProps = (0, _defineProperty3.default)({}, propName, result);
if (config.props) {
var _newResult;
var newResult = (_newResult = {}, (0, _defineProperty3.default)(_newResult, propName, result), (0, _defineProperty3.default)(_newResult, 'ownProps', _this2.props), _newResult);
lastResultProps = config.props(newResult, lastResultProps);
childProps = lastResultProps;
}
if (Placeholder && result.loading) {
return _react2.default.createElement(Placeholder, _this2.props);
}
return _react2.default.createElement(WrappedComponent, (0, _extends3.default)({}, _this2.props, childProps));
}
);
}
}]);
return RestQueryHoc;
}(_react.Component);
return RestQueryHoc;
};
};
;