import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; import { Link } from 'gatsby'; import { HelpBlock, Alert, FormGroup, ControlLabel, FormControl, Button } from '@freecodecamp/react-bootstrap'; import isEmail from 'validator/lib/isEmail'; import { Trans, withTranslation } from 'react-i18next'; import { updateMyEmail } from '../../redux/settings'; import { maybeEmailRE } from '../../utils'; import FullWidthRow from '../helpers/FullWidthRow'; import Spacer from '../helpers/Spacer'; import SectionHeader from './SectionHeader'; import BlockSaveButton from '../helpers/form/BlockSaveButton'; import ToggleSetting from './ToggleSetting'; const mapStateToProps = () => ({}); const mapDispatchToProps = dispatch => bindActionCreators({ updateMyEmail }, dispatch); const propTypes = { email: PropTypes.string, isEmailVerified: PropTypes.bool, sendQuincyEmail: PropTypes.bool, t: PropTypes.func.isRequired, updateMyEmail: PropTypes.func.isRequired, updateQuincyEmail: PropTypes.func.isRequired }; export function UpdateEmailButton() { const { t } = this.props; return ( ); } class EmailSettings extends Component { constructor(props) { super(props); this.state = { emailForm: { currentEmail: props.email, newEmail: '', confirmNewEmail: '', isPristine: true } }; } handleSubmit = e => { e.preventDefault(); const { emailForm: { newEmail } } = this.state; const { updateMyEmail } = this.props; return updateMyEmail(newEmail); }; createHandleEmailFormChange = key => e => { e.preventDefault(); const userInput = e.target.value.slice(); return this.setState(state => ({ emailForm: { ...state.emailForm, [key]: userInput, isPristine: userInput === state.emailForm.currentEmail } })); }; getValidationForNewEmail = () => { const { emailForm: { newEmail, currentEmail } } = this.state; const { t } = this.props; if (!maybeEmailRE.test(newEmail)) { return { state: null, message: '' }; } if (newEmail === currentEmail) { return { state: 'error', message: t('validation.same-email') }; } if (isEmail(newEmail)) { return { state: 'success', message: '' }; } else { return { state: 'error', message: t('validation.invalid-email') }; } }; getValidationForConfirmEmail = () => { const { emailForm: { confirmNewEmail, newEmail } } = this.state; const { t } = this.props; if (!maybeEmailRE.test(newEmail)) { return { state: null, message: '' }; } const isMatch = newEmail === confirmNewEmail; if (maybeEmailRE.test(confirmNewEmail)) { return { state: isMatch ? 'success' : 'error', message: isMatch ? '' : t('validation.email-mismatch') }; } else { return { state: null, message: '' }; } }; render() { const { emailForm: { newEmail, confirmNewEmail, currentEmail, isPristine } } = this.state; const { isEmailVerified, updateQuincyEmail, sendQuincyEmail, t } = this.props; const { state: newEmailValidation, message: newEmailValidationMessage } = this.getValidationForNewEmail(); const { state: confirmEmailValidation, message: confirmEmailValidationMessage } = this.getValidationForConfirmEmail(); if (!currentEmail) { return (

{t('settings.email.missing')}

); } return (
{t('settings.email.heading')} {isEmailVerified ? null : ( {t('settings.email.not-verified')}
)}
{t('settings.email.current')} {currentEmail} {t('settings.email.new')} {newEmailValidationMessage ? ( {newEmailValidationMessage} ) : null} {t('settings.email.confirm')} {confirmEmailValidationMessage ? ( {confirmEmailValidationMessage} ) : null}
updateQuincyEmail(!sendQuincyEmail)} />
); } } EmailSettings.displayName = 'EmailSettings'; EmailSettings.propTypes = propTypes; export default connect( mapStateToProps, mapDispatchToProps )(withTranslation()(EmailSettings));