redux-boost
Version:
133 lines (110 loc) • 4.42 kB
JavaScript
import _regeneratorRuntime from 'babel-runtime/regenerator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
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 { Component } from 'react';
import { merge } from 'lodash';
export var RestMutationComponent = function (_Component) {
_inherits(RestMutationComponent, _Component);
function RestMutationComponent() {
var _ref,
_this2 = this;
var _temp, _this, _ret;
_classCallCheck(this, RestMutationComponent);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = RestMutationComponent.__proto__ || Object.getPrototypeOf(RestMutationComponent)).call.apply(_ref, [this].concat(args))), _this), _this.hasMounted = false, _this.state = {
entities: undefined
}, _this.getMutationResult = function () {
return _extends({}, _this.state, _this.props.requestState);
}, _this.updateState = function (data) {
if (_this.hasMounted) {
_this.setState(data);
}
}, _this.runMutation = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _this$props, query, fetchStart, options, _this$props$action, action, dynamicPayload, result;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_this$props = _this.props, query = _this$props.query, fetchStart = _this$props.fetchStart, options = _this$props.options, _this$props$action = _this$props.action, action = _this$props$action === undefined ? fetchStart : _this$props$action;
dynamicPayload = payload.nativeEvent ? undefined : options(payload);
_context.prev = 2;
_context.next = 5;
return new Promise(function (resolve, reject) {
var requestAction = action(merge(query, dynamicPayload), {
resolve: resolve,
reject: reject
});
// Handy to use with thunks
if (requestAction.then) {
requestAction.then(function (resp) {
return resolve(resp);
}).catch(function (err) {
return reject(err);
});
}
});
case 5:
result = _context.sent;
_this.updateState({
entities: result.entities
});
return _context.abrupt('return', result);
case 10:
_context.prev = 10;
_context.t0 = _context['catch'](2);
throw _context.t0;
case 13:
case 'end':
return _context.stop();
}
}
}, _callee, _this2, [[2, 10]]);
})), _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(RestMutationComponent, [{
key: 'componentDidMount',
value: function componentDidMount() {
this.hasMounted = true;
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.hasMounted = false;
}
}, {
key: 'render',
value: function render() {
var children = this.props.children;
return children(this.runMutation, this.getMutationResult());
}
}], [{
key: 'getDerivedStateFromProps',
value: function getDerivedStateFromProps(_ref3, _ref4) {
var requestState = _ref3.requestState;
var entities = _ref4.entities;
if (requestState && requestState.result) {
return {
entities: entities
};
}
return {
entities: undefined
};
}
// eslint-disable-next-line react/sort-comp
}]);
return RestMutationComponent;
}(Component);
RestMutationComponent.defaultProps = {
query: {},
options: function options(v) {
return v;
}
};