UNPKG

redux-boost

Version:
90 lines (72 loc) 3.18 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 { RestMutation } from './MutationContainer'; export var restMutation = function restMutation(config) { return function (WrappedComponent) { var lastResultProps = void 0; var name = config.name, _config$method = config.method, method = _config$method === undefined ? 'post' : _config$method, serialize = config.serialize, executor = config.executor, prepareExecutor = config.prepareExecutor, query = config.query, payload = config.payload, options = config.options, applyProps = config.applyProps; var RestMutationHoc = function (_Component) { _inherits(RestMutationHoc, _Component); function RestMutationHoc() { var _ref; var _temp, _this, _ret; _classCallCheck(this, RestMutationHoc); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = RestMutationHoc.__proto__ || Object.getPrototypeOf(RestMutationHoc)).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(RestMutationHoc, [{ 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(applyProps)); return React.createElement( RestMutation, _extends({ options: options }, config, this.props), function (mutate, r) { var result = _extends({ mutate: mutate }, r); 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; } return React.createElement(WrappedComponent, _extends({}, _this2.props, childProps)); } ); } }]); return RestMutationHoc; }(Component); return RestMutationHoc; }; };