Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: syncing attributes to inner blocks #63

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/blocks/curated-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,19 @@ const CuratedListEditorComponent = ( {
}
}, [ selectedBlock ]);

/**
* Sync parent attributes to inner blocks.
*/
useEffect(() => {
if ( list ) {
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.
*/
Expand Down
8 changes: 1 addition & 7 deletions src/blocks/list-container/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 2 additions & 9 deletions src/blocks/listing/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -188,7 +181,7 @@ const ListingEditorComponent = ( {

return (
<div className="newspack-listings__listing-editor newspack-listings__listing">
<Listing attributes={ parent.curatedList.attributes } error={ error } post={ post } />
<Listing attributes={ attributes } error={ error } post={ post } />
{ post && (
<Button
isLink
Expand Down