Skip to content

Commit

Permalink
Merge pull request #990 from GatherPress/event-date-context
Browse files Browse the repository at this point in the history
Add context to event date block.
  • Loading branch information
mauteri authored Jan 11, 2025
2 parents d8e8e92 + 0718de8 commit 170e0e2
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 18 deletions.
6 changes: 3 additions & 3 deletions build/blocks/event-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
"queryId"
],
"attributes": {
"postId": {
"type": "number"
},
"eventEnd": {
"type": "string"
},
Expand All @@ -29,6 +26,9 @@
}
},
"supports": {
"gatherpress": {
"postIdOverride": true
},
"html": false,
"color": {
"gradients": true,
Expand Down
2 changes: 1 addition & 1 deletion build/blocks/event-date/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('moment', 'react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-date', 'wp-element', 'wp-i18n'), 'version' => '8db485ef7a20d1f6e7c4');
<?php return array('dependencies' => array('moment', 'react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-date', 'wp-element', 'wp-i18n'), 'version' => 'c6829ea8513beba981dc');
4 changes: 2 additions & 2 deletions build/blocks/event-date/index.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion build/blocks/event-date/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use GatherPress\Core\Event;
use GatherPress\Core\Block;

$gatherpress_event = new Event( get_the_ID() );
$gatherpress_block_instance = Block::get_instance();
$gatherpress_post_id = $gatherpress_block_instance->get_post_id( $block->parsed_block );
$gatherpress_event = new Event( $gatherpress_post_id );
?>
<div <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
<?php echo esc_html( $gatherpress_event->get_display_datetime() ); ?>
Expand Down
6 changes: 3 additions & 3 deletions src/blocks/event-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"description": "Displays the date and time for an event.",
"usesContext": [ "postId", "queryId" ],
"attributes": {
"postId": {
"type": "number"
},
"eventEnd": {
"type": "string"
},
Expand All @@ -26,6 +23,9 @@
}
},
"supports": {
"gatherpress": {
"postIdOverride": true
},
"html": false,
"color": {
"gradients": true,
Expand Down
19 changes: 12 additions & 7 deletions src/blocks/event-date/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalVStack as VStack,
PanelBody,
Spinner,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';

Expand All @@ -26,8 +27,6 @@ import { useSelect } from '@wordpress/data';
*/
import {
convertPHPToMomentFormat,
defaultDateTimeStart,
defaultDateTimeEnd,
getTimezone,
getUtcOffset,
} from '../../helpers/datetime';
Expand Down Expand Up @@ -104,7 +103,7 @@ const Edit = ({ attributes, setAttributes, context }) => {
[`has-text-align-${textAlign}`]: textAlign,
}),
});
const { postId } = context;
const postId = attributes?.postId ?? context?.postId ?? null;

const { dateTimeStart, dateTimeEnd, timezone } = useSelect(
(select) => {
Expand All @@ -127,16 +126,22 @@ const Edit = ({ attributes, setAttributes, context }) => {
)?.meta;

return {
dateTimeStart:
meta?.gatherpress_datetime_start || defaultDateTimeStart,
dateTimeEnd:
meta?.gatherpress_datetime_end || defaultDateTimeEnd,
dateTimeStart: meta?.gatherpress_datetime_start,
dateTimeEnd: meta?.gatherpress_datetime_end,
timezone: meta?.gatherpress_timezone,
};
},
[postId]
);

if (!dateTimeStart || !dateTimeEnd || !timezone) {
return (
<div {...blockProps}>
<Spinner />
</div>
);
}

return (
<div {...blockProps}>
<BlockControls>
Expand Down
5 changes: 4 additions & 1 deletion src/blocks/event-date/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use GatherPress\Core\Event;
use GatherPress\Core\Block;

$gatherpress_event = new Event( get_the_ID() );
$gatherpress_block_instance = Block::get_instance();
$gatherpress_post_id = $gatherpress_block_instance->get_post_id( $block->parsed_block );
$gatherpress_event = new Event( $gatherpress_post_id );
?>
<div <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
<?php echo esc_html( $gatherpress_event->get_display_datetime() ); ?>
Expand Down

0 comments on commit 170e0e2

Please sign in to comment.