TableData.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _react = _interopRequireDefault(require("react"));
  7. var _modularityFront = _interopRequireDefault(require("modularity-front"));
  8. var _reactMaterialize = require("react-materialize");
  9. var _index = _interopRequireDefault(require("../Modules/index.js"));
  10. var _FilterModule = _interopRequireDefault(require("../Modules/FilterModule.js"));
  11. require("./TableData.css");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. 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); }
  14. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  15. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  16. function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  17. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  18. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  19. 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); } }
  20. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  21. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  22. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  23. function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }
  24. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  25. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  26. 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); }
  27. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  28. var Table = _index.default.Table,
  29. Pagination = _index.default.Pagination;
  30. var Module = _modularityFront.default.Module;
  31. var pTimeout = function pTimeout(time, item) {
  32. return new Promise(function (res, rej) {
  33. setTimeout(function () {
  34. return res(item);
  35. }, time);
  36. });
  37. };
  38. var TableData =
  39. /*#__PURE__*/
  40. function (_Modularity$Module) {
  41. _inherits(TableData, _Modularity$Module);
  42. function TableData(props) {
  43. var _this;
  44. _classCallCheck(this, TableData);
  45. _this = _possibleConstructorReturn(this, _getPrototypeOf(TableData).call(this, props));
  46. _this.state = {
  47. page: 1,
  48. data: [],
  49. totalPages: 1
  50. };
  51. _this.service.onUpdate(function () {
  52. return _this.serviceUpdate(_this.state.page);
  53. });
  54. _this.serviceUpdate(_this.state.page);
  55. return _this;
  56. }
  57. _createClass(TableData, [{
  58. key: "serviceUpdate",
  59. value: function serviceUpdate() {
  60. var _this2 = this;
  61. var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
  62. var Service = this.service.value;
  63. if (!Service.count) return;
  64. var perPage = this.perPage.value;
  65. return Service.count().then(function (_ref) {
  66. var _ref2 = _slicedToArray(_ref, 1),
  67. counter = _ref2[0].counter;
  68. var totalPages = parseInt(counter / perPage + 0.999);
  69. page = page <= totalPages ? page : totalPages;
  70. _this2.setState({
  71. totalPages: totalPages,
  72. page: page
  73. });
  74. return Service.getPage(page, perPage);
  75. }).then(function (data) {
  76. _this2.setState({
  77. data: data
  78. });
  79. }).catch(function (e) {
  80. return console.log(e);
  81. });
  82. }
  83. }, {
  84. key: "pageChanged",
  85. value: function pageChanged(v) {
  86. this.serviceUpdate(v);
  87. }
  88. }, {
  89. key: "render",
  90. value: function render() {
  91. var _this3 = this;
  92. var page = this.state.page,
  93. totalPages = this.state.totalPages,
  94. data = this.state.data;
  95. var TableView = _react.default.createElement(_reactMaterialize.Row, {
  96. style: {
  97. display: 'flex'
  98. },
  99. key: 'Table' + this.getId()
  100. }, _react.default.createElement(_reactMaterialize.Col, {
  101. s: 12
  102. }, _react.default.createElement(Table, {
  103. TableHeaders: this.TableHeaders.value,
  104. TableData: data,
  105. TableClass: this.TableClass.value,
  106. onView: this.props.onView
  107. }), _react.default.createElement(Pagination, {
  108. selected: page,
  109. lastPage: totalPages,
  110. onPage: function onPage(v) {
  111. return _this3.pageChanged(v);
  112. }
  113. })));
  114. return [TableView, _get(_getPrototypeOf(TableData.prototype), "render", this).call(this)];
  115. }
  116. }]);
  117. return TableData;
  118. }(_modularityFront.default.Module);
  119. TableData.Inputs = {
  120. service: {
  121. type: Module.Types.Object,
  122. defaultValue: {}
  123. },
  124. perPage: {
  125. type: Module.Types.Number,
  126. defaultValue: 10
  127. },
  128. TableHeaders: {
  129. type: Module.Types.Object,
  130. defaultValue: {
  131. "Id": "id",
  132. "Name": "name",
  133. "Value": "price"
  134. }
  135. },
  136. TableClass: {
  137. type: Module.Types.Text,
  138. defaultValue: "striped centered"
  139. }
  140. };
  141. TableData.Outputs = {};
  142. TableData.Category = "Higher Modules";
  143. TableData.EditPosition = "top-right";
  144. var _default = TableData;
  145. exports.default = _default;