UNPKG

@exivity/proton

Version:
199 lines (164 loc) 8 kB
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import { withData } from 'react-orbitjs'; import { CrudContext, decorateQuery } from '@exivity/proton'; var _Entities = /*#__PURE__*/ function (_PureComponent) { _inherits(_Entities, _PureComponent); function _Entities(props) { var _this; _classCallCheck(this, _Entities); _this = _possibleConstructorReturn(this, _getPrototypeOf(_Entities).call(this, props)); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "findOne", function (id) { var _entities = _this.props._entities; return _entities.find(function (item) { return item.id === id; }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "find", function (ids) { var _entities = _this.props._entities; return ids.map(function (id) { return _entities.find(function (item) { return item.id === id; }); }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "findByAttribute", function (_ref) { var attribute = _ref.attribute, value = _ref.value; var _entities = _this.props._entities; return _entities.filter(function (item) { return item.attributes[attribute] === value; }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "buildSaveTransforms", function (records) { return function (t) { return records.map(function (record) { if (record.id) { return t.replaceRecord(record); } return t.addRecord(record); }); }; }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "buildRemoveTransforms", function (records) { return function (t) { return records.map(function (record) { return t.removeRecord(record); }); }; }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "queryStore", /*#__PURE__*/ _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee() { return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.prev = 0; _context.next = 3; return _this.props.queryStore(function (q) { return q.findRecords('_entity'); }); case 3: _this.setState({ loading: false }); _context.next = 9; break; case 6: _context.prev = 6; _context.t0 = _context["catch"](0); _this.setState({ loading: false }); case 9: case "end": return _context.stop(); } } }, _callee, this, [[0, 6]]); }))); _this.state = { loading: false }; return _this; } _createClass(_Entities, [{ key: "componentDidMount", value: function componentDidMount() { var _entities = this.props._entities; if (!_entities.length) { this.setState({ loading: true }, this.queryStore); } } }, { key: "render", value: function render() { var _this2 = this; var _entities = this.props._entities; var loading = this.state.loading; return React.createElement(CrudContext.Consumer, null, function (_ref3) { var performTransforms = _ref3.performTransforms; return _this2.props.children({ _entities: { findOne: _this2.findOne, find: _this2.find, findByAttribute: _this2.findByAttribute, all: function all() { return _entities; } }, loading: loading, save: function save(records) { return performTransforms(_this2.buildSaveTransforms(records)); }, remove: function remove(records) { return performTransforms(_this2.buildRemoveTransforms(records)); } }); }); } }]); return _Entities; }(PureComponent); var mapRecordsToProps = function mapRecordsToProps(_ref4) { var sort = _ref4.sort, filter = _ref4.filter, page = _ref4.page; return { _entities: decorateQuery(function (q) { return q.findRecords('_entity'); }, { sort: sort, filter: filter, page: page }) }; }; export default withData(mapRecordsToProps)(_Entities); _Entities.propTypes = { _entities: PropTypes.array, queryStore: PropTypes.func, sort: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), filter: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), page: PropTypes.oneOfType([PropTypes.number, PropTypes.object]) }; //# sourceMappingURL=entities.js.map