2017-07-29 19:42:07 +00:00
'use strict' ;
import React , { Component } from 'react' ;
import PropTypes from 'prop-types' ;
import { translate , Trans } from 'react-i18next' ;
2017-07-30 13:22:07 +00:00
import { requiresAuthenticatedUser , withPageHelpers , Title , NavButton } from '../lib/page' ;
2017-07-29 19:42:07 +00:00
import {
2017-07-30 13:22:07 +00:00
withForm , Form , FormSendMethod , InputField , TextArea , TableSelect , ButtonRow , Button ,
Dropdown , StaticField , CheckBox
2017-07-29 19:42:07 +00:00
} from '../lib/form' ;
2017-08-11 16:16:44 +00:00
import { withErrorHandling } from '../lib/error-handling' ;
2017-08-20 21:44:33 +00:00
import { DeleteModalDialog } from '../lib/modals' ;
2017-07-29 19:42:07 +00:00
import { validateNamespace , NamespaceSelect } from '../lib/namespace' ;
import { UnsubscriptionMode } from '../../../shared/lists' ;
2017-08-22 06:15:13 +00:00
import styles from "../lib/styles.scss" ;
2017-09-17 14:36:23 +00:00
import mailtrainConfig from 'mailtrainConfig' ;
2018-04-29 16:13:40 +00:00
import { getMailerTypes } from "../send-configurations/helpers" ;
2017-07-29 19:42:07 +00:00
@ translate ( )
@ withForm
@ withPageHelpers
@ withErrorHandling
@ requiresAuthenticatedUser
export default class CUD extends Component {
constructor ( props ) {
super ( props ) ;
2017-07-30 13:22:07 +00:00
this . state = { } ;
2017-07-29 19:42:07 +00:00
this . initForm ( ) ;
2018-04-29 16:13:40 +00:00
this . mailerTypes = getMailerTypes ( props . t ) ;
2017-07-29 19:42:07 +00:00
}
static propTypes = {
2017-08-11 16:16:44 +00:00
action : PropTypes . string . isRequired ,
entity : PropTypes . object
2017-07-29 19:42:07 +00:00
}
2017-08-11 16:16:44 +00:00
2017-07-29 19:42:07 +00:00
componentDidMount ( ) {
2017-08-11 16:16:44 +00:00
if ( this . props . entity ) {
this . getFormValuesFromEntity ( this . props . entity , data => {
data . form = data . default _form ? 'custom' : 'default' ;
} ) ;
2017-07-29 19:42:07 +00:00
} else {
this . populateFormValues ( {
name : '' ,
description : '' ,
form : 'default' ,
default _form : null ,
public _subscribe : true ,
2018-04-22 15:33:43 +00:00
contact _email : '' ,
homepage : '' ,
2017-07-29 19:42:07 +00:00
unsubscription _mode : UnsubscriptionMode . ONE _STEP ,
2018-09-18 08:30:13 +00:00
namespace : mailtrainConfig . user . namespace ,
to _name : '[FIRST_NAME] [LAST_NAME]'
2017-07-29 19:42:07 +00:00
} ) ;
}
}
localValidateFormValues ( state ) {
const t = this . props . t ;
if ( ! state . getIn ( [ 'name' , 'value' ] ) ) {
state . setIn ( [ 'name' , 'error' ] , t ( 'Name must not be empty' ) ) ;
} else {
state . setIn ( [ 'name' , 'error' ] , null ) ;
}
if ( state . getIn ( [ 'form' , 'value' ] ) === 'custom' && ! state . getIn ( [ 'default_form' , 'value' ] ) ) {
state . setIn ( [ 'default_form' , 'error' ] , t ( 'Custom form must be selected' ) ) ;
} else {
state . setIn ( [ 'default_form' , 'error' ] , null ) ;
}
validateNamespace ( t , state ) ;
}
async submitHandler ( ) {
const t = this . props . t ;
let sendMethod , url ;
2017-08-11 16:16:44 +00:00
if ( this . props . entity ) {
2017-07-29 19:42:07 +00:00
sendMethod = FormSendMethod . PUT ;
2018-05-09 02:07:01 +00:00
url = ` rest/lists/ ${ this . props . entity . id } `
2017-07-29 19:42:07 +00:00
} else {
sendMethod = FormSendMethod . POST ;
2018-05-09 02:07:01 +00:00
url = 'rest/lists'
2017-07-29 19:42:07 +00:00
}
this . disableForm ( ) ;
2017-08-14 20:53:29 +00:00
this . setFormStatusMessage ( 'info' , t ( 'Saving ...' ) ) ;
2017-07-29 19:42:07 +00:00
const submitSuccessful = await this . validateAndSendFormValuesToURL ( sendMethod , url , data => {
if ( data . form === 'default' ) {
data . default _form = null ;
}
2017-07-30 13:22:07 +00:00
delete data . form ;
2017-07-29 19:42:07 +00:00
} ) ;
if ( submitSuccessful ) {
this . navigateToWithFlashMessage ( '/lists' , 'success' , t ( 'List 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 ;
2017-08-11 16:16:44 +00:00
const isEdit = ! ! this . props . entity ;
2017-09-17 14:36:23 +00:00
const canDelete = isEdit && this . props . entity . permissions . includes ( 'delete' ) ;
2017-07-29 19:42:07 +00:00
const unsubcriptionModeOptions = [
{
key : UnsubscriptionMode . ONE _STEP ,
label : t ( 'One-step (i.e. no email with confirmation link)' )
} ,
{
key : UnsubscriptionMode . ONE _STEP _WITH _FORM ,
label : t ( 'One-step with unsubscription form (i.e. no email with confirmation link)' )
} ,
{
key : UnsubscriptionMode . TWO _STEP ,
label : t ( 'Two-step (i.e. an email with confirmation link will be sent)' )
} ,
{
key : UnsubscriptionMode . TWO _STEP _WITH _FORM ,
label : t ( 'Two-step with unsubscription form (i.e. an email with confirmation link will be sent)' )
} ,
{
key : UnsubscriptionMode . MANUAL ,
label : t ( 'Manual (i.e. unsubscription has to be performed by the list administrator)' )
}
] ;
const formsOptions = [
{
key : 'default' ,
label : t ( 'Default Mailtrain Forms' )
} ,
{
key : 'custom' ,
label : t ( 'Custom Forms (select form below)' )
}
2017-08-11 16:16:44 +00:00
] ;
2017-07-29 19:42:07 +00:00
const customFormsColumns = [
{ data : 0 , title : "#" } ,
{ data : 1 , title : t ( 'Name' ) } ,
{ data : 2 , title : t ( 'Description' ) } ,
{ data : 3 , title : t ( 'Namespace' ) }
] ;
2018-04-29 16:13:40 +00:00
const sendConfigurationsColumns = [
{ data : 1 , title : t ( 'Name' ) } ,
{ data : 2 , title : t ( 'Description' ) } ,
{ data : 3 , title : t ( 'Type' ) , render : data => this . mailerTypes [ data ] . typeName } ,
{ data : 5 , title : t ( 'Namespace' ) }
] ;
2017-07-29 19:42:07 +00:00
return (
< div >
2017-09-17 14:36:23 +00:00
{ canDelete &&
2017-07-30 13:22:07 +00:00
< DeleteModalDialog
stateOwner = { this }
2017-08-11 16:16:44 +00:00
visible = { this . props . action === 'delete' }
2018-05-09 02:07:01 +00:00
deleteUrl = { ` rest/lists/ ${ this . props . entity . id } ` }
2018-08-05 04:47:05 +00:00
backUrl = { ` /lists/ ${ this . props . entity . id } /edit ` }
successUrl = "/lists"
2017-07-30 13:22:07 +00:00
deletingMsg = { t ( 'Deleting list ...' ) }
deletedMsg = { t ( 'List deleted' ) } / >
2017-07-29 19:42:07 +00:00
}
2017-08-11 16:16:44 +00:00
< Title > { isEdit ? t ( 'Edit List' ) : t ( 'Create List' ) } < / T i t l e >
2017-07-29 19:42:07 +00:00
< Form stateOwner = { this } onSubmitAsync = { : : this . submitHandler } >
< InputField id = "name" label = { t ( 'Name' ) } / >
2017-08-11 16:16:44 +00:00
{ isEdit &&
2018-02-13 22:50:13 +00:00
< StaticField id = "cid" className = { styles . formDisabled } label = { t ( 'List ID' ) } help = { t ( 'This is the list ID displayed to the subscribers' ) } >
2017-07-29 19:42:07 +00:00
{ this . getFormValue ( 'cid' ) }
< / S t a t i c F i e l d >
}
2018-04-22 15:33:43 +00:00
< TextArea id = "description" label = { t ( 'Description' ) } / >
< InputField id = "contact_email" label = { t ( 'Contact email' ) } help = { t ( 'Contact email used in subscription forms and emails that are sent out. If not filled in, the admin email from the global settings will be used.' ) } / >
< InputField id = "homepage" label = { t ( 'Homepage' ) } help = { t ( 'Homepage URL used in subscription forms and emails that are sent out. If not filled in, the default homepage from global settings will be used.' ) } / >
2018-09-18 08:30:13 +00:00
< InputField id = "to_name" label = { t ( 'Recipients name template' ) } help = { t ( 'Specify using merge tags of this list how to construct full name of the recipient. This full name is used as "To" header when sending emails.' ) } / >
2018-05-09 02:07:01 +00:00
< TableSelect id = "send_configuration" label = { t ( 'Send configuration' ) } withHeader dropdown dataUrl = 'rest/send-configurations-table' columns = { sendConfigurationsColumns } selectionLabelIndex = { 1 } help = { t ( 'Send configuration that will be used for sending out subscription-related emails.' ) } / >
2017-07-29 19:42:07 +00:00
< NamespaceSelect / >
< Dropdown id = "form" label = { t ( 'Forms' ) } options = { formsOptions } help = { t ( 'Web and email forms and templates used in subscription management process.' ) } / >
{ this . getFormValue ( 'form' ) === 'custom' &&
2018-05-09 02:07:01 +00:00
< TableSelect id = "default_form" label = { t ( 'Custom forms' ) } withHeader dropdown dataUrl = 'rest/forms-table' columns = { customFormsColumns } selectionLabelIndex = { 1 } help = { < Trans > The custom form used for this list . You can create a form < a href = { ` /lists/forms/create/ ${ this . props . entity . id } ` } > here < / a > . < / T r a n s > } / >
2017-07-29 19:42:07 +00:00
}
< CheckBox id = "public_subscribe" label = { t ( 'Subscription' ) } text = { t ( 'Allow public users to subscribe themselves' ) } / >
< Dropdown id = "unsubscription_mode" label = { t ( 'Unsubscription' ) } options = { unsubcriptionModeOptions } help = { t ( 'Select how an unsuscription request by subscriber is handled.' ) } / >
< ButtonRow >
< Button type = "submit" className = "btn-primary" icon = "ok" label = { t ( 'Save' ) } / >
2017-09-17 14:36:23 +00:00
{ canDelete && < NavButton className = "btn-danger" icon = "remove" label = { t ( 'Delete' ) } linkTo = { ` /lists/ ${ this . props . entity . id } /delete ` } / > }
2017-07-29 19:42:07 +00:00
< / B u t t o n R o w >
< / F o r m >
< / d i v >
) ;
}
}