Skip to content

Commit

Permalink
set the template on click
Browse files Browse the repository at this point in the history
  • Loading branch information
draganescu committed Sep 26, 2023
1 parent 35476e4 commit b0e1bcf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,18 @@ function injectThemeAttributeInBlockTemplateContent(
block,
currentThemeStylesheet
) {
if (
block.innerBlocks.find(
( innerBlock ) => innerBlock.name === 'core/template-part'
)
) {
block.innerBlocks = block.innerBlocks.map( ( innerBlock ) => {
if (
innerBlock.name === 'core/template-part' &&
innerBlock.attributes.theme === undefined
) {
innerBlock.attributes.theme = currentThemeStylesheet;
}
return innerBlock;
} );
}
block.innerBlocks = block.innerBlocks.map( ( innerBlock ) => {
if (
innerBlock.name === 'core/template-part' &&
innerBlock.attributes.theme === undefined
) {
innerBlock.attributes.theme = currentThemeStylesheet;
}
return injectThemeAttributeInBlockTemplateContent(
innerBlock,
currentThemeStylesheet
);
} );

if (
block.name === 'core/template-part' &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useState, useCallback } from '@wordpress/element';
import { __experimentalBlockPatternsList as BlockPatternsList } from '@wordpress/block-editor';
import { MenuItem, Modal } from '@wordpress/components';
Expand All @@ -16,8 +16,10 @@ import { store as editSiteStore } from '../../../store';

export default function ReplaceTemplateButton( {
onClick,
template,
availableTemplates,
} ) {
const { setTemplate } = useDispatch( editSiteStore );
const [ showModal, setShowModal ] = useState( false );
const onClose = useCallback( () => {
setShowModal( false );
Expand All @@ -30,12 +32,12 @@ export default function ReplaceTemplateButton( {
};
}, [] );

const entitiy = useEntityRecord( 'postType', postType, postId );

const entity = useEntityRecord( 'postType', postType, postId );
const onTemplateSelect = async ( selectedTemplate ) => {
// TODO - trigger a reload
entitiy.edit( { content: selectedTemplate.content } );

//FIXME: This is a hack to get around the fact that the template is not being set correctly.
await setTemplate( null, null );
await entity.edit( { content: selectedTemplate.content } );
await setTemplate( postId, template.slug );
onClose(); // Close the template suggestions modal first.
onClick();
};
Expand Down

0 comments on commit b0e1bcf

Please sign in to comment.