From 1993e6a60957dd95ffc82c0d05b8746f07feca9d Mon Sep 17 00:00:00 2001 From: Derrick Koo Date: Tue, 1 Jun 2021 19:50:10 -0600 Subject: [PATCH 1/2] fix: properly sync parent attributes to inner blocks --- src/blocks/curated-list/edit.js | 13 +++++++++++++ src/blocks/list-container/edit.js | 8 +------- src/blocks/listing/edit.js | 11 ++--------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/blocks/curated-list/edit.js b/src/blocks/curated-list/edit.js index ca4b8987..3dbc6da7 100644 --- a/src/blocks/curated-list/edit.js +++ b/src/blocks/curated-list/edit.js @@ -273,6 +273,19 @@ const CuratedListEditorComponent = ( { } }, [ selectedBlock ]); + /** + * If in specific listings mode, sync parent attributes to inner blocks. + */ + useEffect(() => { + if ( list && list.innerBlocks ) { + updateBlockAttributes( + [ list.clientId ].concat( list.innerBlocks.map( innerBlock => innerBlock.clientId ) ), // Array of client IDs for both list container and individual listings. + attributes, + false + ); + } + }, [ JSON.stringify( attributes ) ]); + /** * Determine if the background color is dark or light. */ diff --git a/src/blocks/list-container/edit.js b/src/blocks/list-container/edit.js index 3963b987..6bc43dbd 100644 --- a/src/blocks/list-container/edit.js +++ b/src/blocks/list-container/edit.js @@ -6,18 +6,12 @@ import { InnerBlocks, InspectorControls } from '@wordpress/block-editor'; import { Notice, PanelRow, Spinner } from '@wordpress/components'; import { compose } from '@wordpress/compose'; import { withDispatch, withSelect } from '@wordpress/data'; -import { useEffect } from '@wordpress/element'; -const ListContainerEditorComponent = ( { clientId, innerBlocks, parent, setAttributes } ) => { +const ListContainerEditorComponent = ( { clientId, innerBlocks, parent } ) => { const parentAttributes = parent.attributes || {}; const { queryMode, queryOptions, showSortUi } = parentAttributes; const { order } = queryOptions; - // Sync parent attributes to list container attributes, so that we can use parent attributes in the PHP render callback. - useEffect(() => { - setAttributes( { ...parentAttributes } ); - }, [ JSON.stringify( parentAttributes ) ]); - if ( queryMode && ! showSortUi ) { return null; } diff --git a/src/blocks/listing/edit.js b/src/blocks/listing/edit.js index 7f6a3e61..6a6c3138 100644 --- a/src/blocks/listing/edit.js +++ b/src/blocks/listing/edit.js @@ -36,8 +36,6 @@ const ListingEditorComponent = ( { if ( 'newspack-listings/list-container' === blockInfo.name ) { acc.listContainer = blockInfo; - } else if ( 'newspack-listings/curated-list' === blockInfo.name ) { - acc.curatedList = blockInfo; } return acc; @@ -58,16 +56,11 @@ const ListingEditorComponent = ( { // Fetch listing post data if we have a listing post ID. useEffect(() => { - if ( listing ) { + if ( ! post && listing ) { fetchPost( listing ); } }, [ listing ]); - // Sync parent attributes to listing attributes, so that we can use parent attributes in the PHP render callback. - useEffect(() => { - setAttributes( { ...parent.curatedList.attributes } ); - }, [ JSON.stringify( parent.curatedList.attributes ) ]); - // Fetch listing post by listingId. const fetchPost = async listingId => { try { @@ -188,7 +181,7 @@ const ListingEditorComponent = ( { return (
- + { post && (