Presentation1.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 _Pagination = _interopRequireDefault(require("../../Modules/Pagination"));
  10. var _Card = _interopRequireDefault(require("../../Modules/Card"));
  11. var _Repository = _interopRequireDefault(require("../../Services/Repository"));
  12. var _DictionaryService = _interopRequireDefault(require("../../Services/DictionaryService"));
  13. var _FilterModule = _interopRequireDefault(require("../../Modules/FilterModule"));
  14. var _Done = _interopRequireDefault(require("@material-ui/icons/Done"));
  15. var _searchbar = _interopRequireDefault(require("../../Modules/searchbar"));
  16. var _Chip = _interopRequireDefault(require("../../Modules/Chip"));
  17. var _Toast = _interopRequireDefault(require("../../helpers/Toast"));
  18. var _DropdownFilter = _interopRequireDefault(require("../../Modules/UserInputs/DropdownFilter"));
  19. require("./presentation1.css");
  20. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21. 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); }
  22. 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; }
  23. 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; }
  24. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
  25. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
  26. function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
  27. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
  28. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  29. 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); } }
  30. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  31. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  32. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  33. 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); }
  34. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  35. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  36. 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); }
  37. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  38. var Module = _modularityFront.default.Module;
  39. var PresantationModule =
  40. /*#__PURE__*/
  41. function (_Modularity$Module) {
  42. _inherits(PresantationModule, _Modularity$Module);
  43. function PresantationModule(props) {
  44. var _this;
  45. _classCallCheck(this, PresantationModule);
  46. _this = _possibleConstructorReturn(this, _getPrototypeOf(PresantationModule).call(this, props));
  47. _this.state = {
  48. FilterInputs: [],
  49. page: 1,
  50. totalPages: 1,
  51. products: {},
  52. search: {},
  53. filterVisible: false
  54. };
  55. _this.orderingFilter = _this.getOrderingFilter();
  56. _this.perPage = 9;
  57. _this.Repository.onUpdate(function (repo) {
  58. return _this.updateRepository(repo);
  59. });
  60. _this.filters = [];
  61. _this.search = {
  62. type: "search",
  63. values: [],
  64. field: "name,description"
  65. };
  66. _this.setSearch(_this.search);
  67. _this.Filters.onUpdate(function (v) {
  68. return _this.updateFilters(v);
  69. });
  70. var session = sessionStorage.getItem("FilterInputs");
  71. if (!session) {
  72. _this.getModel();
  73. } else {
  74. _this.state.FilterInputs = JSON.parse(session);
  75. _this.filters = _this.state.FilterInputs;
  76. _this.update();
  77. }
  78. return _this;
  79. }
  80. _createClass(PresantationModule, [{
  81. key: "getModel",
  82. value: function getModel() {
  83. var _this2 = this;
  84. var ProductsRepo = new _Repository.default("products");
  85. ProductsRepo.model().then(function (model) {
  86. var inputs = [];
  87. for (var i in model) {
  88. if (model[i].Filter) {
  89. //TODO update these 2 lines logic
  90. if (model[i].Filter.type === 'search' && model[i].Filter.field === 'name') {
  91. _this2.setSearch(model[i].Filter);
  92. } else {
  93. inputs.push(model[i].Filter);
  94. }
  95. }
  96. }
  97. return inputs;
  98. }).then(function (v) {
  99. return v.reduce(function (initial, b) {
  100. return b.order > -1 ? initial.push(b) && initial : initial;
  101. }, []);
  102. }).then(function (v) {
  103. return v.sort(function (a, b) {
  104. return a.order - b.order;
  105. });
  106. }).then(function (v) {
  107. _this2.setState({
  108. FilterInputs: v
  109. });
  110. sessionStorage.setItem("FilterInputs", JSON.stringify(v));
  111. _this2.update();
  112. });
  113. }
  114. }, {
  115. key: "updateFilters",
  116. value: function updateFilters(filters) {
  117. if (filters === undefined) {
  118. return;
  119. }
  120. this.filters = filters;
  121. this.update();
  122. this.setState({
  123. FilterInputs: filters
  124. });
  125. sessionStorage.setItem("FilterInputs", JSON.stringify(filters));
  126. }
  127. }, {
  128. key: "getOrderingFilter",
  129. value: function getOrderingFilter() {
  130. return sessionStorage.getItem("Ordering") && JSON.parse(sessionStorage.getItem("Ordering")) || {
  131. type: 'ascending',
  132. options: {
  133. 'price': 'Τιμή',
  134. 'name': 'Όνομα προϊόντος'
  135. },
  136. field: 'price',
  137. required: true,
  138. value: 'price'
  139. };
  140. }
  141. }, {
  142. key: "update",
  143. value: function update() {
  144. sessionStorage.setItem("Ordering", JSON.stringify(this.orderingFilter));
  145. this.updateRepository(new _Repository.default('products', null, [].concat(_toConsumableArray(this.filters), [this.orderingFilter, this.search])));
  146. }
  147. }, {
  148. key: "updateRepository",
  149. value: function updateRepository(repo) {
  150. this.back_service = repo;
  151. this.getData();
  152. }
  153. }, {
  154. key: "updatePattern",
  155. value: function updatePattern(v) {
  156. window.history.replaceState({}, '', '/search/' + v); // Implement Into CoreSystem
  157. }
  158. }, {
  159. key: "getData",
  160. value: function getData() {
  161. var _this3 = this;
  162. var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
  163. this.back_service.count().then(function (result) {
  164. var totalPages = Math.ceil(result[0].counter / _this3.perPage);
  165. _this3.setState({
  166. totalPages: totalPages
  167. });
  168. });
  169. this.back_service.getPage(page, this.perPage).then(function (result) {
  170. return _this3.parseData(result);
  171. }).then(function (data) {
  172. _this3.setState({
  173. products: data
  174. });
  175. _this3.forceUpdate();
  176. }).catch(function (e) {
  177. console.log(e);
  178. });
  179. }
  180. }, {
  181. key: "parseData",
  182. value: function parseData(result) {
  183. return result.map(function (item) {
  184. var val = {
  185. id: item.id,
  186. name: item.name,
  187. description: item.description || "",
  188. shortdescription: item.shortdescription || "",
  189. price: item.price,
  190. image: item.image || "/logo.png",
  191. quantity: 1
  192. };
  193. return val;
  194. });
  195. }
  196. }, {
  197. key: "setSearch",
  198. value: function setSearch(filter) {
  199. var route = (this.RoutingSystem.getRoute().params || {}).value;
  200. filter.values = [route || ''];
  201. filter.field = "name,description";
  202. sessionStorage.setItem("SearchFilter", JSON.stringify(filter));
  203. this.search = filter; //this.update();
  204. }
  205. }, {
  206. key: "setSearchFilter",
  207. value: function setSearchFilter(v) {
  208. var filter = {
  209. type: "search",
  210. values: [v],
  211. field: "name,description"
  212. };
  213. sessionStorage.setItem("SearchFilter", JSON.stringify(filter));
  214. this.search = filter;
  215. this.updatePattern(v);
  216. this.update();
  217. }
  218. }, {
  219. key: "pageChanged",
  220. value: function pageChanged(v) {
  221. this.getData(v);
  222. }
  223. }, {
  224. key: "addProduct",
  225. value: function addProduct(v) {
  226. this.selectedProduct.set(v);
  227. _Toast.default.success(v.name + " added to your cart");
  228. this.forceUpdate();
  229. }
  230. }, {
  231. key: "render",
  232. value: function render() {
  233. var _this4 = this;
  234. var page = this.state.page;
  235. var totalPages = this.state.totalPages;
  236. return _react.default.createElement("div", {
  237. className: "present_container"
  238. }, _react.default.createElement(_reactMaterialize.Row, null, _react.default.createElement(_reactMaterialize.Col, {
  239. className: "filter_container " + (this.state.filterVisible && 'show'),
  240. l: 3
  241. }, _react.default.createElement(_FilterModule.default, {
  242. className: "filter_layout",
  243. Inputs: this.state.FilterInputs,
  244. Filters: function Filters(filters) {
  245. return _this4.updateFilters(filters);
  246. }
  247. }), _react.default.createElement("div", {
  248. className: "badge",
  249. onClick: function onClick() {
  250. _this4.setState({
  251. filterVisible: !_this4.state.filterVisible
  252. });
  253. }
  254. }, _react.default.createElement("i", {
  255. className: "fa fa-filter"
  256. }))), _react.default.createElement(_reactMaterialize.Col, {
  257. l: 9
  258. }, _react.default.createElement("div", {
  259. className: "chips_container"
  260. }, _react.default.createElement(_searchbar.default, {
  261. SearchBar: {
  262. defaultValue: (this.RoutingSystem.getRoute().params || {}).value
  263. },
  264. SearchData: function SearchData(v) {
  265. return _this4.setSearchFilter(v);
  266. }
  267. }), _react.default.createElement("div", {
  268. className: "filters"
  269. }, _react.default.createElement("div", {
  270. className: "orderBy"
  271. }, _react.default.createElement("div", {
  272. className: "tool",
  273. onClick: function onClick() {
  274. _this4.orderingFilter = _objectSpread({}, _this4.orderingFilter, {
  275. type: _this4.orderingFilter.type === "ascending" ? 'descending' : 'ascending'
  276. });
  277. _this4.update();
  278. }
  279. }, _react.default.createElement("i", {
  280. className: "fa fa-sort-" + (this.orderingFilter.type === "ascending" ? 'up' : 'down')
  281. })), _react.default.createElement(_DropdownFilter.default, {
  282. filter: this.orderingFilter,
  283. defaultValue: this.orderingFilter.field,
  284. Filters: function Filters(data) {
  285. var field = data.values[0];
  286. _this4.orderingFilter = _objectSpread({}, _this4.orderingFilter, {
  287. field: field,
  288. value: field
  289. });
  290. _this4.update();
  291. }
  292. })), _react.default.createElement("div", {
  293. className: "chip_module"
  294. }, _react.default.createElement(_Chip.default, {
  295. Inputs: this.state.FilterInputs,
  296. Filters: function Filters(filters) {
  297. return _this4.updateFilters(filters);
  298. }
  299. })))), _react.default.createElement("div", {
  300. className: "products_container"
  301. }, _react.default.createElement(PresentData, {
  302. products: this.state.products,
  303. addProduct: function addProduct(e) {
  304. return _this4.addProduct(e);
  305. },
  306. onClick: function onClick(p) {
  307. _this4.CoreSystem.go('/product/' + p.id);
  308. }
  309. }), _react.default.createElement(_Pagination.default, {
  310. selected: page,
  311. lastPage: totalPages,
  312. onPage: function onPage(v) {
  313. return _this4.pageChanged(v);
  314. }
  315. }))), _get(_getPrototypeOf(PresantationModule.prototype), "render", this).call(this)));
  316. }
  317. }]);
  318. return PresantationModule;
  319. }(_modularityFront.default.Module);
  320. var PresentData =
  321. /*#__PURE__*/
  322. function (_React$Component) {
  323. _inherits(PresentData, _React$Component);
  324. function PresentData(props) {
  325. _classCallCheck(this, PresentData);
  326. return _possibleConstructorReturn(this, _getPrototypeOf(PresentData).call(this, props));
  327. }
  328. _createClass(PresentData, [{
  329. key: "render",
  330. value: function render() {
  331. var _this5 = this;
  332. if (!this.props.products.length) {
  333. return _react.default.createElement("h6", null, _DictionaryService.default.get("No products"));
  334. }
  335. var prods = this.props.products;
  336. var prodList = Object.keys(prods).map(function (item, index) {
  337. var key = item; //item.id;
  338. var p = prods[key]; //item;
  339. return _react.default.createElement(_Card.default, {
  340. id: key,
  341. key: p.name,
  342. className: "col s12 m6 l4",
  343. CardTitle: p.name,
  344. Price: p.price,
  345. CardBody: _react.default.createElement("span", {
  346. dangerouslySetInnerHTML: {
  347. __html: p.shortdescription
  348. }
  349. }),
  350. CardImage: p.image,
  351. product: function product(prod) {
  352. return _this5.props.addProduct(p);
  353. },
  354. onClick: function onClick() {
  355. return _this5.props.onClick(p);
  356. }
  357. });
  358. });
  359. return _react.default.createElement(_reactMaterialize.Row, null, prodList);
  360. }
  361. }]);
  362. return PresentData;
  363. }(_react.default.Component);
  364. PresantationModule.EditPosition = "center";
  365. PresantationModule.Inputs = {
  366. Repository: {
  367. type: Module.Types.Object
  368. },
  369. Page: {
  370. type: Module.Types.Number
  371. },
  372. Url: {
  373. type: Module.Types.Text,
  374. defaultValue: ""
  375. },
  376. Filters: {
  377. type: Module.Types.Object
  378. }
  379. };
  380. PresantationModule.Outputs = {
  381. selectedProduct: {}
  382. };
  383. PresantationModule.Category = "Sxoinas Packages";
  384. var _default = PresantationModule;
  385. exports.default = _default;