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 { 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, updateMyEmail: PropTypes.func.isRequired, updateQuincyEmail: PropTypes.func.isRequired }; export function UpdateEmailButton() { 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; if (!maybeEmailRE.test(newEmail)) { return { state: null, message: '' }; } if (newEmail === currentEmail) { return { state: 'error', message: 'This email is the same as your current email' }; } if (isEmail(newEmail)) { return { state: 'success', message: '' }; } else { return { state: 'warning', message: 'We could not validate your email correctly, ' + 'please ensure it is correct' }; } }; getValidationForConfirmEmail = () => { const { emailForm: { confirmNewEmail, newEmail } } = this.state; if (!maybeEmailRE.test(newEmail)) { return { state: null, message: '' }; } const isMatch = newEmail === confirmNewEmail; if (maybeEmailRE.test(confirmNewEmail)) { return { state: isMatch ? 'success' : 'error', message: isMatch ? '' : 'Both new email addresses must be the same' }; } else { return { state: null, message: '' }; } }; render() { const { emailForm: { newEmail, confirmNewEmail, currentEmail, isPristine } } = this.state; const { isEmailVerified, updateQuincyEmail, sendQuincyEmail } = this.props; const { state: newEmailValidation, message: newEmailValidationMessage } = this.getValidationForNewEmail(); const { state: confirmEmailValidation, message: confirmEmailValidationMessage } = this.getValidationForConfirmEmail(); if (!currentEmail) { return (
You do not have an email associated with this account.