import React from 'react' import PropTypes from 'prop-types' import { regExpPattern } from 'regexp-pattern' import { api } from 'src/API' import { WaitModal, MessageModal } from '../Modal' import { Image, Link, Text, Row, Column, BoundInput, BoundCheckbox, BoundButton } from 'ui' import headerLogo from 'images/deighton.png' import { versionInfo } from '../version' import { FormBinder } from 'react-form-binder' import { reactAutoBind } from 'auto-bind2' import { sizeInfo } from 'ui/style' export class Login extends React.Component { static propTypes = { history: PropTypes.oneOfType([PropTypes.array, PropTypes.object]) } static bindings = { email: { alwaysGet: true, isValid: (r, v) => (regExpPattern.email.test(v)) }, password: { alwaysGet: true, isValid: (r, v) => (v !== '') }, rememberMe: { alwaysGet: true, initValue: true }, submit: { nonValue: true, isDisabled: (r) => (!r.anyModified || !r.allValid) } } constructor(props) { super(props) reactAutoBind(this) this.state = { waitModal: false, messageModal: null, binder: new FormBinder({}, Login.bindings) } } handleSubmit(e) { e.preventDefault() e.stopPropagation() if (this.state.messageModal) { this.setState({ messageModal: null }) return } else if (this.state.waitModal) { return } let obj = this.state.binder.getModifiedFieldValues() if (obj) { this.setState({ waitModal: true }) api.login(obj.email, obj.password, obj.rememberMe).then((user) => { this.setState({ waitModal: false }) if (this.props.history) { const landing = user.role === 'broker' ? '/broker-dashboard' : 'dashboard' let url = new URLSearchParams(window.location.search).get('redirect') || landing try { this.props.history.replace(url) } catch (error) { this.props.history.replace('/') } } }).catch((error) => { this.setState({ waitModal: false, messageModal: { icon: 'hold', message: `Unable to login`, detail: error.message } }) }) } } handleMessageModalDismiss() { this.setState({ messageModal: null, binder: new FormBinder({ email: this.state.binder.getFieldValue('email') }, Login.bindings) }) const elems = document.getElementsByName('password') if (elems) { elems[0].focus() } } render() { const { messageModal, waitModal } = this.state return (
Forgot your password? Please contact {versionInfo.supportEmail} to request login credentials.
) } }