react-redux-quest
Version:
API and generic utilities for react and redux eco-system
81 lines (58 loc) • 2.76 kB
JavaScript
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import React from 'react';
import PropTypes from 'prop-types';
import { apiRequest } from '../middleware/reduxQuest';
var string = PropTypes.string,
func = PropTypes.func;
var Read = function (_React$PureComponent) {
_inherits(Read, _React$PureComponent);
function Read() {
_classCallCheck(this, Read);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var _this = _possibleConstructorReturn(this, _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)));
_this.state = {
response: null,
error: null
};
_this.updateSuccess = function (response) {
return _this.setState({ response: response });
};
_this.updateError = function (error) {
return _this.setState({ error: error });
};
var url = _this.props.url;
apiRequest(url, { method: 'GET' }).then(function (response) {
return _this.updateSuccess(response);
}, function (error) {
return _this.updateError(error);
});
return _this;
}
Read.prototype.render = function render() {
var _props = this.props,
render = _props.render,
children = _props.children;
var _state = this.state,
response = _state.response,
error = _state.error;
var preferredRender = render || children;
if (!(response || error)) {
return null;
}
if (!preferredRender) {
return null;
}
return preferredRender(response, error);
};
return Read;
}(React.PureComponent);
Read.propTypes = {
url: string.isRequired,
render: func,
children: func
};
export default Read;