'use strict'; import React, {Component} from 'react'; import PropTypes from 'prop-types'; import {Trans} from 'react-i18next'; import {withTranslation} from '../lib/i18n'; import { LinkButton, requiresAuthenticatedUser, Title, withPageHelpers } from '../lib/page'; import { Button, ButtonRow, CheckBox, Dropdown, Form, FormSendMethod, InputField, StaticField, TableSelect, TextArea, withForm } from '../lib/form'; import {withErrorHandling} from '../lib/error-handling'; import {DeleteModalDialog} from '../lib/modals'; import { NamespaceSelect, validateNamespace } from '../lib/namespace'; import {UnsubscriptionMode, FieldWizard} from '../../../shared/lists'; import styles from "../lib/styles.scss"; import mailtrainConfig from 'mailtrainConfig'; import {getMailerTypes} from "../send-configurations/helpers"; import {withComponentMixins} from "../lib/decorator-helpers"; @withComponentMixins([ withTranslation, withForm, withErrorHandling, withPageHelpers, requiresAuthenticatedUser ]) export default class CUD extends Component { constructor(props) { super(props); this.state = {}; this.initForm(); this.mailerTypes = getMailerTypes(props.t); } static propTypes = { action: PropTypes.string.isRequired, entity: PropTypes.object } getFormValuesMutator(data) { data.form = data.default_form ? 'custom' : 'default'; data.listunsubscribe_disabled = !!data.listunsubscribe_disabled; } componentDidMount() { if (this.props.entity) { this.getFormValuesFromEntity(this.props.entity, ::this.getFormValuesMutator); } else { this.populateFormValues({ name: '', description: '', form: 'default', default_form: null, public_subscribe: true, contact_email: '', homepage: '', unsubscription_mode: UnsubscriptionMode.ONE_STEP, namespace: mailtrainConfig.user.namespace, to_name: '', fieldWizard: FieldWizard.FIRST_LAST_NAME, send_configuration: null, listunsubscribe_disabled: false }); } } localValidateFormValues(state) { const t = this.props.t; if (!state.getIn(['name', 'value'])) { state.setIn(['name', 'error'], t('nameMustNotBeEmpty')); } else { state.setIn(['name', 'error'], null); } if (!state.getIn(['send_configuration', 'value'])) { state.setIn(['send_configuration', 'error'], t('sendConfigurationMustBeSelected')); } else { state.setIn(['send_configuration', 'error'], null); } if (state.getIn(['form', 'value']) === 'custom' && !state.getIn(['default_form', 'value'])) { state.setIn(['default_form', 'error'], t('customFormMustBeSelected')); } else { state.setIn(['default_form', 'error'], null); } validateNamespace(t, state); } async submitHandler(submitAndLeave) { const t = this.props.t; let sendMethod, url; if (this.props.entity) { sendMethod = FormSendMethod.PUT; url = `rest/lists/${this.props.entity.id}` } else { sendMethod = FormSendMethod.POST; url = 'rest/lists' } this.disableForm(); this.setFormStatusMessage('info', t('saving')); const submitResult = await this.validateAndSendFormValuesToURL(sendMethod, url, data => { if (data.form === 'default') { data.default_form = null; } delete data.form; if (data.fieldWizard === FieldWizard.FIRST_LAST_NAME || data.fieldWizard === FieldWizard.NAME) { data.to_name = null; } }); if (submitResult) { if (this.props.entity) { if (submitAndLeave) { this.navigateToWithFlashMessage('/lists', 'success', t('List updated')); } else { await this.getFormValuesFromURL(`rest/lists/${this.props.entity.id}`, ::this.getFormValuesMutator); this.enableForm(); this.setFormStatusMessage('success', t('List updated')); } } else { if (submitAndLeave) { this.navigateToWithFlashMessage('/lists', 'success', t('List created')); } else { this.navigateToWithFlashMessage(`/lists/${submitResult}/edit`, 'success', t('List created')); } } } else { this.enableForm(); this.setFormStatusMessage('warning', t('thereAreErrorsInTheFormPleaseFixThemAnd')); } } render() { const t = this.props.t; const isEdit = !!this.props.entity; const canDelete = isEdit && this.props.entity.permissions.includes('delete'); const unsubcriptionModeOptions = [ { key: UnsubscriptionMode.ONE_STEP, label: t('onestepIeNoEmailWithConfirmationLink') }, { key: UnsubscriptionMode.ONE_STEP_WITH_FORM, label: t('onestepWithUnsubscriptionFormIeNoEmail') }, { key: UnsubscriptionMode.TWO_STEP, label: t('twostepIeAnEmailWithConfirmationLinkWill') }, { key: UnsubscriptionMode.TWO_STEP_WITH_FORM, label: t('twostepWithUnsubscriptionFormIeAnEmail') }, { key: UnsubscriptionMode.MANUAL, label: t('manualIeUnsubscriptionHasToBePerformedBy') } ]; const formsOptions = [ { key: 'default', label: t('defaultMailtrainForms') }, { key: 'custom', label: t('customFormsSelectFormBelow') } ]; const customFormsColumns = [ {data: 0, title: "#"}, {data: 1, title: t('name')}, {data: 2, title: t('description')}, {data: 3, title: t('namespace')} ]; const sendConfigurationsColumns = [ { data: 1, title: t('name') }, { data: 2, title: t('id'), render: data => {data} }, { data: 3, title: t('description') }, { data: 4, title: t('type'), render: data => this.mailerTypes[data].typeName }, { data: 6, title: t('namespace') } ]; let toNameFields; if (isEdit) { toNameFields = ; } else { const fieldWizardOptions = [ {key: FieldWizard.NONE, label: t('Empty / Custom (no fields)')}, {key: FieldWizard.NAME, label: t('Name (one field)')}, {key: FieldWizard.FIRST_LAST_NAME, label: t('First name and Last name (two fields)')}, ]; const fieldWizardValue = this.getFormValue('fieldWizard'); const fieldWizardSelector = if (fieldWizardValue === FieldWizard.NONE) { toNameFields = ( <> {fieldWizardSelector} ); } else { toNameFields = fieldWizardSelector; } } return (
{canDelete && } {isEdit ? t('editList') : t('createList')}
{isEdit && {this.getFormValue('cid')} }