AddressesDisplay.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 _Form = _interopRequireDefault(require("../Modules/Form"));
  8. var _AddressBox = _interopRequireDefault(require("../Modules/AddressBox"));
  9. var _ArraySelector = _interopRequireDefault(require("../Modules/ArraySelector"));
  10. var _AddressInput = _interopRequireDefault(require("../Inputs/AddressInput"));
  11. var _Toast = _interopRequireDefault(require("../helpers/Toast"));
  12. var _Merge = _interopRequireDefault(require("../helpers/Merge"));
  13. var _DictionaryService = _interopRequireDefault(require("../Services/DictionaryService"));
  14. var _reactMaterialize = require("react-materialize");
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. 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); }
  17. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  18. 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); } }
  19. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  20. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  21. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  22. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  23. 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); }
  24. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  25. var Addresses =
  26. /*#__PURE__*/
  27. function (_React$Component) {
  28. _inherits(Addresses, _React$Component);
  29. function Addresses(props) {
  30. var _this;
  31. _classCallCheck(this, Addresses);
  32. _this = _possibleConstructorReturn(this, _getPrototypeOf(Addresses).call(this, props));
  33. _this.state = {
  34. addresses: [],
  35. showAddressForm: false,
  36. selectedAddress: {},
  37. edit: false
  38. };
  39. _this.updateService(props.service);
  40. return _this;
  41. }
  42. _createClass(Addresses, [{
  43. key: "componentWillReceiveProps",
  44. value: function componentWillReceiveProps(props) {
  45. if (props.service) {
  46. var service = props.service;
  47. this.updateService(service);
  48. }
  49. }
  50. }, {
  51. key: "updateService",
  52. value: function updateService(service) {
  53. var _this2 = this;
  54. service.getPage(1, 100).then(function (addresses) {
  55. _this2.setState({
  56. addresses: addresses,
  57. showAddressForm: false
  58. });
  59. });
  60. }
  61. }, {
  62. key: "edit",
  63. value: function edit(address) {
  64. this.setState({
  65. showAddressForm: true,
  66. edit: true,
  67. editAddress: address
  68. });
  69. }
  70. }, {
  71. key: "delete",
  72. value: function _delete(address) {
  73. var _this3 = this;
  74. this.props.update && this.props.update('' + Math.random() * Math.random());
  75. if (window.confirm(_DictionaryService.default.get("Delete address ?"))) this.props.service.update(address.id, {
  76. deleted: true
  77. }).then(function () {
  78. return _this3.updateService(_this3.props.service);
  79. });
  80. }
  81. }, {
  82. key: "getAddressForm",
  83. value: function getAddressForm() {
  84. var _this4 = this;
  85. return _react.default.createElement("div", null, _react.default.createElement(_Form.default, {
  86. FormInputs: this.state.edit ? (0, _Merge.default)(_AddressInput.default, this.state.editAddress) : _AddressInput.default,
  87. formData: function formData(address) {
  88. return _this4.setState({
  89. newAddress: address
  90. });
  91. }
  92. }), _react.default.createElement(_reactMaterialize.Button, {
  93. onClick: function onClick() {
  94. var action = _this4.state.edit ? _this4.props.service.update(_this4.state.editAddress.id, _this4.state.newAddress) : _this4.props.service.add(_this4.state.newAddress);
  95. action.then(function () {
  96. return _this4.updateService(_this4.props.service);
  97. }).then(function () {
  98. _this4.props.update && _this4.props.update(Math.random());
  99. _this4.setState({
  100. showAddressForm: false
  101. });
  102. });
  103. }
  104. }, this.state.edit ? _DictionaryService.default.get('Update') : _DictionaryService.default.get('Add')), !!this.state.addresses.length && _react.default.createElement(_reactMaterialize.Button, {
  105. onClick: function onClick() {
  106. _this4.setState({
  107. showAddressForm: false
  108. });
  109. }
  110. }, _DictionaryService.default.get('Cancel')));
  111. }
  112. }, {
  113. key: "getSelector",
  114. value: function getSelector() {
  115. var _this5 = this;
  116. return _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_ArraySelector.default, {
  117. Array: this.state.addresses,
  118. displayItem: function displayItem(address, indx) {
  119. return _react.default.createElement(_AddressBox.default, {
  120. key: indx,
  121. Address: address,
  122. Selected: _this5.state.selectedAddress.id == address.id,
  123. onSelect: function onSelect() {
  124. _this5.setState({
  125. selectedAddress: address
  126. });
  127. _this5.props.onSelect && _this5.props.onSelect(address);
  128. },
  129. onEdit: function onEdit() {
  130. return _this5.edit(address);
  131. },
  132. onDelete: function onDelete() {
  133. return _this5.delete(address);
  134. }
  135. });
  136. }
  137. }), _react.default.createElement(_reactMaterialize.Button, {
  138. onClick: function onClick() {
  139. return _this5.setState({
  140. showAddressForm: true,
  141. edit: false
  142. });
  143. }
  144. }, _DictionaryService.default.get("Add address")));
  145. }
  146. }, {
  147. key: "render",
  148. value: function render() {
  149. if (!this.state.showAddressForm && !this.state.addresses.length) {
  150. this.setState({
  151. showAddressForm: true
  152. });
  153. }
  154. return this.state.showAddressForm ? this.getAddressForm() : this.getSelector();
  155. }
  156. }]);
  157. return Addresses;
  158. }(_react.default.Component);
  159. var _default = Addresses;
  160. exports.default = _default;