SearchBarPackage.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 _searchbar = _interopRequireDefault(require("../../Modules/searchbar.js"));
  10. var _API = _interopRequireDefault(require("../../Services/API.js"));
  11. require("./SearchBarPackage.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 _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
  15. 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; }
  16. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  17. 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); } }
  18. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  19. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  20. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  21. 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); }
  22. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  23. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  24. 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); }
  25. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  26. var Module = _modularityFront.default.Module;
  27. var SearchBarPackage =
  28. /*#__PURE__*/
  29. function (_Modularity$Module) {
  30. _inherits(SearchBarPackage, _Modularity$Module);
  31. function SearchBarPackage(props) {
  32. var _this;
  33. _classCallCheck(this, SearchBarPackage);
  34. _this = _possibleConstructorReturn(this, _getPrototypeOf(SearchBarPackage).call(this, props));
  35. _this.state = {
  36. suggestions: {},
  37. printerSuggestions: {},
  38. text: ''
  39. };
  40. _this.api = new _API.default(); // this.Token.onUpdate((v) => { this.back_service.Token.update(v);this.forceUpdate(); });
  41. return _this;
  42. }
  43. _createClass(SearchBarPackage, [{
  44. key: "getSuggestions",
  45. value: function getSuggestions(value) {
  46. var _this2 = this;
  47. if (!value || value.length !== 3) return;
  48. this.api.get('/products/search/' + value).then(function (suggestions) {
  49. return suggestions;
  50. }).then(function (suggestions) {
  51. return suggestions.reduce(function (res, s) {
  52. res[s.name] = {
  53. image: s.image,
  54. key: s.name,
  55. id: s.id
  56. };
  57. return res;
  58. }, {});
  59. }).then(function (suggestions) {
  60. return _this2.setState({
  61. suggestions: suggestions,
  62. text: value
  63. });
  64. });
  65. this.api.get('/printers/search/' + value).then(function (suggestions) {
  66. return suggestions.reduce(function (res, s) {
  67. res[s.name] = {
  68. image: s.image,
  69. key: s.name,
  70. id: s.id,
  71. printer: true
  72. };
  73. return res;
  74. }, {});
  75. }).then(function (printerSuggestions) {
  76. _this2.setState({
  77. printerSuggestions: printerSuggestions,
  78. text: value
  79. });
  80. });
  81. }
  82. }, {
  83. key: "search",
  84. value: function search(data) {
  85. console.log("SEARCH", data);
  86. if (data && data.printer) this.CoreSystem.go("/printer/" + data.id);else if (data) this.CoreSystem.go("/search/" + data.key);
  87. }
  88. }, {
  89. key: "render",
  90. value: function render() {
  91. var _this3 = this;
  92. console.log("Suggestions:", this.state.suggestions);
  93. return _react.default.createElement("div", null, _react.default.createElement(_searchbar.default, {
  94. Data: _objectSpread({}, this.state.printerSuggestions, this.state.suggestions),
  95. SearchBar: {
  96. placeholder: "Search"
  97. },
  98. Text: function Text(v) {
  99. return _this3.getSuggestions(v);
  100. },
  101. ObjectData: function ObjectData(data) {
  102. return _this3.search(data);
  103. },
  104. className: "SearchBarPackage " + this.ClassName.value
  105. }), _get(_getPrototypeOf(SearchBarPackage.prototype), "render", this).call(this));
  106. }
  107. }]);
  108. return SearchBarPackage;
  109. }(_modularityFront.default.Module);
  110. SearchBarPackage.Inputs = {
  111. SearchRoute: {
  112. type: Module.Types.Text,
  113. defaultValue: '/search'
  114. },
  115. ClassName: {
  116. type: Module.Types.Text,
  117. defaultValue: ''
  118. }
  119. };
  120. SearchBarPackage.Category = "NikatlasPackages";
  121. SearchBarPackage.EditPosition = "top-center";
  122. var _default = SearchBarPackage;
  123. exports.default = _default;