'use strict'; import React, {Component} from 'react'; import {withTranslation} from '../lib/i18n'; import {Trans} from 'react-i18next'; import {requiresAuthenticatedUser, Title, withPageHelpers} from '../lib/page' import { Button, ButtonRow, Fieldset, filterData, Form, FormSendMethod, InputField, withForm, withFormErrorHandlers } from '../lib/form'; import {withAsyncErrorHandler, withErrorHandling} from '../lib/error-handling'; import passwordValidator from '../../../shared/password-validator'; import interoperableErrors from '../../../shared/interoperable-errors'; import mailtrainConfig from 'mailtrainConfig'; import {withComponentMixins} from "../lib/decorator-helpers"; @withComponentMixins([ withTranslation, withForm, withErrorHandling, withPageHelpers, requiresAuthenticatedUser ]) export default class Account extends Component { constructor(props) { super(props); this.passwordValidator = passwordValidator(props.t); this.state = {}; this.initForm({ serverValidation: { url: 'rest/account-validate', changed: ['email', 'currentPassword'] } }); } getFormValuesMutator(data) { data.password = ''; data.password2 = ''; data.currentPassword = ''; } submitFormValuesMutator(data) { return filterData(data, ['name', 'email', 'password', 'currentPassword']); } @withAsyncErrorHandler async loadFormValues() { await this.getFormValuesFromURL('rest/account'); } componentDidMount() { // noinspection JSIgnoredPromiseFromCall this.loadFormValues(); } localValidateFormValues(state) { const t = this.props.t; const email = state.getIn(['email', 'value']); const emailServerValidation = state.getIn(['email', 'serverValidation']); if (!email) { state.setIn(['email', 'error'], t('emailMustNotBeEmpty')); } else if (emailServerValidation && emailServerValidation.invalid) { state.setIn(['email', 'error'], t('invalidEmailAddress')); } else if (emailServerValidation && emailServerValidation.exists) { state.setIn(['email', 'error'], t('theEmailIsAlreadyAssociatedWithAnother')); } else if (!emailServerValidation) { state.setIn(['email', 'error'], t('validationIsInProgress')); } else { state.setIn(['email', 'error'], null); } const name = state.getIn(['name', 'value']); if (!name) { state.setIn(['name', 'error'], t('fullNameMustNotBeEmpty')); } else { state.setIn(['name', 'error'], null); } const password = state.getIn(['password', 'value']) || ''; const password2 = state.getIn(['password2', 'value']) || ''; const currentPassword = state.getIn(['currentPassword', 'value']) || ''; let passwordMsgs = []; if (password || currentPassword) { const passwordResults = this.passwordValidator.test(password); passwordMsgs.push(...passwordResults.errors); const currentPasswordServerValidation = state.getIn(['currentPassword', 'serverValidation']); if (!currentPassword) { state.setIn(['currentPassword', 'error'], t('currentPasswordMustNotBeEmpty')); } else if (currentPasswordServerValidation && currentPasswordServerValidation.incorrect) { state.setIn(['currentPassword', 'error'], t('incorrectPassword')); } else if (!currentPasswordServerValidation) { state.setIn(['email', 'error'], t('validationIsInProgress')); } else { state.setIn(['currentPassword', 'error'], null); } } if (passwordMsgs.length > 1) { passwordMsgs = passwordMsgs.map((msg, idx) =>
{t('accountManagementIsNotPossibleBecause')}
{mailtrainConfig.externalPasswordResetLink &&