Skip to content

Commit

Permalink
More adjustments to storage of block.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauteri committed Dec 14, 2024
1 parent a63cc82 commit 6de1b57
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build/blocks/rsvp-v2/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "string",
"default": "[]"
},
"status": {
"selectedStatus": {
"type": "string",
"default": "no_status"
}
Expand Down
2 changes: 1 addition & 1 deletion build/blocks/rsvp-v2/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '81768e6e75749d7fdb1c');
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => 'cd34d55ab7e56db44f13');
2 changes: 1 addition & 1 deletion build/blocks/rsvp-v2/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions includes/core/classes/blocks/class-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function transform_block_content( string $block_content, array $block ):
if ( 'gatherpress/rsvp-v2' === $block['blockName'] ) {
$inner_blocks = isset( $block['innerBlocks'] ) ? $block['innerBlocks'] : array();
$tag = new WP_HTML_Tag_Processor( $block_content );
$attributes = isset( $block['attrs'] ) ? $block['attrs'] : array();

if ( $tag->next_tag() ) {
/**
Expand All @@ -78,14 +79,16 @@ public function transform_block_content( string $block_content, array $block ):
* with the current inner blocks. The updated serialized inner blocks are then re-encoded and
* saved as an attribute.
*/
$saved_status = $tag->get_attribute( 'data-saved-status' );
$saved_status = $attributes['selectedStatus'] ?? 'no_status';
$serialized_inner_blocks = $attributes['serializedInnerBlocks'] ?? '';
$serialized_inner_blocks = json_decode(
$tag->get_attribute( 'data-serialized-inner-blocks' ),
$serialized_inner_blocks,
true
);
$serialized_inner_blocks[ $saved_status ] = rawurlencode( serialize_blocks( $inner_blocks ) );
$serialized_inner_blocks = wp_json_encode( $serialized_inner_blocks );

$tag->remove_attribute( 'data-saved-status' );
$tag->set_attribute(
'data-serialized-inner-blocks',
$serialized_inner_blocks
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/rsvp-v2/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "string",
"default": "[]"
},
"status": {
"selectedStatus": {
"type": "string",
"default": "no_status"
}
Expand Down
14 changes: 7 additions & 7 deletions src/blocks/rsvp-v2/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function templateToBlocks(template) {
* @return {JSX.Element} The rendered edit interface for the RSVP block.
*/
const Edit = ({ attributes, setAttributes, clientId }) => {
const { serializedInnerBlocks = '{}', status } = attributes;
const { serializedInnerBlocks = '{}', selectedStatus } = attributes;
const blockProps = useBlockProps();
const { replaceInnerBlocks } = useDispatch(blockEditorStore);

Expand Down Expand Up @@ -102,9 +102,9 @@ const Edit = ({ attributes, setAttributes, clientId }) => {
const handleStatusChange = (newStatus) => {
loadInnerBlocksForState(newStatus); // Load blocks for the new state
setAttributes({
status: newStatus,
selectedStatus: newStatus,
}); // Update the state
saveInnerBlocks(status, newStatus, innerBlocks); // Save current inner blocks before switching state
saveInnerBlocks(selectedStatus, newStatus, innerBlocks); // Save current inner blocks before switching state
};

// Hydrate inner blocks for all statuses if not set
Expand All @@ -122,7 +122,7 @@ const Edit = ({ attributes, setAttributes, clientId }) => {
return updatedSerializedBlocks;
}

if (templateKey !== status) {
if (templateKey !== selectedStatus) {
const blocks = templateToBlocks(TEMPLATES[templateKey]);
const serialized = serialize(blocks);

Expand All @@ -141,15 +141,15 @@ const Edit = ({ attributes, setAttributes, clientId }) => {
};

hydrateInnerBlocks();
}, [serializedInnerBlocks, setAttributes, status]);
}, [serializedInnerBlocks, setAttributes, selectedStatus]);

return (
<>
<InspectorControls>
<PanelBody>
<SelectControl
label={__('RSVP Status', 'gatherpress')}
value={status}
value={selectedStatus}
options={[
{
label: __(
Expand Down Expand Up @@ -185,7 +185,7 @@ const Edit = ({ attributes, setAttributes, clientId }) => {
</PanelBody>
</InspectorControls>
<div {...blockProps}>
<InnerBlocks template={TEMPLATES[status]} />
<InnerBlocks template={TEMPLATES[selectedStatus]} />
</div>
</>
);
Expand Down
11 changes: 2 additions & 9 deletions src/blocks/rsvp-v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,9 @@ import './style.scss';
*/
registerBlockType(metadata, {
edit,
save: ({ attributes }) => {
const { serializedInnerBlocks, status } = attributes;
const blockProps = useBlockProps.save();

save: () => {
return (
<div
{...blockProps}
data-saved-status={status}
data-serialized-inner-blocks={serializedInnerBlocks}
>
<div {...useBlockProps.save()}>
<InnerBlocks.Content />
</div>
);
Expand Down

0 comments on commit 6de1b57

Please sign in to comment.