@risecx/carespace-chat-ui
Version:
carespace-chat-ui React component
197 lines (167 loc) • 7.53 kB
JavaScript
import _regeneratorRuntime from "@babel/runtime/regenerator";
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
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; }
import React, { Fragment, useReducer } from 'react';
import { reactLocalStorage } from 'reactjs-localstorage';
import { v4 } from 'uuid';
import Register from './Register';
import Login from './Login';
import Header from '../Header';
import ForgotPassword from './ForgotPassword';
import carespaceLogo from './../../assets/carespace-white-logo.png';
import './auth.css';
var Auth = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(Auth, _React$Component);
function Auth(props) {
var _this;
_this = _React$Component.call(this, props) || this;
_defineProperty(_assertThisInitialized(_this), "onLoginClickHandler", function () {
_this.setState({
isAuth: false,
toLogin: false,
toRegister: false,
toForgetPassword: false
});
});
_defineProperty(_assertThisInitialized(_this), "onLogoutClickHandler", function () {
_this.setState({
isAuth: false,
toLogin: false,
toRegister: false,
toForgetPassword: false
});
_this.props.onAuth({
isAnonymous: true,
loggedIn: false
});
});
_this.state = {
isAuth: false,
toRegister: false,
toLogin: false,
toForgetPassword: false,
carespaceLogoUrl: carespaceLogo
};
return _this;
}
var _proto = Auth.prototype;
_proto.componentDidMount = function componentDidMount() {
if (this.__hasRegisteredUser) this.onChangeAuthStatus();
};
_proto.onClickLogin = function onClickLogin() {
this.setState({
toLogin: !this.state.toLogin
});
};
_proto.onClickForgotPassword = function onClickForgotPassword() {
this.setState({
toForgetPassword: !this.state.toForgetPassword
});
};
_proto.onClickRegister = function onClickRegister() {
this.setState({
toRegister: !this.state.toRegister
});
};
_proto.onClickAnonymous = function onClickAnonymous() {
reactLocalStorage.set('userId', v4());
reactLocalStorage.set('isAnonymous', true);
this.setState({
isAuth: true
});
this.props.onAuth({
isAnonymous: true,
loggedIn: true
});
};
_proto.onChangeAuthStatus = /*#__PURE__*/function () {
var _onChangeAuthStatus = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(loginData) {
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
this.setState({
isAuth: true
});
this.props.onAuth({
isAnonymous: false,
loggedIn: true,
loginData: loginData
});
case 2:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function onChangeAuthStatus(_x) {
return _onChangeAuthStatus.apply(this, arguments);
}
return onChangeAuthStatus;
}();
_proto.render = function render() {
var _this$state = this.state,
isAuth = _this$state.isAuth,
toRegister = _this$state.toRegister,
toLogin = _this$state.toLogin,
toForgetPassword = _this$state.toForgetPassword; //const { helloMessage } = this.props.clientConfig;
var _this$props$agentProf = this.props.agentProfile,
teamName = _this$props$agentProf.teamName,
avatarImageUrl = _this$props$agentProf.avatarImageUrl,
logoUrl = _this$props$agentProf.logoUrl;
if (isAuth) return this.props.children;
return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(Header, {
isAuth: isAuth,
teamName: teamName,
avatarImageUrl: avatarImageUrl,
onClose: this.props.onClose,
onLoginClick: this.onLoginClickHandler,
onLogoutClick: this.onLogoutClickHandler
}), /*#__PURE__*/React.createElement("div", {
className: "sc-auth--component"
}, toRegister === false && toLogin === false && /*#__PURE__*/React.createElement("div", {
className: "content-container"
}, /*#__PURE__*/React.createElement("img", {
className: "auth-logo",
src: logoUrl || carespaceLogo,
alt: ""
}), /*#__PURE__*/React.createElement("div", {
className: "buttons"
}, /*#__PURE__*/React.createElement("button", {
className: "sc-button button outline",
onClick: this.onClickLogin.bind(this)
}, "Sign In"), /*#__PURE__*/React.createElement("button", {
className: "sc-button button outline",
onClick: this.onClickRegister.bind(this)
}, "Register")), /*#__PURE__*/React.createElement("a", {
className: "btn-anonymous",
onClick: this.onClickAnonymous.bind(this)
}, "Skip")), toLogin && !toForgetPassword && /*#__PURE__*/React.createElement(Login, {
clientConfig: this.props.clientConfig,
onClickLogin: this.onClickLogin.bind(this),
onForgotPassword: this.onClickForgotPassword.bind(this),
onChangeAuthStatus: this.onChangeAuthStatus.bind(this)
}), toRegister && !toForgetPassword && /*#__PURE__*/React.createElement(Register, {
clientConfig: this.props.clientConfig,
onClickRegister: this.onClickRegister.bind(this),
onChangeAuthStatus: this.onChangeAuthStatus.bind(this)
}), toForgetPassword && /*#__PURE__*/React.createElement(ForgotPassword, {
clientConfig: this.props.clientConfig,
onCancel: this.onClickForgotPassword.bind(this)
})));
};
_createClass(Auth, [{
key: "__hasRegisteredUser",
get: function get() {
return !!reactLocalStorage.get('userId');
}
}]);
return Auth;
}(React.Component);
export default Auth;