diff --git a/assets/js/translation-events.js b/assets/js/translation-events.js index 5052977d..d2fbc5ab 100644 --- a/assets/js/translation-events.js +++ b/assets/js/translation-events.js @@ -99,6 +99,39 @@ } ); + $( document ).on( + 'click', + '.load-more-events-btn', + function ( e ) { + $( this ).text('Loading...').prop('disabled', true); + let eventType = $( this ).data('event-type'); + let nextPage = $( this ).data('event-next-page'); + const url = `/events/?${encodeURIComponent(eventType)}=${encodeURIComponent(nextPage)}&format=html`; + + fetch(url) + .then(response => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + return response.json(); + }) + .then(response => { + const $html = $(response.data.html); + const $listItems = $html.find('li'); + if ( 0 === response.data.nextPage ){ + $(this).hide(); + } else { + $(this).data('event-next-page', response.data.nextPage); + } + $(this).parent().prev('.wp-block-wporg-event-list').find('ul').append($listItems); + $(this).text('Load more').prop('disabled', false); + }) + .catch(error => { + console.error('Error fetching next page:', error); + }); + } + ); + } ); diff --git a/includes/routes/event/list.php b/includes/routes/event/list.php index 42f14ac4..21c44978 100644 --- a/includes/routes/event/list.php +++ b/includes/routes/event/list.php @@ -22,48 +22,91 @@ public function handle(): void { $_upcoming_events_paged = 1; $_past_events_paged = 1; $_user_attending_events_paged = 1; + $filter_key = ''; // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['current_events_paged'] ) ) { $value = sanitize_text_field( wp_unslash( $_GET['current_events_paged'] ) ); if ( is_numeric( $value ) ) { $_current_events_paged = (int) $value; + $filter_key = 'current_events_query'; } } if ( isset( $_GET['upcoming_events_paged'] ) ) { $value = sanitize_text_field( wp_unslash( $_GET['upcoming_events_paged'] ) ); if ( is_numeric( $value ) ) { $_upcoming_events_paged = (int) $value; + $filter_key = 'upcoming_events_query'; } } if ( isset( $_GET['past_events_paged'] ) ) { $value = sanitize_text_field( wp_unslash( $_GET['past_events_paged'] ) ); if ( is_numeric( $value ) ) { $_past_events_paged = (int) $value; + $filter_key = 'past_events_query'; } } if ( isset( $_GET['user_attending_events_paged'] ) ) { $value = sanitize_text_field( wp_unslash( $_GET['user_attending_events_paged'] ) ); if ( is_numeric( $value ) ) { $_user_attending_events_paged = (int) $value; + $filter_key = 'user_attending_events_query'; } } // phpcs:enable - - $current_events_query = $this->event_repository->get_current_events( $_current_events_paged, 10 ); - $upcoming_events_query = $this->event_repository->get_upcoming_events( $_upcoming_events_paged, 10 ); - $past_events_query = $this->event_repository->get_past_events( $_past_events_paged, 10 ); - $user_attending_events_query = $this->event_repository->get_current_and_upcoming_events_for_user( get_current_user_id(), $_user_attending_events_paged, 10 ); + $tmpl_args = array( + 'current_events_query' => $this->event_repository->get_current_events( $_current_events_paged, 10 ), + 'upcoming_events_query' => $this->event_repository->get_upcoming_events( $_upcoming_events_paged, 10 ), + 'past_events_query' => $this->event_repository->get_past_events( $_past_events_paged, 10 ), + 'user_attending_events_query' => $this->event_repository->get_current_and_upcoming_events_for_user( get_current_user_id(), $_user_attending_events_paged, 10 ), + ); $this->use_theme(); + // phpcs:disable WordPress.Security.NonceVerification.Recommended + if ( isset( $_GET['format'] ) ) { + + $format = sanitize_text_field( wp_unslash( $_GET['format'] ) ); + + if ( 'html' !== $format || empty( $filter_key ) ) { + return; + } + + if ( ! empty( $tmpl_args[ $filter_key ]->event_ids ) ) { + $this->handle_ajax( $filter_key, $tmpl_args ); + } + } + $this->tmpl( 'home', + $tmpl_args, + ); + } + + public function handle_ajax( $filter_key, $tmpl_args ) { + $event_ids = $tmpl_args[ $filter_key ]->event_ids; + $current_page = $tmpl_args[ $filter_key ]->current_page; + $page_count = $tmpl_args[ $filter_key ]->page_count; + $next_page = ( ( $current_page + 1 ) <= $page_count ) ? $current_page + 1 : 0; + + $list_block_markup = ''; + + $rendered_html = ''; + $parsed_blocks = parse_blocks( do_blocks( $list_block_markup ) ); + foreach ( $parsed_blocks as $block ) { + $rendered_html .= render_block( $block ); + } + + wp_send_json_success( array( - 'current_events_query' => $current_events_query, - 'upcoming_events_query' => $upcoming_events_query, - 'past_events_query' => $past_events_query, - 'user_attending_events_query' => $user_attending_events_query, - ), + 'nextPage' => $next_page, + 'html' => $rendered_html, + ) ); } } diff --git a/themes/wporg-translate-events-2024/blocks/event-list/index.php b/themes/wporg-translate-events-2024/blocks/event-list/index.php index 8c91dca6..aa321585 100644 --- a/themes/wporg-translate-events-2024/blocks/event-list/index.php +++ b/themes/wporg-translate-events-2024/blocks/event-list/index.php @@ -9,12 +9,12 @@ // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found 'render_callback' => function ( array $attributes ) { $event_ids = $attributes['event_ids'] ?? array(); - + $event_filter = $attributes['filter_by'] ?? ''; if ( empty( $event_ids ) ) { return get_no_result_view(); } $show_flag = ! empty( $attributes['show_flag'] ) && true === $attributes['show_flag']; - + $next_page = ! empty( $attributes['next_page'] ) ? $attributes['next_page'] : 0; ob_start(); ?>