UNPKG

redux-boost

Version:
96 lines (77 loc) 3.23 kB
import _defineProperty from 'babel-runtime/helpers/defineProperty'; import _extends from 'babel-runtime/helpers/extends'; import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _createClass from 'babel-runtime/helpers/createClass'; import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn'; import _inherits from 'babel-runtime/helpers/inherits'; import React, { Component } from 'react'; import { RestQuery } from './QueryContainer'; export var 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) { _inherits(RestQueryHoc, _Component); function RestQueryHoc() { var _ref; var _temp, _this, _ret; _classCallCheck(this, RestQueryHoc); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(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), _possibleConstructorReturn(_this, _ret); } _createClass(RestQueryHoc, [{ key: 'render', value: function render() { var _this2 = this; config.query = _extends({ name: name, method: method, payload: payload, serialize: serialize, executor: executor, prepareExecutor: prepareExecutor }, query, this.applyProps(options)); if (skip) { config.shouldSkip = this.applyProps(skip); } return React.createElement( RestQuery, _extends({}, config, this.props), function (result) { var propName = config.name || 'data'; var childProps = _defineProperty({}, propName, result); if (config.props) { var _newResult; var newResult = (_newResult = {}, _defineProperty(_newResult, propName, result), _defineProperty(_newResult, 'ownProps', _this2.props), _newResult); lastResultProps = config.props(newResult, lastResultProps); childProps = lastResultProps; } if (Placeholder && result.loading) { return React.createElement(Placeholder, _this2.props); } return React.createElement(WrappedComponent, _extends({}, _this2.props, childProps)); } ); } }]); return RestQueryHoc; }(Component); return RestQueryHoc; }; };