'use strict'; import React, {Component} from 'react'; import PropTypes from 'prop-types'; import { Trans, translate } from 'react-i18next'; import { NavButton, requiresAuthenticatedUser, Title, withPageHelpers } from '../lib/page' import { Button, ButtonRow, CheckBox, Fieldset, Form, FormSendMethod, InputField, TextArea, withForm } from '../lib/form'; import {withErrorHandling} from '../lib/error-handling'; import { NamespaceSelect, validateNamespace } from '../lib/namespace'; import {DeleteModalDialog} from "../lib/modals"; import {getMailerTypes} from "./helpers"; import { getSystemSendConfigurationId, MailerType } from "../../../shared/send-configurations"; import mailtrainConfig from 'mailtrainConfig'; @translate() @withForm @withPageHelpers @withErrorHandling @requiresAuthenticatedUser export default class CUD extends Component { constructor(props) { super(props); this.mailerTypes = getMailerTypes(props.t); this.state = {}; this.initForm({ onChangeBeforeValidation: { mailer_type: ::this.onMailerTypeChanged } }); } static propTypes = { action: PropTypes.string.isRequired, wizard: PropTypes.string, entity: PropTypes.object } onMailerTypeChanged(mutState, key, oldType, type) { if (type) { this.mailerTypes[type].afterTypeChange(mutState); } } componentDidMount() { if (this.props.entity) { this.getFormValuesFromEntity(this.props.entity, data => { this.mailerTypes[data.mailer_type].afterLoad(data); data.verpEnabled = !!data.verp_hostname; }); } else { this.populateFormValues({ name: '', description: '', namespace: mailtrainConfig.user.namespace, from_email_overridable: false, from_name_overridable: false, reply_to_overridable: false, subject_overridable: false, verpEnabled: false, verp_hostname: '', x_mailer: '', mailer_type: MailerType.ZONE_MTA, ...this.mailerTypes[MailerType.ZONE_MTA].initData() }); } } localValidateFormValues(state) { const t = this.props.t; const typeKey = state.getIn(['mailer_type', 'value']); if (!state.getIn(['name', 'value'])) { state.setIn(['name', 'error'], t('Name must not be empty')); } else { state.setIn(['name', 'error'], null); } if (!typeKey) { state.setIn(['mailer_type', 'error'], t('Mailer type must be selected')); } else { state.setIn(['mailer_type', 'error'], null); } if (state.getIn(['verpEnabled', 'value']) && !state.getIn(['verp_hostname', 'value'])) { state.setIn(['verp_hostname', 'error'], t('VERP hostname must not be empty')); } else { state.setIn(['verp_hostname', 'error'], null); } validateNamespace(t, state); if (typeKey) { this.mailerTypes[typeKey].validate(state); } } async submitHandler() { const t = this.props.t; let sendMethod, url; if (this.props.entity) { sendMethod = FormSendMethod.PUT; url = `rest/send-configurations/${this.props.entity.id}` } else { sendMethod = FormSendMethod.POST; url = 'rest/send-configurations' } this.disableForm(); this.setFormStatusMessage('info', t('Saving ...')); const submitSuccessful = await this.validateAndSendFormValuesToURL(sendMethod, url, data => { this.mailerTypes[data.mailer_type].beforeSave(data); if (!data.verpEnabled) { data.verp_hostname = null; } }); if (submitSuccessful) { this.navigateToWithFlashMessage('/send-configurations', 'success', t('Send configuration saved')); } else { this.enableForm(); this.setFormStatusMessage('warning', t('There are errors in the form. Please fix them and submit again.')); } } render() { const t = this.props.t; const isEdit = !!this.props.entity; const canDelete = isEdit && this.props.entity.permissions.includes('delete') && this.props.entity.id !== getSystemSendConfigurationId(); const typeKey = this.getFormValue('mailer_type'); let mailerForm = null; if (typeKey) { mailerForm = this.mailerTypes[typeKey].getForm(this); } const verpEnabled = this.getFormValue('verpEnabled'); return (