@exivity/proton
Version:
Exivity React SDK
231 lines (185 loc) • 9.06 kB
JavaScript
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';
var Crud =
/*#__PURE__*/
function (_PureComponent) {
_inherits(Crud, _PureComponent);
function Crud() {
var _getPrototypeOf2;
var _this;
_classCallCheck(this, Crud);
for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) {
_args[_key] = arguments[_key];
}
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Crud)).call.apply(_getPrototypeOf2, [this].concat(_args)));
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "build", function () {
var buildRecord = _this.props.buildRecord;
return buildRecord.apply(void 0, arguments);
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "add",
/*#__PURE__*/
_asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee() {
var _this$props,
beforeAdd,
addRecord,
onAdd,
proceed,
_args2 = arguments;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_this$props = _this.props, beforeAdd = _this$props.beforeAdd, addRecord = _this$props.addRecord, onAdd = _this$props.onAdd;
_context.next = 3;
return beforeAdd();
case 3:
proceed = _context.sent;
if (proceed) addRecord.apply(void 0, _args2).then(onAdd);
case 5:
case "end":
return _context.stop();
}
}
}, _callee, this);
})));
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "update",
/*#__PURE__*/
_asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee2() {
var _this$props2,
beforeUpdate,
updateRecord,
onUpdate,
proceed,
_args3 = arguments;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_this$props2 = _this.props, beforeUpdate = _this$props2.beforeUpdate, updateRecord = _this$props2.updateRecord, onUpdate = _this$props2.onUpdate;
_context2.next = 3;
return beforeUpdate();
case 3:
proceed = _context2.sent;
if (proceed) updateRecord.apply(void 0, _args3).then(onUpdate);
case 5:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
})));
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "remove",
/*#__PURE__*/
_asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee3() {
var _len2,
args,
_key2,
_this$props3,
beforeRemove,
removeRecord,
onRemove,
proceed,
_args4 = arguments;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
for (_len2 = _args4.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = _args4[_key2];
}
_this$props3 = _this.props, beforeRemove = _this$props3.beforeRemove, removeRecord = _this$props3.removeRecord, onRemove = _this$props3.onRemove;
_context3.next = 4;
return beforeRemove();
case 4:
proceed = _context3.sent;
if (proceed) removeRecord.apply(void 0, args).then(function () {
return onRemove.apply(void 0, args);
});
case 6:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
})));
return _this;
}
_createClass(Crud, [{
key: "render",
value: function render() {
var handlers = {
build: this.build,
add: this.add,
update: this.update,
remove: this.remove
};
return this.props.children(handlers);
}
}]);
return Crud;
}(PureComponent);
Crud.propTypes = {
/** Callback with build, add, update, remove promises. */
children: PropTypes.func.isRequired,
/** Function to build objectType. */
buildRecord: PropTypes.func,
/** Function to add record. */
addRecord: PropTypes.func,
/** Function to update record. */
updateRecord: PropTypes.func,
/** Function to remove record. */
removeRecord: PropTypes.func,
/** Callback called when add() resolves. Provides added record. */
onAdd: PropTypes.func,
/** Callback called when update() resolves. Provides updated record. */
onUpdate: PropTypes.func,
/** Callback called when remove() resolves. Provides removed record. */
onRemove: PropTypes.func,
/** Callback called before add(). Takes a promise or function.
Return truthy value to proceed with add() */
beforeAdd: PropTypes.func,
/** Callback called before update(). Takes a promise or function.
Return truthy value to proceed with update() */
beforeUpdate: PropTypes.func,
/** Callback called before remove(). Takes a promise or function.
Return truthy value to proceed with remove() */
beforeRemove: PropTypes.func
};
Crud.defaultProps = {
buildRecord: function buildRecord() {},
addRecord: function addRecord() {},
updateRecord: function updateRecord() {},
removeRecord: function removeRecord() {},
onAdd: function onAdd() {},
onUpdate: function onUpdate() {},
onRemove: function onRemove() {},
beforeAdd: function beforeAdd() {
return true;
},
beforeUpdate: function beforeUpdate() {
return true;
},
beforeRemove: function beforeRemove() {
return true;
}
};
export default Crud;
//# sourceMappingURL=Crud.js.map