react-blips
Version:
Official React bindings for Blips
132 lines (95 loc) • 5.81 kB
JavaScript
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; };
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, { Component } from 'react';
import PropTypes from 'prop-types';
import { parse } from 'graphql';
import hoistNonReactStatics from 'hoist-non-react-statics';
import invariant from 'invariant';
import { shallowEqual, getPropNameOr, operationsMap, mapObject, mergeOperations, isEmpty, isNil } from '../utils';
export function createWithOperationsHoc(sources, config) {
var mutationsKey = getPropNameOr('mutations')(config);
var queriesKey = getPropNameOr('queries')(config);
var computeOptions = function computeOptions(props) {
return config.options && typeof config.options === 'function' ? config.options(props) : config.options || {};
};
return function withOperations(BaseComponent) {
invariant(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(parse);
var operations = documents.map(operationsMap).reduce(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({}, mapObject(_this.query)(queries), mapObject(_this.fetch)(fetches)), _this$operations[mutationsKey] = 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 (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 (isEmpty(value) || isNil(value)) return acc;
return _extends({}, acc, (_extends2 = {}, _extends2[key] = _extends({}, acc[key] || {}, value), _extends2));
}, this.props);
return React.createElement(BaseComponent, props);
};
return WithOperations;
}(Component);
WithOperations.displayName = 'withOperations(' + (BaseComponent.displayName || BaseComponent.name || 'Component') + ')';
WithOperations.WrappedComponent = BaseComponent;
WithOperations.contextTypes = {
client: PropTypes.object.isRequired
};
return hoistNonReactStatics(WithOperations, BaseComponent, {});
};
}