Skip to content

Commit f8a38be

Browse files
committed
Update form field visibility based on the previous list form while switching the list.
1 parent 8c914a5 commit f8a38be

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

includes/blocks/mailchimp/edit.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ export const BlockEdit = (props) => {
7575
);
7676
const exisingTags = innerBlocks.map((block) => block?.attributes?.tag).filter(Boolean);
7777
const exisingGroups = innerBlocks.map((block) => block?.attributes?.id).filter(Boolean);
78-
const visibleFieldsCount = innerBlocks.filter((block) => block?.attributes?.visible).length;
78+
const visibleFields = innerBlocks
79+
.filter((block) => block?.attributes?.visible)
80+
.map((block) => block?.attributes?.tag);
81+
const visibleFieldsCount = visibleFields.length;
7982

8083
const listOptions = [];
8184
// Check if selected list is not in the list of available lists.
@@ -117,20 +120,26 @@ export const BlockEdit = (props) => {
117120
tag: field.tag,
118121
label: field.name,
119122
type: field.type,
123+
/**
124+
* Visibility logic:
125+
* 1. If there are visible fields from the previous list, make the field visible if it's required or it's visible in the previous list form (Try to keep the same visibility as the previous list form).
126+
* 2. If there are no visible fields from the previous list, make the field visible if it's required or it's public and the visibility setting is on in the global settings.
127+
*/
120128
visible:
121-
(field.required ||
122-
merge_fields_visibility?.[field.tag] === 'on') &&
123-
field.public,
129+
field.required ||
130+
(visibleFields.length > 0 &&
131+
visibleFields.includes(field.tag)) ||
132+
(visibleFields.length === 0 &&
133+
merge_fields_visibility?.[field.tag] === 'on' &&
134+
field.public),
124135
}),
125136
) || [];
126137
const listGroupsBlocks =
127138
data?.interest_groups?.map((group) =>
128139
createBlock('mailchimp/mailchimp-audience-group', {
129140
id: group.id,
130141
label: group.title,
131-
visible:
132-
interest_groups_visibility?.[group.id] === 'on' &&
133-
group.type !== 'hidden',
142+
visible: false, // Keep the groups hidden by default.
134143
}),
135144
) || [];
136145
replaceInnerBlocks(clientId, [...listFieldsBlocks, ...listGroupsBlocks], false);

0 commit comments

Comments
 (0)