UserAccount.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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("./UserAccount.css");
  10. var _Form = _interopRequireDefault(require("../../Modules/Form"));
  11. var _AddressBox = _interopRequireDefault(require("../../Modules/AddressBox"));
  12. var _ArraySelector = _interopRequireDefault(require("../../Modules/ArraySelector"));
  13. var _AddressInput = _interopRequireDefault(require("../../Inputs/AddressInput"));
  14. var _TableData = _interopRequireDefault(require("../../HigherModules/TableData"));
  15. var _ClientInput = _interopRequireDefault(require("../../Inputs/ClientInput"));
  16. var _Merge = _interopRequireDefault(require("../../helpers/Merge"));
  17. var _Toast = _interopRequireDefault(require("../../helpers/Toast"));
  18. var _AddressesDisplay = _interopRequireDefault(require("../../HigherModules/AddressesDisplay"));
  19. var _Repository = _interopRequireDefault(require("../../Services/Repository"));
  20. var _UserService = _interopRequireDefault(require("../../Services/UserService"));
  21. var _DictionaryService = _interopRequireDefault(require("../../Services/DictionaryService"));
  22. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  23. 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); }
  24. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  25. 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); } }
  26. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  27. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  28. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  29. 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); }
  30. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  31. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  32. 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); }
  33. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  34. var Module = _modularityFront.default.Module; // let Link = Modularity.Modules.Link;
  35. var UserAccount =
  36. /*#__PURE__*/
  37. function (_Modularity$Module) {
  38. _inherits(UserAccount, _Modularity$Module);
  39. function UserAccount(props) {
  40. var _this;
  41. _classCallCheck(this, UserAccount);
  42. _this = _possibleConstructorReturn(this, _getPrototypeOf(UserAccount).call(this, props));
  43. _this.state = {
  44. view: "info" //this.getProfile();
  45. };
  46. _this.addressRepo = new _Repository.default('addresses', _this.Token.value);
  47. _this.accountRepo = new _Repository.default('accounts', _this.Token.value);
  48. _this.ordersRepo = new _Repository.default('orders', _this.Token.value, [{
  49. type: "descending",
  50. field: "createdAt"
  51. }]);
  52. _this.userService = new _UserService.default({});
  53. _this.Token.onUpdate(function () {
  54. return _this.handleToken();
  55. });
  56. _this.handleToken();
  57. _this.RoutingSystem.onRouteChange("UserAccount", function () {
  58. _this.checkRoute();
  59. });
  60. setTimeout(function () {
  61. return _this.checkRoute();
  62. }, 0);
  63. return _this;
  64. }
  65. _createClass(UserAccount, [{
  66. key: "checkRoute",
  67. value: function checkRoute() {
  68. var route = this.RoutingSystem.getRoute().url || '';
  69. if (route.endsWith("/orders")) {
  70. this.setState({
  71. view: "orders"
  72. });
  73. } else if (route.endsWith("/addresses")) {
  74. this.setState({
  75. view: "addresses"
  76. });
  77. } else {
  78. this.setState({
  79. view: "info"
  80. });
  81. }
  82. }
  83. }, {
  84. key: "updateUser",
  85. value: function updateUser() {
  86. var _this2 = this;
  87. this.userService.getUser(this.Token.value).then(function (user) {
  88. return _this2.setState({
  89. user: user
  90. });
  91. });
  92. }
  93. }, {
  94. key: "handleToken",
  95. value: function handleToken() {
  96. if (this.Token.value === undefined) return;
  97. if (this.Token.value === "missing" && this.RoutingSystem.getRoute().url !== "/login" && this.routeMatch()) {
  98. this.CoreSystem.go('/login', {
  99. ref: this.RoutingSystem.getRoute().url
  100. }, true);
  101. }
  102. this.accountRepo.setToken(this.Token.value);
  103. this.addressRepo.setToken(this.Token.value);
  104. this.ordersRepo.setToken(this.Token.value);
  105. this.updateUser();
  106. this.forceUpdate();
  107. }
  108. }, {
  109. key: "routeMatch",
  110. value: function routeMatch() {
  111. return this.RoutingSystem.getRoute().url.startsWith(this.baseURL.value);
  112. }
  113. }, {
  114. key: "updateUserInfo",
  115. value: function updateUserInfo(data) {
  116. var _this3 = this;
  117. this.accountRepo.update(this.state.user.Account.id, data).then(function () {
  118. return _this3.updateUser();
  119. }).then(function () {
  120. return _Toast.default.notify(_DictionaryService.default.get("Successfully updated user info"));
  121. });
  122. }
  123. }, {
  124. key: "changePass",
  125. value: function changePass(data) {
  126. if (data.npass !== data.rpass) {
  127. _Toast.default.error("New Passwords are different");
  128. return;
  129. }
  130. this.userService.changePass(this.Token.value, data.cpass, data.npass).then(function () {
  131. return _Toast.default.notify("Password changed");
  132. }).catch(function () {
  133. return _Toast.default.error("Wrong credentials");
  134. });
  135. }
  136. }, {
  137. key: "_viewOrder",
  138. value: function _viewOrder(order) {
  139. console.log("sss", order.id);
  140. this.CoreSystem.go('/profile/order/' + order.id);
  141. }
  142. }, {
  143. key: "render",
  144. value: function render() {
  145. var _this4 = this;
  146. var Link = function Link(props) {
  147. return _react.default.createElement("div", {
  148. onClick: function onClick() {
  149. _this4.CoreSystem.go(props.link);
  150. }
  151. }, props.children);
  152. };
  153. return this.state.user ? _react.default.createElement("div", {
  154. className: "row UserAccount " + this.ClassName.value
  155. }, _react.default.createElement("div", {
  156. className: 'col s12 m4 l3 Menu '
  157. }, _react.default.createElement("div", {
  158. className: "Menu_container"
  159. }, _react.default.createElement("h6", {
  160. className: "Menu_header"
  161. }, _DictionaryService.default.get("User Profile")), _react.default.createElement(Link, {
  162. link: '/user/orders',
  163. className: this.state.view === "orders" && 'selected'
  164. }, _react.default.createElement("i", {
  165. className: "fa fa-shopping-basket"
  166. }), _react.default.createElement("span", null, _DictionaryService.default.get("Orders"))), _react.default.createElement(Link, {
  167. link: '/user',
  168. className: this.state.view === "info" && 'selected'
  169. }, _react.default.createElement("i", {
  170. className: "fa fa-user"
  171. }), _react.default.createElement("span", null, _DictionaryService.default.get("Account information"))), _react.default.createElement(Link, {
  172. link: '/user/addresses',
  173. className: this.state.view === "addresses" && 'selected'
  174. }, _react.default.createElement("i", {
  175. className: "fa fa-home"
  176. }), _react.default.createElement("span", null, _DictionaryService.default.get("Addresses"))))), _react.default.createElement("div", {
  177. className: 'col s12 m8 l9 wrapper'
  178. }, _react.default.createElement(_reactMaterialize.Card, {
  179. className: ""
  180. }, this.state.view === "orders" && _react.default.createElement(Orders, {
  181. service: this.ordersRepo,
  182. viewOrder: function viewOrder(e) {
  183. return _this4._viewOrder(e);
  184. }
  185. }), this.state.view === "info" && _react.default.createElement(Info, {
  186. account: this.state.user.Account,
  187. onSubmit: function onSubmit(data) {
  188. return _this4.updateUserInfo(data);
  189. },
  190. onChangePassSubmit: function onChangePassSubmit(data) {
  191. return _this4.changePass(data);
  192. }
  193. }), this.state.view === "addresses" && _react.default.createElement("div", null, _DictionaryService.default.get("My addresses"), _react.default.createElement("br", null), _react.default.createElement(_AddressesDisplay.default, {
  194. key: this.addressRepo.token,
  195. service: this.addressRepo
  196. })))), _get(_getPrototypeOf(UserAccount.prototype), "render", this).call(this)) : null;
  197. }
  198. }]);
  199. return UserAccount;
  200. }(_modularityFront.default.Module);
  201. var Orders =
  202. /*#__PURE__*/
  203. function (_React$Component) {
  204. _inherits(Orders, _React$Component);
  205. function Orders(props) {
  206. var _this5;
  207. _classCallCheck(this, Orders);
  208. _this5 = _possibleConstructorReturn(this, _getPrototypeOf(Orders).call(this, props));
  209. _this5.state = {};
  210. _this5.tableHeaders = {
  211. "Αριθμός παραγγελίας": "id",
  212. "Ημερομηνία παραγγελίας": {
  213. field: "createdAt",
  214. type: "date"
  215. },
  216. "Κατάσταση": "status",
  217. "Ποσό": {
  218. field: "amount",
  219. suffix: " €",
  220. precision: 2
  221. }
  222. };
  223. return _this5;
  224. }
  225. _createClass(Orders, [{
  226. key: "render",
  227. value: function render() {
  228. var _this6 = this;
  229. return _react.default.createElement("div", null, _react.default.createElement(_TableData.default, {
  230. service: this.props.service,
  231. TableHeaders: this.tableHeaders,
  232. onView: function onView(e) {
  233. return _this6.props.viewOrder(e);
  234. }
  235. }));
  236. }
  237. }]);
  238. return Orders;
  239. }(_react.default.Component);
  240. var Info =
  241. /*#__PURE__*/
  242. function (_React$Component2) {
  243. _inherits(Info, _React$Component2);
  244. function Info(props) {
  245. var _this7;
  246. _classCallCheck(this, Info);
  247. _this7 = _possibleConstructorReturn(this, _getPrototypeOf(Info).call(this, props));
  248. _this7.state = {};
  249. _this7.passwordInputs = {
  250. "cpass": {
  251. type: "text",
  252. placeholder: "Current password",
  253. columns: "col s12",
  254. filter: {
  255. password: true,
  256. required: true
  257. }
  258. },
  259. "npass": {
  260. type: "text",
  261. placeholder: "New password",
  262. columns: "col s12",
  263. filter: {
  264. password: true,
  265. required: true
  266. }
  267. },
  268. "rpass": {
  269. type: "text",
  270. placeholder: "Repeat new password",
  271. columns: "col s12",
  272. filter: {
  273. password: true,
  274. required: true
  275. }
  276. }
  277. };
  278. return _this7;
  279. }
  280. _createClass(Info, [{
  281. key: "render",
  282. value: function render() {
  283. var _this8 = this;
  284. return _react.default.createElement("div", {
  285. className: "Info"
  286. }, _react.default.createElement("h6", {
  287. className: "Title_Container"
  288. }, _react.default.createElement("div", {
  289. className: "title"
  290. }, "\u039F\u03B9 \u03C0\u03BB\u03B7\u03C1\u03BF\u03C6\u03BF\u03C1\u03B9\u03B5\u03C2 \u03BC\u03BF\u03C5")), _react.default.createElement("div", {
  291. className: "Content"
  292. }, _react.default.createElement(_Form.default, {
  293. FormInputs: (0, _Merge.default)(_ClientInput.default, this.props.account || {}),
  294. activeButton: true,
  295. button: 'Αποθήκευση',
  296. onSubmit: function onSubmit(d) {
  297. return _this8.props.onSubmit(d);
  298. }
  299. })), _react.default.createElement("h6", {
  300. className: "Title_Container"
  301. }, _react.default.createElement("div", {
  302. className: "title"
  303. }, "\u0391\u03BB\u03BB\u03B1\u03B3\u03AE \u03BA\u03C9\u03B4\u03B9\u03BA\u03BF\u03CD")), _react.default.createElement("div", {
  304. className: "Content"
  305. }, _react.default.createElement(_Form.default, {
  306. FormInputs: this.passwordInputs,
  307. activeButton: true,
  308. button: 'Αλλαγή κωδικού',
  309. onSubmit: function onSubmit(d) {
  310. return _this8.props.onChangePassSubmit(d);
  311. }
  312. })));
  313. }
  314. }]);
  315. return Info;
  316. }(_react.default.Component);
  317. UserAccount.Inputs = {
  318. Account: {
  319. type: Module.Types.Object
  320. },
  321. Token: {
  322. type: Module.Types.Text
  323. },
  324. ClassName: {
  325. type: Module.Types.Text
  326. },
  327. baseURL: {
  328. type: Module.Types.Text,
  329. defaultValue: "/user"
  330. }
  331. };
  332. UserAccount.Category = "Sxoinas Packages";
  333. var _default = UserAccount;
  334. exports.default = _default;