import React from 'react'; import isEqual from 'lodash/isEqual'; import PropTypes from 'prop-types'; import cn from 'classnames'; import styles from './AddEmailAddressDialog.css'; import { Button, Dialog, Icon } from 'plugin-api/beta/client/components/ui'; import validate from 'coral-framework/helpers/validate'; import errorMsj from 'coral-framework/helpers/error'; import InputField from './InputField'; import { getErrorMessages } from 'coral-framework/utils'; const initialState = { showErrors: false, errors: {}, formData: {}, }; class AddEmailAddressDialog extends React.Component { state = initialState; validKeys = ['emailAddress', 'confirmEmailAddress']; onChange = e => { const { name, value, type } = e.target; this.setState( state => ({ formData: { ...state.formData, [name]: value, }, }), () => { this.fieldValidation(value, type, name); } ); }; fieldValidation = (value, type, name) => { if (!value.length) { this.addError({ [name]: 'Field is required', }); } else if (!validate[type](value)) { this.addError({ [name]: errorMsj[type] }); } else { this.removeError(name); } }; addError = err => { this.setState(({ errors }) => ({ errors: { ...errors, ...err }, })); }; removeError = errKey => { this.setState(state => { const { [errKey]: _, ...errors } = state.errors; return { errors, }; }); }; hasError = err => { return Object.keys(this.state.errors).indexOf(err) !== -1; }; formHasError = () => { const formHasErrors = !!Object.keys(this.state.errors).length; const formIncomplete = !isEqual( Object.keys(this.state.formData), this.validKeys ); return formHasErrors || formIncomplete; }; showError = () => { this.setState({ showError: true, }); }; confirmChanges = async () => { if (this.formHasError()) { this.showError(); return; } const { emailAddress } = this.state.formData; const { attachLocalAuth } = this.props; try { await attachLocalAuth({ email: emailAddress, }); this.props.notify('success', 'Email Added!'); } catch (err) { this.props.notify('error', getErrorMessages(err)); } }; render() { const { errors, formData, showErrors } = this.state; return (

Add an Email Address

For your added security, we require users to add an email address to their accounts. Your email address will be used to:

); } } AddEmailAddressDialog.propTypes = { attachLocalAuth: PropTypes.func.isRequired, notify: PropTypes.func.isRequired, }; export default AddEmailAddressDialog;