UNPKG

redux-boost

Version:
154 lines (127 loc) 4.68 kB
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'; export var RestQueryComponent = function (_Component) { _inherits(RestQueryComponent, _Component); function RestQueryComponent() { var _ref, _this2 = this; var _temp, _this, _ret; _classCallCheck(this, RestQueryComponent); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = RestQueryComponent.__proto__ || Object.getPrototypeOf(RestQueryComponent)).call.apply(_ref, [this].concat(args))), _this), _this.hasMounted = false, _this.state = { entities: undefined }, _this.getQueryResult = function () { return _extends({ fetchData: _this.fetchData }, _this.state, _this.props.requestState); }, _this.updateState = function (data) { if (_this.hasMounted) { _this.setState(data); } }, _this.fetchData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var _this$props, query, fetchStart, _this$props$action, action, result; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!_this.props.shouldSkip) { _context.next = 2; break; } return _context.abrupt('return', null); case 2: _this$props = _this.props, query = _this$props.query, fetchStart = _this$props.fetchStart, _this$props$action = _this$props.action, action = _this$props$action === undefined ? fetchStart : _this$props$action; _context.prev = 3; _context.next = 6; return new Promise(function (resolve, reject) { var requestAction = action(query, { 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 6: result = _context.sent; _this.updateState({ entities: result.entities }); return _context.abrupt('return', result); case 11: _context.prev = 11; _context.t0 = _context['catch'](3); throw _context.t0; case 14: case 'end': return _context.stop(); } } }, _callee, _this2, [[3, 11]]); })), _temp), _possibleConstructorReturn(_this, _ret); } _createClass(RestQueryComponent, [{ key: 'componentDidMount', value: function componentDidMount() { this.fetchData(); this.hasMounted = true; } }, { key: 'componentDidUpdate', value: function componentDidUpdate(_ref3) { var _this3 = this; var query = _ref3.query; var queryUpdated = Object.keys(query).some(function (key) { return query[key] !== _this3.props.query[key]; }); if (queryUpdated) { this.fetchData(); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.hasMounted = false; } }, { key: 'render', value: function render() { var children = this.props.children; var queryResult = this.getQueryResult(); return children(queryResult); } }], [{ key: 'getDerivedStateFromProps', value: function getDerivedStateFromProps(_ref4, _ref5) { var requestState = _ref4.requestState; var entities = _ref5.entities; if (requestState && requestState.result) { return { entities: entities }; } return { entities: undefined }; } // eslint-disable-next-line react/sort-comp }]); return RestQueryComponent; }(Component); RestQueryComponent.defaultProps = { query: {}, options: {} };