/* eslint-disable @wordpress/i18n-ellipsis */ import { clearEventsList, displayErrorMessage, displayEvents, hideErrorMessages, showLoadingIndicator, } from '../../events-displayer.js' const { InspectorControls, useBlockProps } = wp.blockEditor const { PanelBody } = wp.components const { useEffect } = wp.element const { __ } = wp.i18n const NAME = '' let timer export default ({ attributes, setAttributes }) => { const blockProps = useBlockProps({ className: NAME + '_events-list', 'data-maximum': attributes.eventsCount, 'data-group-name': attributes.groupName, // TODO still necessary? }) function reloadEventList() { if (timer) { clearTimeout(timer) } timer = setTimeout(() => { const container = document.getElementById(blockProps.id) if (container) { // TODO not using newest values yet, can get out of sync hideErrorMessages(container) clearEventsList(container) showLoadingIndicator(container) const eventsCount = attributes.eventsCount const groupName = attributes.groupName let url = `/wp-json/connector-mobilizon/v1/events?eventsCount=${eventsCount}` if (groupName) { url += `&groupName=${groupName}` } fetch(url) .then((response) => response.text()) // TODO also handle response.ok being false .then((events) => { displayEvents({ events, document, container }) }) .catch((data) => { displayErrorMessage({ data, container }) }) } }, 500) } useEffect(() => { reloadEventList() }, []) function updateEventsCount(event) { let newValue = Number(event.target.value) if (newValue < 1) newValue = 1 setAttributes({ eventsCount: newValue }) reloadEventList() } function updateGroupName(event) { // TODO not triggered on pasting only setAttributes({ groupName: event.target.value }) reloadEventList() } return [ ')}> ,
{__('The events could not be loaded!', '')}
{__('The group could not be found!', '')}
{__('Loading...', '')}
, ] }