Load MailChimp Lists

[MAILPOET-1808]
This commit is contained in:
Pavel Dohnal
2019-04-11 16:00:28 +02:00
committed by M. Shull
parent 1a4b0f2785
commit 8a3077db0b
3 changed files with 86 additions and 48 deletions

View File

@@ -117,52 +117,7 @@ jQuery(document).ready(() => {
mailChimpListsContainerElement.show(); mailChimpListsContainerElement.show();
} }
/*
* MailChimp
*/
mailChimpKeyInputElement.keyup((event) => {
if (event.currentTarget.value.trim() === ''
|| !/[a-zA-Z0-9]{32}-/.exec(event.currentTarget.value.trim())) {
mailChimpListsContainerElement.hide();
jQuery('.mailpoet_mailchimp-key-status')
.html('')
.removeClass('mailpoet_mailchimp-ok mailpoet_mailchimp-error');
toggleNextStepButton(mailChimpProcessButtonElement, 'off');
}
});
mailChimpKeyVerifyButtonElement.click(() => {
MailPoet.Modal.loading(true);
MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
endpoint: 'importExport',
action: 'getMailChimpLists',
data: {
api_key: mailChimpKeyInputElement.val(),
},
}).always(() => {
MailPoet.Modal.loading(false);
}).done((response) => {
jQuery('.mailpoet_mailchimp-key-status')
.html('')
.removeClass()
.addClass('mailpoet_mailchimp-key-status mailpoet_mailchimp-ok');
if (response.data.length === 0) {
jQuery('.mailpoet_mailchimp-key-status').html(MailPoet.I18n.t('noMailChimpLists'));
mailChimpListsContainerElement.hide();
toggleNextStepButton(mailChimpProcessButtonElement, 'off');
} else {
displayMailChimpLists(response.data);
}
}).fail((response) => {
if (response.errors.length > 0) {
MailPoet.Notice.error(
response.errors.map(error => error.message),
{ scroll: true }
);
}
});
});
mailChimpProcessButtonElement.click(() => { mailChimpProcessButtonElement.click(() => {
if (mailChimpProcessButtonElement.closest('table a').hasClass('button-disabled')) { if (mailChimpProcessButtonElement.closest('table a').hasClass('button-disabled')) {

View File

@@ -1,9 +1,84 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import Selection from '../../../../form/fields/selection.jsx';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import MailPoet from 'mailpoet'; import MailPoet from 'mailpoet';
import classNames from 'classnames';
const MethodMailChimp = ({ setInputValid, setInputInvalid, onValueChange }) => { const MethodMailChimp = ({ setInputValid, setInputInvalid, onValueChange }) => {
const [key, setKey] = useState(''); const [key, setKey] = useState('');
const [mailChimpLoadedLists, setMailChimpLoadedLists] = useState(undefined);
const [selectedLists, setSelectedLists] = useState([]);
const keyChange = (e) => {
setKey(e.target.value);
if (e.target.value.trim() === '') {
setMailChimpLoadedLists(undefined);
setInputInvalid();
}
};
const listSelected = (lists) => {
setSelectedLists(lists);
if (Array.isArray(lists) && lists.length === 0) {
setInputInvalid();
} else {
setInputValid();
}
};
const verifyButtonClicked = () => {
MailPoet.Modal.loading(true);
MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
endpoint: 'importExport',
action: 'getMailChimpLists',
data: {
api_key: key,
},
}).always(() => {
MailPoet.Modal.loading(false);
}).done((response) => {
setMailChimpLoadedLists(response.data);
if (response.data.length === 0) {
setInputInvalid();
}
}).fail((response) => {
setInputInvalid();
if (response.errors.length > 0) {
MailPoet.Notice.error(
response.errors.map(error => error.message),
{ scroll: true }
);
}
});
};
const showListsSelection = () => {
if (!mailChimpLoadedLists) return null;
return (
<>
<div>
<span className="import_heading">{MailPoet.I18n.t('methodMailChimpSelectList')}</span>
</div>
<Selection
field={{
id: 'segments',
name: 'list-selection',
multiple: true,
placeholder: MailPoet.I18n.t('methodMailChimpSelectPlaceholder'),
forceSelect2: true,
values: mailChimpLoadedLists,
}}
onValueChange={e => listSelected(e.target.value)}
/>
</>
);
};
const statusClasses = classNames(
'mailpoet_mailchimp-key-status',
{ 'mailpoet_mailchimp-ok': Array.isArray(mailChimpLoadedLists) }
);
return ( return (
<> <>
@@ -14,12 +89,18 @@ const MethodMailChimp = ({ setInputValid, setInputInvalid, onValueChange }) => {
<input <input
id="paste_input" id="paste_input"
type="text" type="text"
onChange={setKey} onChange={keyChange}
/> />
<button className="button" type="button"> <button className="button" type="button" onClick={verifyButtonClicked}>
{MailPoet.I18n.t('methodMailChimpVerify')} {MailPoet.I18n.t('methodMailChimpVerify')}
</button> </button>
<span className="mailpoet_mailchimp-key-status"></span> <span className={statusClasses}>
{ Array.isArray(mailChimpLoadedLists) && mailChimpLoadedLists.length === 0
? MailPoet.I18n.t('noMailChimpLists')
: null
}
</span>
{showListsSelection()}
</label> </label>
</> </>
); );

View File

@@ -99,6 +99,8 @@
'methodMailChimp': __('Import from MailChimp'), 'methodMailChimp': __('Import from MailChimp'),
'methodMailChimpLabel': __('Enter your MailChimp API key'), 'methodMailChimpLabel': __('Enter your MailChimp API key'),
'methodMailChimpVerify': __('Verify'), 'methodMailChimpVerify': __('Verify'),
'methodMailChimpSelectList': __('Select list(s)'),
'methodMailChimpSelectPlaceholder': _x('Select', 'Verb'),
'pasteLabel': __('Copy and paste your subscribers from Excel/Spreadsheets'), 'pasteLabel': __('Copy and paste your subscribers from Excel/Spreadsheets'),
'pasteDescription': __('This file needs to be formatted in a CSV style (comma-separated-values.) Look at some [link]examples on our support site[/link].'), 'pasteDescription': __('This file needs to be formatted in a CSV style (comma-separated-values.) Look at some [link]examples on our support site[/link].'),
'methodSelectionHead': __('How would you like to import subscribers?') 'methodSelectionHead': __('How would you like to import subscribers?')