Save form selection

[MAILPOET-1798]
This commit is contained in:
Pavel Dohnal
2020-02-03 12:38:18 +01:00
committed by Jack Kitterhing
parent eb4ea14744
commit 9fcb189295
2 changed files with 26 additions and 5 deletions

View File

@@ -1,18 +1,26 @@
/* eslint-disable react/react-in-jsx-scope */ /* eslint-disable react/react-in-jsx-scope */
const wp = window.wp; import PropTypes from 'prop-types';
const { useState } = wp.element;
const allForms = window.mailpoet_forms; const allForms = window.mailpoet_forms;
function Edit() { function Edit({ attributes, setAttributes }) {
function displayFormsSelect() { function displayFormsSelect() {
if (!Array.isArray(allForms)) return null; if (!Array.isArray(allForms)) return null;
if (allForms.length === 0) return null; if (allForms.length === 0) return null;
return ( return (
<select> <select
onChange={(event) => {
setAttributes({
selectedForm: parseInt(event.target.value, 10),
});
}}
defaultValue={attributes.selectedForm}
>
<option value="" disabled selected>Select a MailPoet form</option> <option value="" disabled selected>Select a MailPoet form</option>
{allForms.map((form) => ( {allForms.map((form) => (
<option value={form.id}>{form.name}</option> <option value={form.id}>
{form.name}
</option>
))} ))}
</select> </select>
); );
@@ -26,4 +34,11 @@ function Edit() {
); );
} }
Edit.propTypes = {
attributes: PropTypes.shape({
selectedForm: PropTypes.number,
}).isRequired,
setAttributes: PropTypes.func.isRequired,
};
export default Edit; export default Edit;

View File

@@ -9,6 +9,12 @@ registerBlockType('mailpoet/form-block', {
icon: Icon, icon: Icon,
category: 'widgets', category: 'widgets',
example: {}, example: {},
attributes: {
selectedForm: {
type: 'number',
default: null,
},
},
edit: Edit, edit: Edit,
save() { save() {
return null; return null;