Login.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 _Module2 = _interopRequireDefault(require("../Module"));
  8. var _Form = _interopRequireDefault(require("../theme/Form.js"));
  9. var _User = _interopRequireDefault(require("../../systems/services/User"));
  10. var _Modal = _interopRequireDefault(require("../layout/Modal.js"));
  11. require("./Login.css");
  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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  15. 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); } }
  16. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  17. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  18. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  19. 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); }
  20. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  21. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  22. 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); }
  23. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  24. var LoginFormInputs = {
  25. object: {
  26. value: {
  27. Username: '',
  28. Password: ''
  29. }
  30. }
  31. };
  32. var RegisterFormInputs = {
  33. object: {
  34. value: {
  35. Username: '',
  36. Password: '',
  37. 'Confirm Password': '',
  38. Email: ''
  39. }
  40. }
  41. };
  42. var UserLogin =
  43. /*#__PURE__*/
  44. function (_Module) {
  45. _inherits(UserLogin, _Module);
  46. function UserLogin(props) {
  47. var _this;
  48. _classCallCheck(this, UserLogin);
  49. _this = _possibleConstructorReturn(this, _getPrototypeOf(UserLogin).call(this, props));
  50. _this.state = {
  51. username: '',
  52. password: ''
  53. };
  54. return _this;
  55. }
  56. _createClass(UserLogin, [{
  57. key: "register",
  58. value: function register(data) {
  59. var _this2 = this;
  60. _User.default.register({
  61. email: data.Email,
  62. username: data.Username,
  63. password: data.Password
  64. }).then(function (res) {
  65. _this2.login(data);
  66. });
  67. }
  68. }, {
  69. key: "login",
  70. value: function login(data) {
  71. var _this3 = this;
  72. _User.default.login({
  73. username: data.Username,
  74. password: data.Password
  75. }).then(function (res) {
  76. _this3.user.set(_User.default.me());
  77. _this3.forceUpdate();
  78. });
  79. }
  80. }, {
  81. key: "logout",
  82. value: function logout() {
  83. var _this4 = this;
  84. _User.default.logout().then(function () {
  85. _this4.user.set(_User.default.me());
  86. _this4.forceUpdate();
  87. });
  88. }
  89. }, {
  90. key: "render",
  91. value: function render() {
  92. var _this5 = this;
  93. var logged = _User.default.isLogged();
  94. if (!logged) return _react.default.createElement("div", {
  95. className: "Login"
  96. }, _react.default.createElement("button", {
  97. type: "button",
  98. className: "btn btn-secondary btn-sm",
  99. "data-toggle": "modal",
  100. "data-target": "#loginModal"
  101. }, "Login"), _react.default.createElement(_Modal.default, {
  102. id: "loginModal",
  103. title: "Sign in"
  104. }, this.state.register ? [_react.default.createElement(_Form.default, {
  105. key: 55,
  106. inputs: RegisterFormInputs,
  107. onSubmit: function onSubmit(data) {
  108. return _this5.register(data);
  109. },
  110. buttonText: "Register"
  111. }), _react.default.createElement("a", {
  112. className: "small",
  113. key: 56,
  114. onClick: function onClick() {
  115. return _this5.setState({
  116. register: false
  117. });
  118. }
  119. }, "Already a user ", _react.default.createElement("br", null), " Click to here")] : [_react.default.createElement(_Form.default, {
  120. key: 65,
  121. inputs: LoginFormInputs,
  122. onSubmit: function onSubmit(data) {
  123. return _this5.login(data);
  124. },
  125. buttonText: "Login"
  126. }), _react.default.createElement("a", {
  127. className: "small",
  128. key: 66,
  129. onClick: function onClick() {
  130. return _this5.setState({
  131. register: true
  132. });
  133. }
  134. }, "You don't have an account! ", _react.default.createElement("br", null), " Register here")]), _get(_getPrototypeOf(UserLogin.prototype), "render", this).call(this));else return _react.default.createElement("div", {
  135. className: "Logout"
  136. }, _react.default.createElement("span", null, "Logged in as ", _User.default.getName(), "!"), _react.default.createElement("button", {
  137. type: "button",
  138. className: "btn btn-secondary btn-sm small",
  139. onClick: function onClick() {
  140. return _this5.logout();
  141. }
  142. }, "Logout"), _get(_getPrototypeOf(UserLogin.prototype), "render", this).call(this));
  143. }
  144. }]);
  145. return UserLogin;
  146. }(_Module2.default);
  147. UserLogin.EditPosition = "top-center";
  148. UserLogin.Outputs = {
  149. user: {}
  150. };
  151. var _default = UserLogin;
  152. exports.default = _default;