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

Added datetime formatting to Events List block. #517

Merged
merged 2 commits into from
Jan 27, 2024
Merged
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
4 changes: 4 additions & 0 deletions build/blocks/events-list/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"showVenue": true
}
},
"datetimeFormat": {
"type": "string",
"default": "D, M j, Y, g:i a T"
},
"maxNumberOfEvents": {
"type": "integer",
"default": 5
Expand Down
2 changes: 1 addition & 1 deletion build/blocks/events-list/events-list.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '3604abe8943f95554574');
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '102d9ce24cd27f5a1b3d');
2 changes: 1 addition & 1 deletion build/blocks/events-list/events-list.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/blocks/events-list/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '4fec9a94e4a16f2a0a93');
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '7d75b0853ccb1fee0c70');
4 changes: 2 additions & 2 deletions build/blocks/events-list/index.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions includes/core/classes/class-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ protected function events_list_route(): array {
'required' => true,
'validate_callback' => array( $this, 'validate_number' ),
),
'datetime_format' => array(
'required' => false,
),
'topics' => array(
'required' => false,
),
Expand Down Expand Up @@ -520,6 +523,7 @@ public function events_list( WP_REST_Request $request ): WP_REST_Response {
$params = $request->get_params();
$event_list_type = $params['event_list_type'];
$max_number = $this->max_number( (int) $params['max_number'], 5 );
$datetime_format = ! empty( $params['datetime_format'] ) ? $params['datetime_format'] : 'D, M j, Y, g:i a T';
$posts = array();
$topics = array();
$venues = array();
Expand Down Expand Up @@ -550,8 +554,8 @@ static function( $slug ): string {
$venue_information = $event->get_venue_information();
$posts[] = array(
'ID' => $post_id,
'datetime_start' => $event->get_datetime_start(),
'datetime_end' => $event->get_datetime_end(),
'datetime_start' => $event->get_datetime_start( $datetime_format ),
'datetime_end' => $event->get_datetime_end( $datetime_format ),
'permalink' => get_the_permalink( $post_id ),
'title' => get_the_title( $post_id ),
'excerpt' => get_the_excerpt( $post_id ),
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/events-list/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"showVenue": true
}
},
"datetimeFormat": {
"type": "string",
"default": "D, M j, Y, g:i a T"
},
"maxNumberOfEvents": {
"type": "integer",
"default": 5
Expand Down
15 changes: 15 additions & 0 deletions src/blocks/events-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { includes } from 'lodash';
import classnames from 'classnames';
import HtmlReactParser from 'html-react-parser';

/**
* WordPress dependencies.
Expand Down Expand Up @@ -188,6 +189,19 @@ const Edit = (props) => {
</ButtonGroup>
</PanelBody>
<PanelBody>
<TextControl
label={__('Date & time format', 'gatherpress')}
value={attributes.datetimeFormat}
help={HtmlReactParser(
__(
'For more information read the <a href="https://wordpress.org/documentation/article/customize-date-and-time-format/">Documentation on date and time formatting</a>.',
'gatherpress'
)
)}
onChange={(newVal) =>
setAttributes({ datetimeFormat: newVal })
}
/>
<RangeControl
label={__(
'Maximum number of events to display',
Expand Down Expand Up @@ -362,6 +376,7 @@ const Edit = (props) => {
<EventsList
eventOptions={attributes.eventOptions}
maxNumberOfEvents={attributes.maxNumberOfEvents}
datetimeFormat={attributes.datetimeFormat}
type={attributes.type}
topics={attributes.topics}
venues={attributes.venues}
Expand Down
1 change: 1 addition & 0 deletions src/blocks/events-list/events-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ domReady(() => {
}
type={attrs.type ?? 'upcoming'}
maxNumberOfEvents={attrs.maxNumberOfEvents ?? 5}
datetimeFormat={attrs.datetimeFormat ?? 'D, M j, Y, g:i a T'}
topics={attrs.topics ?? []}
/>
);
Expand Down
15 changes: 11 additions & 4 deletions src/components/EventsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ import apiFetch from '@wordpress/api-fetch';
* @return {JSX.Element} The rendered React component.
*/
const EventsList = (props) => {
const { eventOptions, maxNumberOfEvents, type, topics, venues } = props;
const {
eventOptions,
maxNumberOfEvents,
datetimeFormat,
type,
topics,
venues,
} = props;
const [events, setEvents] = useState([]);
const [loaded, setLoaded] = useState(false);
const renderEvents = events.map((event) => {
Expand Down Expand Up @@ -84,15 +91,15 @@ const EventsList = (props) => {
*/
if (getFromGlobal('is_user_logged_in')) {
apiFetch({
path: `/gatherpress/v1/event/events-list?event_list_type=${type}&max_number=${maxNumberOfEvents}&topics=${topicsString}&venues=${venuesString}`,
path: `/gatherpress/v1/event/events-list?event_list_type=${type}&max_number=${maxNumberOfEvents}&datetime_format=${datetimeFormat}&topics=${topicsString}&venues=${venuesString}`,
}).then((data) => {
setLoaded(true);
setEvents(data);
});
} else {
const endpoint =
getFromGlobal('event_rest_api') +
`/events-list?event_list_type=${type}&max_number=${maxNumberOfEvents}&topics=${topicsString}&venues=${venuesString}`;
`/events-list?event_list_type=${type}&max_number=${maxNumberOfEvents}&datetime_format=${datetimeFormat}&topics=${topicsString}&venues=${venuesString}`;

/**
* Not using apiFetch helper here as it will use X-Wp-Nonce and cache it when page caching is on causing a 403.
Expand All @@ -108,7 +115,7 @@ const EventsList = (props) => {
setEvents(data);
});
}
}, [setEvents, maxNumberOfEvents, type, topics, venues]);
}, [setEvents, maxNumberOfEvents, datetimeFormat, type, topics, venues]);

return (
<div className={`gp-${type}-events-list`}>
Expand Down
Loading