2019-01-23 00:53:35 +03:00
|
|
|
import React, { Component, Fragment } from 'react';
|
2018-08-30 15:36:26 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Link } from 'gatsby';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
import {
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
FormControl,
|
|
|
|
ControlLabel,
|
|
|
|
Grid,
|
|
|
|
Row,
|
|
|
|
Col,
|
|
|
|
Button
|
2018-09-05 13:39:23 +01:00
|
|
|
} from '@freecodecamp/react-bootstrap';
|
2018-09-14 16:14:35 +01:00
|
|
|
import Helmet from 'react-helmet';
|
2018-09-20 10:22:45 +01:00
|
|
|
import isEmail from 'validator/lib/isEmail';
|
|
|
|
import { isString } from 'lodash';
|
2018-08-30 15:36:26 +01:00
|
|
|
|
|
|
|
import { Spacer } from '../components/helpers';
|
|
|
|
import './update-email.css';
|
2018-09-20 10:22:45 +01:00
|
|
|
import { userSelector } from '../redux';
|
|
|
|
import { updateMyEmail } from '../redux/settings';
|
|
|
|
import { maybeEmailRE } from '../utils';
|
2018-08-30 15:36:26 +01:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
isNewEmail: PropTypes.bool,
|
|
|
|
updateMyEmail: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = createSelector(
|
|
|
|
userSelector,
|
|
|
|
({ email, emailVerified }) => ({
|
|
|
|
isNewEmail: !email || emailVerified
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch =>
|
|
|
|
bindActionCreators({ updateMyEmail }, dispatch);
|
|
|
|
|
|
|
|
class UpdateEmail extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
emailValue: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onChange = this.onChange.bind(this);
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:22:45 +01:00
|
|
|
handleSubmit = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
const { emailValue } = this.state;
|
|
|
|
const { updateMyEmail } = this.props;
|
|
|
|
return updateMyEmail(emailValue);
|
|
|
|
};
|
2018-08-30 15:36:26 +01:00
|
|
|
|
|
|
|
onChange(e) {
|
|
|
|
const change = e.target.value;
|
|
|
|
if (!isString(change)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this.setState({
|
|
|
|
emailValue: change
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getEmailValidationState() {
|
|
|
|
const { emailValue } = this.state;
|
|
|
|
if (maybeEmailRE.test(emailValue)) {
|
|
|
|
return isEmail(emailValue) ? 'success' : 'error';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-09-20 10:22:45 +01:00
|
|
|
const { isNewEmail } = this.props;
|
2018-08-30 15:36:26 +01:00
|
|
|
return (
|
2019-01-23 00:53:35 +03:00
|
|
|
<Fragment>
|
2018-09-14 16:14:35 +01:00
|
|
|
<Helmet>
|
|
|
|
<title>Update your email address | freeCodeCamp.org</title>
|
|
|
|
</Helmet>
|
2018-08-30 15:36:26 +01:00
|
|
|
<Spacer />
|
|
|
|
<h2 className='text-center'>Update your email address here:</h2>
|
|
|
|
<Grid>
|
|
|
|
<Row>
|
|
|
|
<Col sm={6} smOffset={3}>
|
|
|
|
<Row>
|
2018-09-20 10:22:45 +01:00
|
|
|
<Form horizontal={true} onSubmit={this.handleSubmit}>
|
2018-08-30 15:36:26 +01:00
|
|
|
<FormGroup
|
|
|
|
controlId='emailInput'
|
|
|
|
validationState={this.getEmailValidationState()}
|
2019-02-19 01:59:12 +03:00
|
|
|
>
|
2018-08-30 15:36:26 +01:00
|
|
|
<Col
|
|
|
|
className='email-label'
|
|
|
|
componentClass={ControlLabel}
|
|
|
|
sm={2}
|
2019-02-19 01:59:12 +03:00
|
|
|
>
|
2018-08-30 15:36:26 +01:00
|
|
|
Email
|
|
|
|
</Col>
|
|
|
|
<Col sm={10}>
|
|
|
|
<FormControl
|
|
|
|
onChange={this.onChange}
|
|
|
|
placeholder='camperbot@example.com'
|
|
|
|
required={true}
|
|
|
|
type='email'
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
</FormGroup>
|
|
|
|
<Button
|
|
|
|
block={true}
|
|
|
|
bsSize='lg'
|
|
|
|
bsStyle='primary'
|
|
|
|
disabled={this.getEmailValidationState() !== 'success'}
|
|
|
|
type='submit'
|
2019-02-19 01:59:12 +03:00
|
|
|
>
|
2018-08-30 15:36:26 +01:00
|
|
|
{isNewEmail ? 'Update my Email' : 'Verify Email'}
|
|
|
|
</Button>
|
|
|
|
</Form>
|
|
|
|
<p className='text-center'>
|
|
|
|
<Link to='/signout'>Sign out</Link>
|
|
|
|
</p>
|
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
</Grid>
|
2019-01-23 00:53:35 +03:00
|
|
|
</Fragment>
|
2018-08-30 15:36:26 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateEmail.displayName = 'Update-Email';
|
|
|
|
UpdateEmail.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps
|
|
|
|
)(UpdateEmail);
|