Skip to content

Commit

Permalink
Fix: prevent meta key conflict on block duplication (#6946)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloiankoski authored Sep 16, 2023
1 parent 74028d1 commit 19b3b0d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import MetaKeyTextControl, {slugifyMeta} from '@givewp/form-builder/supports/fie
*/
const FieldSettingsHOC = createHigherOrderComponent((BlockEdit) => {
return (props) => {
const {name, attributes, setAttributes} = props;
const {name, attributes, setAttributes, clientId} = props;

const fieldSettings: FieldSettings = useMemo(() => {
// @ts-ignore
Expand All @@ -41,6 +41,7 @@ const FieldSettingsHOC = createHigherOrderComponent((BlockEdit) => {
attributes={attributes}
setAttributes={setAttributes}
fieldSettings={fieldSettings}
clientId={clientId}
/>
</>
);
Expand All @@ -58,10 +59,10 @@ const generateEmailTag = (fieldName, storeAsDonorMeta) => {
*
* @since 3.0.0
*/
function FieldSettingsEdit({attributes, setAttributes, fieldSettings}) {
function FieldSettingsEdit({attributes, setAttributes, fieldSettings, clientId}) {
const validateFieldName = useFieldNameValidator();
const [hasFieldNameAttribute, setHasFieldNameAttribute] = useState(attributes.hasOwnProperty('fieldName'));
const [isNewField] = useState(!hasFieldNameAttribute);
const [hasFieldNameAttribute, setHasFieldNameAttribute] = useState<boolean>(attributes.hasOwnProperty('fieldName'));
const [isNewField] = useState<boolean>(attributes.metaUUID !== clientId);

const updateFieldName = useCallback(
(newFieldName = null, bumpUniqueness = false) => {
Expand Down Expand Up @@ -109,6 +110,7 @@ function FieldSettingsEdit({attributes, setAttributes, fieldSettings}) {
if (isNewField) {
updateFieldName();
setHasFieldNameAttribute(false);
setAttributes({metaUUID: clientId});
}
}, []);

Expand Down Expand Up @@ -224,7 +226,7 @@ function FieldSettingsEdit({attributes, setAttributes, fieldSettings}) {
value={attributes.fieldName}
onChange={(newName) => setAttributes({fieldName: newName})}
onBlur={enforceFieldName}
lockValue={isNewField}
lockValue={!isNewField}
/>
</PanelRow>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export default function updateBlockTypes(settings) {
}

if (fieldSettings.metaKey) {
fieldAttributes.metaUUID = {
type: 'string',
};

fieldAttributes.fieldName = {
type: 'string',
};
Expand Down

0 comments on commit 19b3b0d

Please sign in to comment.