UserPackage.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 _Button = _interopRequireDefault(require("@material-ui/core/Button"));
  10. var _Form = _interopRequireDefault(require("../Modules/Form.js"));
  11. var _UserService = _interopRequireDefault(require("../Services/UserService.js"));
  12. var _DictionaryService = _interopRequireDefault(require("../Services/DictionaryService.js"));
  13. var _Toast = _interopRequireDefault(require("../helpers/Toast"));
  14. require("./UserPackage.css");
  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 _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); }
  23. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  24. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  25. 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); }
  26. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  27. var Module = _modularityFront.default.Module;
  28. var UserPackage =
  29. /*#__PURE__*/
  30. function (_Modularity$Module) {
  31. _inherits(UserPackage, _Modularity$Module);
  32. function UserPackage(props) {
  33. var _this;
  34. _classCallCheck(this, UserPackage);
  35. _this = _possibleConstructorReturn(this, _getPrototypeOf(UserPackage).call(this, props));
  36. _this.state = {
  37. registerForm: false
  38. };
  39. var propertiesService = {
  40. Token: function Token(t) {
  41. _this.checkToken();
  42. _this.Token.set(t);
  43. _this.forceUpdate();
  44. },
  45. User: function User(u) {
  46. _this.User.set(u);
  47. _this.forceUpdate();
  48. }
  49. };
  50. _this.back_service = new _UserService.default(propertiesService); // this.Token.onUpdate((v) => { this.back_service.Token.update(v);this.forceUpdate(); });
  51. var withLogin = _this.LoginURL.value;
  52. if (withLogin) {
  53. _this.RoutingSystem.addRoute(withLogin, _this.RoutingSystem.currentView, 'PackageLogin');
  54. }
  55. var url = new URL(window.location.href);
  56. var confirmtoken = url.searchParams.get("token");
  57. if (confirmtoken) {
  58. _this.back_service.confirmUser(confirmtoken).then(function () {
  59. return _Toast.default.success("Ο χρήστης επιβεβαιώθηκε. Συνδέσου τώρα!");
  60. });
  61. }
  62. setTimeout(function () {
  63. return _this.checkToken();
  64. }, 0);
  65. _this.RoutingSystem.onRouteChange("Login", function () {
  66. return _this.checkToken();
  67. });
  68. return _this;
  69. }
  70. _createClass(UserPackage, [{
  71. key: "checkToken",
  72. value: function checkToken() {
  73. var withLogin = this.LoginURL.value;
  74. var route = this.RoutingSystem.getRoute().url;
  75. if (route === withLogin) {
  76. this.setState({
  77. login: true,
  78. ref: this.RoutingSystem.currentState.ref || "/home"
  79. });
  80. if (this.back_service.isLogged()) {
  81. this.CoreSystem.go(this.state.ref);
  82. }
  83. } else {
  84. this.setState({
  85. login: false
  86. });
  87. }
  88. }
  89. }, {
  90. key: "showLogin",
  91. value: function showLogin() {
  92. this.setState({
  93. login: true,
  94. ref: this.RoutingSystem.getRoute().url
  95. });
  96. this.CoreSystem.go("/login", {
  97. ref: this.RoutingSystem.getRoute().url
  98. });
  99. }
  100. }, {
  101. key: "login",
  102. value: function login(data) {
  103. var _this2 = this;
  104. // check if all data is comple
  105. if (!data) return;
  106. var username = data.username,
  107. password = data.password;
  108. if (username && password) this.back_service.login(data).then(function (d) {
  109. _Toast.default.notify(_DictionaryService.default.get("Welcome") + " &nbsp;<b>" + username + "</b>");
  110. _this2.CoreSystem.go(_this2.state.ref);
  111. }).catch(function (err) {
  112. if (err.status === 400) {
  113. _Toast.default.error(_DictionaryService.default.get("Wrong credentials"));
  114. } else if (err.status === 403) {
  115. _Toast.default.notify(_DictionaryService.default.get("A verification mail has been sent to your inbox. You need to verify before login."));
  116. }
  117. });
  118. }
  119. }, {
  120. key: "register",
  121. value: function register(data) {
  122. var _this3 = this;
  123. // check if all data is comple
  124. if (!data) return;
  125. var username = data.username,
  126. password = data.password,
  127. email = data.email;
  128. if (username && password && email) this.back_service.register(data).then(function (user) {
  129. _Toast.default.success(_DictionaryService.default.get("Successfully registered! Please login"));
  130. _this3.setState({
  131. registerForm: false
  132. });
  133. }).catch(function (err) {
  134. if (err.status === 400) {
  135. _Toast.default.error(_DictionaryService.default.get("Wrong credentials"));
  136. }
  137. });
  138. }
  139. }, {
  140. key: "logout",
  141. value: function logout() {
  142. var _this4 = this;
  143. this.back_service.logout().then(function () {
  144. _Toast.default.notify(_DictionaryService.default.get("Logged out")); //window.location.href = "/login";
  145. _this4.forceUpdate();
  146. }).catch(function () {
  147. _Toast.default.error("Cannot Logout");
  148. });
  149. }
  150. }, {
  151. key: "render",
  152. value: function render() {
  153. var _this5 = this;
  154. var Token = this.back_service.getToken() || 'missing';
  155. var User = this.back_service.User.value || {}; // console.log("Token : ", Token);
  156. var FormInputs = {
  157. "username": {
  158. type: "text",
  159. placeholder: "Username",
  160. columns: 12,
  161. filter: {
  162. required: true
  163. }
  164. },
  165. "password": {
  166. type: "password",
  167. placeholder: "Password",
  168. columns: 12,
  169. filter: {
  170. required: true,
  171. password: true
  172. }
  173. }
  174. };
  175. if (this.state.registerForm && this.withRegister.value) {
  176. FormInputs["email"] = {
  177. type: "email",
  178. placeholder: "Email",
  179. columns: 12,
  180. filter: {
  181. email: true,
  182. required: true
  183. }
  184. };
  185. }
  186. var LoginScreen = _react.default.createElement("div", {
  187. className: 'LoginScreen valign-wrapper'
  188. }, _react.default.createElement("div", {
  189. className: 'FormContainer hoverable'
  190. }, _react.default.createElement(_reactMaterialize.Row, null, _react.default.createElement(_reactMaterialize.Col, {
  191. s: 12
  192. }, _react.default.createElement("img", {
  193. src: this.Logo.value || ''
  194. })), _react.default.createElement(_reactMaterialize.Col, {
  195. s: 12,
  196. className: 'LoginText'
  197. }, _react.default.createElement("h4", null, this.Header.value || ''))), _react.default.createElement(_reactMaterialize.Row, null, _react.default.createElement(_reactMaterialize.Col, {
  198. s: 12
  199. }, _react.default.createElement(_Form.default, {
  200. key: Math.random(),
  201. className: 'center-align',
  202. FormInputs: FormInputs,
  203. button: this.state.registerForm ? _DictionaryService.default.get("Register") : _DictionaryService.default.get("Login"),
  204. onSubmit: function onSubmit(data) {
  205. return _this5.state.registerForm ? _this5.register(data) : _this5.login(data);
  206. },
  207. activeButton: true
  208. })), this.withRegister.value ? _react.default.createElement(_reactMaterialize.Col, {
  209. s: 12,
  210. className: 'RegisterText',
  211. onClick: function onClick() {
  212. return _this5.setState({
  213. registerForm: !_this5.state.registerForm
  214. });
  215. }
  216. }, this.state.registerForm ? _DictionaryService.default.get("Already registered? Login now!") : _DictionaryService.default.get("You don't have an account? Register here!")) : '')), _get(_getPrototypeOf(UserPackage.prototype), "render", this).call(this));
  217. var needsLogin = Token === "missing";
  218. return needsLogin ? this.state.login || this.ForceLogin.value ? LoginScreen : _react.default.createElement(_Button.default, {
  219. className: this.className.value,
  220. onClick: function onClick() {
  221. return _this5.showLogin();
  222. }
  223. }, _DictionaryService.default.get("Login")) : _react.default.createElement("div", null, _react.default.createElement(_Button.default, {
  224. className: this.className.value,
  225. onClick: function onClick() {
  226. return _this5.CoreSystem.go(_this5.UserURL.value);
  227. }
  228. }, User.username || '...'), _react.default.createElement(_Button.default, {
  229. className: this.className.value,
  230. onClick: function onClick() {
  231. return _this5.logout();
  232. }
  233. }, _DictionaryService.default.get("Logout")), _get(_getPrototypeOf(UserPackage.prototype), "render", this).call(this));
  234. }
  235. }]);
  236. return UserPackage;
  237. }(_modularityFront.default.Module);
  238. UserPackage.Inputs = {
  239. ForceLogin: {
  240. type: _modularityFront.default.Module.Types.Boolean,
  241. defaultValue: false
  242. },
  243. withRegister: {
  244. type: _modularityFront.default.Module.Types.Boolean,
  245. defaultValue: true
  246. },
  247. LoginURL: {
  248. type: _modularityFront.default.Module.Types.Text,
  249. defaultValue: '/login'
  250. },
  251. Logo: {
  252. type: _modularityFront.default.Module.Types.Text,
  253. defaultValue: '/logo.png'
  254. },
  255. Header: {
  256. type: _modularityFront.default.Module.Types.Text,
  257. defaultValue: 'Tonersales'
  258. },
  259. UserURL: {
  260. type: _modularityFront.default.Module.Types.Text,
  261. defaultValue: '/user'
  262. },
  263. className: {
  264. type: _modularityFront.default.Module.Types.Text
  265. }
  266. };
  267. UserPackage.Outputs = {
  268. Token: {},
  269. User: {}
  270. };
  271. UserPackage.Category = "NikatlasPackages";
  272. UserPackage.EditPosition = "top-center";
  273. var _default = UserPackage;
  274. exports.default = _default;