Fix custom fields label saving for checkbox

[MAILPOET-3926]
This commit is contained in:
Pavel Dohnal
2021-11-10 15:45:29 +01:00
committed by Veljko V
parent 9700e9abbb
commit 9323a7a0d8
3 changed files with 18 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
import React, { useEffect, useMemo, useState } from 'react'; import React, { useEffect, useMemo, useState } from 'react';
import { import {
BaseControl, BaseControl,
Button, Button, TextControl,
ToggleControl, ToggleControl,
} from '@wordpress/components'; } from '@wordpress/components';
import { isEmpty } from 'lodash'; import { isEmpty } from 'lodash';
@@ -10,6 +10,7 @@ import MailPoet from 'mailpoet';
import CustomFieldDelete from '../custom_field_delete.jsx'; import CustomFieldDelete from '../custom_field_delete.jsx';
const CustomFieldSettings = ({ const CustomFieldSettings = ({
label,
mandatory, mandatory,
isSaving, isSaving,
onSave, onSave,
@@ -19,20 +20,23 @@ const CustomFieldSettings = ({
onCustomFieldDelete, onCustomFieldDelete,
onChange, onChange,
}) => { }) => {
const [localLabel, setLocalLabel] = useState(label);
const [localMandatory, setLocalMandatory] = useState(mandatory); const [localMandatory, setLocalMandatory] = useState(mandatory);
const [localIsChecked, setLocalIsChecked] = useState(isChecked); const [localIsChecked, setLocalIsChecked] = useState(isChecked);
const [localCheckboxLabel, setLocalCheckboxLabel] = useState(checkboxLabel); const [localCheckboxLabel, setLocalCheckboxLabel] = useState(checkboxLabel);
const hasUnsavedChanges = localMandatory !== mandatory const hasUnsavedChanges = localMandatory !== mandatory
|| localIsChecked !== isChecked || localIsChecked !== isChecked
|| localLabel !== label
|| localCheckboxLabel !== checkboxLabel; || localCheckboxLabel !== checkboxLabel;
const localData = useMemo(() => ({ const localData = useMemo(() => ({
mandatory: localMandatory, mandatory: localMandatory,
isChecked: localIsChecked, isChecked: localIsChecked,
label: localLabel,
checkboxLabel: localCheckboxLabel, checkboxLabel: localCheckboxLabel,
isValid: !isEmpty(localCheckboxLabel), isValid: !isEmpty(localCheckboxLabel),
}), [localMandatory, localIsChecked, localCheckboxLabel]); }), [localLabel, localMandatory, localIsChecked, localCheckboxLabel]);
useEffect(() => { useEffect(() => {
onChange(localData, hasUnsavedChanges); onChange(localData, hasUnsavedChanges);
@@ -40,6 +44,12 @@ const CustomFieldSettings = ({
return ( return (
<div className="custom-field-settings"> <div className="custom-field-settings">
<TextControl
label={MailPoet.I18n.t('label')}
value={localLabel}
data-automation-id="settings_custom_text_label_input"
onChange={setLocalLabel}
/>
<ToggleControl <ToggleControl
label={MailPoet.I18n.t('blockMandatory')} label={MailPoet.I18n.t('blockMandatory')}
checked={localMandatory} checked={localMandatory}
@@ -80,6 +90,7 @@ const CustomFieldSettings = ({
}; };
CustomFieldSettings.propTypes = { CustomFieldSettings.propTypes = {
label: PropTypes.string,
mandatory: PropTypes.bool, mandatory: PropTypes.bool,
onSave: PropTypes.func, onSave: PropTypes.func,
isSaving: PropTypes.bool, isSaving: PropTypes.bool,
@@ -91,6 +102,7 @@ CustomFieldSettings.propTypes = {
}; };
CustomFieldSettings.defaultProps = { CustomFieldSettings.defaultProps = {
label: '',
mandatory: false, mandatory: false,
onSave: null, onSave: null,
isSaving: false, isSaving: false,

View File

@@ -3,7 +3,6 @@ import React from 'react';
import { import {
Panel, Panel,
PanelBody, PanelBody,
TextControl,
ToggleControl, ToggleControl,
} from '@wordpress/components'; } from '@wordpress/components';
import { InspectorControls } from '@wordpress/block-editor'; import { InspectorControls } from '@wordpress/block-editor';
@@ -56,6 +55,7 @@ const CustomCheckboxEdit = ({ attributes, setAttributes, clientId }) => {
<Panel> <Panel>
<PanelBody title={MailPoet.I18n.t('customFieldSettings')} initialOpen> <PanelBody title={MailPoet.I18n.t('customFieldSettings')} initialOpen>
<CustomFieldSettings <CustomFieldSettings
label={attributes.label}
mandatory={attributes.mandatory} mandatory={attributes.mandatory}
isSaving={isSaving} isSaving={isSaving}
isChecked={isChecked()} isChecked={isChecked()}
@@ -67,6 +67,7 @@ const CustomCheckboxEdit = ({ attributes, setAttributes, clientId }) => {
}, },
onFinish: () => setAttributes({ onFinish: () => setAttributes({
mandatory: params.mandatory, mandatory: params.mandatory,
label: params.label,
values: [{ values: [{
isChecked: params.isChecked, isChecked: params.isChecked,
name: params.checkboxLabel, name: params.checkboxLabel,
@@ -84,12 +85,6 @@ const CustomCheckboxEdit = ({ attributes, setAttributes, clientId }) => {
</Panel> </Panel>
<Panel> <Panel>
<PanelBody title={MailPoet.I18n.t('formSettings')} initialOpen> <PanelBody title={MailPoet.I18n.t('formSettings')} initialOpen>
<TextControl
label={MailPoet.I18n.t('label')}
value={attributes.label}
data-automation-id="settings_custom_text_label_input"
onChange={(label) => (setAttributes({ label }))}
/>
<ToggleControl <ToggleControl
label={MailPoet.I18n.t('displayLabel')} label={MailPoet.I18n.t('displayLabel')}
checked={!attributes.hideLabel} checked={!attributes.hideLabel}

View File

@@ -2,6 +2,7 @@ function mapFormDataToParams(fieldType, data) {
switch (fieldType) { switch (fieldType) {
case 'checkbox': case 'checkbox':
return { return {
label: data.label,
required: data.mandatory ? '1' : '', required: data.mandatory ? '1' : '',
values: [{ values: [{
is_checked: data.isChecked ? '1' : '', is_checked: data.isChecked ? '1' : '',
@@ -10,6 +11,7 @@ function mapFormDataToParams(fieldType, data) {
}; };
case 'date': case 'date':
return { return {
label: data.label,
required: data.mandatory ? '1' : '', required: data.mandatory ? '1' : '',
date_type: data.dateType, date_type: data.dateType,
date_format: data.dateFormat, date_format: data.dateFormat,