import React from 'react'; import { connect } from 'react-redux'; import { defineMessages, injectIntl } from 'react-intl'; import ImmutablePureComponent from 'react-immutable-pure-component'; import PropTypes from 'prop-types'; import Column from '../ui/components/column'; import { SimpleForm, SimpleInput, FieldsGroup, TextInput, } from 'soapbox/features/forms'; import { changeEmail, changePassword } from 'soapbox/actions/security'; import { showAlert } from 'soapbox/actions/alerts'; const messages = defineMessages({ heading: { id: 'column.security', defaultMessage: 'Security' }, submit: { id: 'security.submit', defaultMessage: 'Save changes' }, updateEmailSuccess: { id: 'security.update_email.success', defaultMessage: 'Email successfully updated.' }, updateEmailFail: { id: 'security.update_email.fail', defaultMessage: 'Update email failed.' }, updatePasswordSuccess: { id: 'security.update_password.success', defaultMessage: 'Password successfully updated.' }, updatePasswordFail: { id: 'security.update_password.fail', defaultMessage: 'Update password failed.' }, }); export default @injectIntl class SecurityForm extends ImmutablePureComponent { render() { const { intl } = this.props; return ( ); } } @connect() @injectIntl class ChangeEmailForm extends ImmutablePureComponent { static propTypes = { email: PropTypes.string, dispatch: PropTypes.func.isRequired, intl: PropTypes.object.isRequired, }; state = { email: '', password: '', isLoading: false, } handleInputChange = e => { this.setState({ [e.target.name]: e.target.value }); } handleSubmit = e => { const { email, password } = this.state; const { dispatch, intl } = this.props; this.setState({ isLoading: true }); return dispatch(changeEmail(email, password)).then(() => { this.setState({ email: '', password: '' }); // TODO: Maybe redirect user dispatch(showAlert('', intl.formatMessage(messages.updateEmailSuccess))); }).catch(error => { this.setState({ password: '' }); dispatch(showAlert('', intl.formatMessage(messages.updateEmailFail))); }).then(() => { this.setState({ isLoading: false }); }); } render() { const { intl } = this.props; return ( {intl.formatMessage(messages.submit)} ); } } @connect() @injectIntl class ChangePasswordForm extends ImmutablePureComponent { static propTypes = { dispatch: PropTypes.func.isRequired, intl: PropTypes.object.isRequired, }; state = { oldPassword: '', newPassword: '', confirmation: '', isLoading: false, } handleInputChange = e => { this.setState({ [e.target.name]: e.target.value }); } clearForm = () => { this.setState({ oldPassword: '', newPassword: '', confirmation: '' }); } handleSubmit = e => { const { oldPassword, newPassword, confirmation } = this.state; const { dispatch, intl } = this.props; this.setState({ isLoading: true }); return dispatch(changePassword(oldPassword, newPassword, confirmation)).then(() => { this.clearForm(); // TODO: Maybe redirect user dispatch(showAlert('', intl.formatMessage(messages.updatePasswordSuccess))); }).catch(error => { this.clearForm(); dispatch(showAlert('', intl.formatMessage(messages.updatePasswordFail))); }).then(() => { this.setState({ isLoading: false }); }); } render() { const { intl } = this.props; return ( {intl.formatMessage(messages.submit)} ); } }