UNPKG

@bigfishtv/cockpit

Version:

106 lines (89 loc) 4.14 kB
var _dec, _class; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import React, { Component } from 'react'; import { Fieldset, createValue } from '@bigfishtv/react-forms'; import { validationErrors } from 'react-forms-cakephp'; import { connect } from 'react-redux'; import ReCAPTCHA from 'react-google-recaptcha'; import { Modal, Field, xhrUtils, Icon, viewerActions, Button, invalidateExternalErrorList } from '../../index'; var LOGIN_URL = '/admin/tank/users/login.json'; var LoginModal = (_dec = connect(function (_ref) { var viewer = _ref.viewer; return { viewer: viewer }; }), _dec(_class = function (_Component) { _inherits(LoginModal, _Component); function LoginModal(props) { _classCallCheck(this, LoginModal); var _this = _possibleConstructorReturn(this, _Component.call(this, props)); _this.onChange = function (formValue) { formValue = invalidateExternalErrorList(formValue, _this.state.formValue); _this.setState({ formValue: formValue }); }; _this.handleLogin = function (event) { event.preventDefault(); xhrUtils.post({ url: LOGIN_URL, data: _this.state.formValue.value, quietSuccess: true, failureMessage: 'Please check your login details', callback: function callback(viewer) { _this.props.dispatch(viewerActions.setViewer(viewer)); _this.props.closeModal(); }, callbackError: function callbackError(response) { if (response.data && response.data.errors) { _this.state.formValue.updateExternalErrorList(validationErrors(response.data.errors)); } if (response.data && response.data.sitekey) { _this.setState({ sitekey: response.data.sitekey }); } _this.setState({ invalidCount: _this.state.invalidCount + 1 }); } }); }; _this.state = { formValue: createValue({ value: { email: props.viewer.email || '' }, onChange: _this.onChange }), sitekey: '', // filled in automatically invalidCount: 0 }; return _this; } LoginModal.prototype.render = function render() { return React.createElement( 'form', { onSubmit: this.handleLogin }, React.createElement( Modal, { title: 'Login', size: 'small', ModalActions: function ModalActions() { return React.createElement(Button, { type: 'submit', text: 'Login', style: 'primary' }); }, ModalToolbar: function ModalToolbar() { return React.createElement(Icon, { name: 'lock', className: 'text-grey' }); } }, React.createElement( Fieldset, { formValue: this.state.formValue }, React.createElement(Field, { select: 'email', type: 'email', autoComplete: 'username', autoFocus: true }), React.createElement(Field, { select: 'password', type: 'password', autoComplete: 'current-password' }), this.state.sitekey && React.createElement( Field, { key: this.state.invalidCount, select: 'g-recaptcha-response', label: false }, React.createElement(ReCAPTCHAWrapper, { sitekey: this.state.sitekey }) ) ) ) ); }; return LoginModal; }(Component)) || _class); export { LoginModal as default }; var ReCAPTCHAWrapper = function ReCAPTCHAWrapper(_ref2) { var onChange = _ref2.onChange, sitekey = _ref2.sitekey; return React.createElement(ReCAPTCHA, { sitekey: sitekey, onChange: onChange }); };