mini-cart.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. require("./mini-cart.css");
  10. var _ShoppingCart = _interopRequireDefault(require("@material-ui/icons/ShoppingCart"));
  11. var _DictionaryService = _interopRequireDefault(require("../Services/DictionaryService"));
  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 Link = _modularityFront.default.Modules.Link;
  28. var MiniCartModule =
  29. /*#__PURE__*/
  30. function (_Modularity$Module) {
  31. _inherits(MiniCartModule, _Modularity$Module);
  32. function MiniCartModule(props) {
  33. var _this;
  34. _classCallCheck(this, MiniCartModule);
  35. _this = _possibleConstructorReturn(this, _getPrototypeOf(MiniCartModule).call(this, props));
  36. _this.state = {
  37. products: {}
  38. };
  39. _this.addProduct.onUpdate(function (value) {
  40. if (!value || !value.id) {
  41. return;
  42. }
  43. var products = _objectSpread({}, _this.state.products);
  44. var key = value.id;
  45. var val = {
  46. image: value.image,
  47. name: value.name,
  48. price: value.price,
  49. quantity: value.quantity
  50. };
  51. console.log(val); // Thats me punk writing this code below !!!
  52. key in products ? products[key].quantity += val.quantity : products[key] = val;
  53. _this.setState({
  54. products: products
  55. });
  56. _this._save(products);
  57. _this.products.set(products);
  58. });
  59. _this._load();
  60. return _this;
  61. }
  62. _createClass(MiniCartModule, [{
  63. key: "_minBut",
  64. value: function _minBut(e, id) {
  65. var products = this.state.products;
  66. if (products[id].quantity > 1) {
  67. products[id].quantity -= 1;
  68. } else {
  69. delete products[id];
  70. }
  71. this.setState({
  72. products: products
  73. });
  74. this._save(products);
  75. this.products.set(products);
  76. }
  77. }, {
  78. key: "_addBut",
  79. value: function _addBut(e, id) {
  80. var products = this.state.products;
  81. products[id].quantity += 1;
  82. this.setState({
  83. products: products
  84. });
  85. this.products.set(products);
  86. this._save(products);
  87. return false;
  88. }
  89. }, {
  90. key: "_save",
  91. value: function _save(products) {
  92. localStorage.setItem("cart_state", JSON.stringify(products));
  93. }
  94. }, {
  95. key: "_load",
  96. value: function _load() {
  97. var local = localStorage.getItem('cart_state');
  98. if (local) {
  99. this.state.products = JSON.parse(local);
  100. this.products.set(this.state.products);
  101. }
  102. }
  103. }, {
  104. key: "cartPage",
  105. value: function cartPage() {
  106. console.log("this");
  107. this.CoreSystem.go("/orders");
  108. }
  109. }, {
  110. key: "render",
  111. value: function render() {
  112. var _this2 = this;
  113. var prods = this.state.products;
  114. var nameList = Object.keys(prods).map(function (id, index) {
  115. return _react.default.createElement("li", {
  116. key: id,
  117. className: "custom-flex basket-row"
  118. }, _react.default.createElement("div", null, _react.default.createElement("img", {
  119. className: "circle mini_img",
  120. src: "/yuna.jpg"
  121. })), _react.default.createElement("div", {
  122. className: "flex-prod-name"
  123. }, _react.default.createElement(Link, {
  124. link: "/product/" + id
  125. }, prods[id].name)), _react.default.createElement("div", {
  126. className: "cust-flex"
  127. }, _react.default.createElement("div", null, _react.default.createElement("span", {
  128. onClick: function onClick(e) {
  129. _this2._minBut(e, id);
  130. },
  131. className: "quantity_but"
  132. }, "\u2212")), _react.default.createElement("div", null, prods[id].quantity), _react.default.createElement("div", null, _react.default.createElement("span", {
  133. onClick: function onClick(e) {
  134. _this2._addBut(e, id);
  135. },
  136. className: "quantity_but"
  137. }, "+"))), _react.default.createElement("div", {
  138. className: "prod_price"
  139. }, prods[id].price, " \u20AC"));
  140. });
  141. var selector = false;
  142. if (Object.keys(prods).length > 0) {
  143. selector = true;
  144. }
  145. var total = Object.keys(prods).reduce(function (total, key) {
  146. return total + prods[key].quantity * prods[key].price;
  147. }, 0).toFixed(2);
  148. console.log("############", total);
  149. return _react.default.createElement("div", {
  150. className: "dropdown " + (selector ? "HAHAHA" : "NO"),
  151. style: {
  152. verticalAlign: 'bottom',
  153. display: 'flex',
  154. height: 64,
  155. justifyContent: 'center',
  156. flexDirection: 'column'
  157. }
  158. }, _react.default.createElement(_ShoppingCart.default, {
  159. onClick: function onClick(e) {
  160. return _this2.cartPage();
  161. },
  162. className: "cart_size"
  163. }), selector ? _react.default.createElement("div", {
  164. className: "dropdown-cont cart",
  165. key: Math.random()
  166. }, _react.default.createElement("div", {
  167. className: "cart_title"
  168. }, _DictionaryService.default.get("Your Cart")), _react.default.createElement("div", {
  169. className: "cart_products"
  170. }, _react.default.createElement("ol", null, nameList)), _react.default.createElement(_reactMaterialize.Row, {
  171. className: "cart_total"
  172. }, _react.default.createElement("span", null, _DictionaryService.default.get("Total")), _react.default.createElement("b", null, total, " \u20AC")), _react.default.createElement(_reactMaterialize.Row, null, _react.default.createElement("div", {
  173. className: "btn order_but",
  174. onClick: function onClick(e) {
  175. return _this2.cartPage();
  176. }
  177. }, _DictionaryService.default.get("Checkout")))) : _react.default.createElement("div", {
  178. className: "dropdown-cont cart empty",
  179. key: Math.random()
  180. }, _react.default.createElement("div", {
  181. className: "empty-cart"
  182. }, _DictionaryService.default.get("Empty cart"))), _get(_getPrototypeOf(MiniCartModule.prototype), "render", this).call(this));
  183. }
  184. }]);
  185. return MiniCartModule;
  186. }(_modularityFront.default.Module);
  187. MiniCartModule.EditPosition = "center";
  188. MiniCartModule.Inputs = {
  189. cartImage: {
  190. type: Module.Types.Text,
  191. defaultValue: "/mini_cart.png"
  192. },
  193. addProduct: {
  194. type: Module.Types.Object
  195. }
  196. };
  197. MiniCartModule.Outputs = {
  198. products: {}
  199. };
  200. MiniCartModule.Category = "sxoinas";
  201. var _default = MiniCartModule;
  202. exports.default = _default;