UNPKG

react-blips

Version:
151 lines (103 loc) 6.34 kB
'use strict'; exports.__esModule = true; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; exports.createWithOperationsHoc = createWithOperationsHoc; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _graphql = require('graphql'); var _hoistNonReactStatics = require('hoist-non-react-statics'); var _hoistNonReactStatics2 = _interopRequireDefault(_hoistNonReactStatics); var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _utils = require('../utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } function createWithOperationsHoc(sources, config) { var mutationsKey = (0, _utils.getPropNameOr)('mutations')(config); var queriesKey = (0, _utils.getPropNameOr)('queries')(config); var computeOptions = function computeOptions(props) { return config.options && typeof config.options === 'function' ? config.options(props) : config.options || {}; }; return function withOperations(BaseComponent) { (0, _invariant2.default)(typeof BaseComponent === 'function', 'You must pass a component to the function returned by blips. Instead received ' + JSON.stringify(BaseComponent)); var WithOperations = function (_Component) { _inherits(WithOperations, _Component); function WithOperations(props, context) { _classCallCheck(this, WithOperations); var _this = _possibleConstructorReturn(this, _Component.call(this, props, context)); _this.parse = function (sources) { var documents = sources.map(_graphql.parse); var operations = documents.map(_utils.operationsMap).reduce(_utils.mergeOperations, {}); return { sources: sources, documents: documents, operations: operations }; }; _this.resolve = function () { var _this$operations; var _this$parsedData$oper = _this.parsedData.operations, _this$parsedData$oper2 = _this$parsedData$oper.query, queries = _this$parsedData$oper2 === undefined ? {} : _this$parsedData$oper2, _this$parsedData$oper3 = _this$parsedData$oper.mutation, mutations = _this$parsedData$oper3 === undefined ? {} : _this$parsedData$oper3, _this$parsedData$oper4 = _this$parsedData$oper.fetch, fetches = _this$parsedData$oper4 === undefined ? {} : _this$parsedData$oper4; _this.operations = (_this$operations = {}, _this$operations[queriesKey] = _extends({}, (0, _utils.mapObject)(_this.query)(queries), (0, _utils.mapObject)(_this.fetch)(fetches)), _this$operations[mutationsKey] = (0, _utils.mapObject)(_this.mutate)(mutations), _this$operations); }; _this.query = function (document) { return function () { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.options; return _this.client.query(document, options); }; }; _this.mutate = function (document) { return function () { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.options; return _this.client.mutate(document, options); }; }; _this.fetch = function (document) { return function () { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.options; return _this.client.fetch(document, options); }; }; _this.client = context.client; _this.options = computeOptions(props); _this.parsedData = _this.parse(sources); return _this; } WithOperations.prototype.componentWillMount = function componentWillMount() { this.resolve(); }; WithOperations.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { var nextOptions = computeOptions(nextProps); if ((0, _utils.shallowEqual)(this.options, nextOptions)) return; this.options = _extends({}, nextOptions); this.resolve(); }; WithOperations.prototype.render = function render() { var props = Object.entries(this.operations).reduce(function (acc, _ref) { var _extends2; var key = _ref[0], value = _ref[1]; if ((0, _utils.isEmpty)(value) || (0, _utils.isNil)(value)) return acc; return _extends({}, acc, (_extends2 = {}, _extends2[key] = _extends({}, acc[key] || {}, value), _extends2)); }, this.props); return _react2.default.createElement(BaseComponent, props); }; return WithOperations; }(_react.Component); WithOperations.displayName = 'withOperations(' + (BaseComponent.displayName || BaseComponent.name || 'Component') + ')'; WithOperations.WrappedComponent = BaseComponent; WithOperations.contextTypes = { client: _propTypes2.default.object.isRequired }; return (0, _hoistNonReactStatics2.default)(WithOperations, BaseComponent, {}); }; }