/* eslint-disable @wordpress/i18n-ellipsis */ import { clearEventsList, displayErrorMessage, displayEvents, hideErrorMessages, showLoadingIndicator, } from '../../events-displayer.js' const { InspectorControls, useBlockProps } = wp.blockEditor const { Panel, PanelBody } = wp.components const { useEffect } = wp.element const { __ } = wp.i18n const NAME = '' export default ({ attributes, setAttributes }) => { let timer const blockProps = useBlockProps({ className: NAME + '_events-list', }) function reloadEventList(eventsCount, groupName) { if (timer) { clearTimeout(timer) } timer = setTimeout(async () => { const container = document.getElementById(blockProps.id) if (container) { hideErrorMessages(container) clearEventsList(container) showLoadingIndicator(container) let url = `/wp-json/connector-mobilizon/v1/events?eventsCount=${eventsCount}` if (groupName) { url += `&groupName=${groupName}` } await fetch(url) .then((response) => response.text()) .then((data) => { const events = JSON.parse(data) displayEvents({ events, document, container, maxEventsCount: eventsCount, }) }) .catch((data) => { displayErrorMessage({ data, container }) }) } }, 500) } useEffect(() => { reloadEventList(attributes.eventsCount, attributes.groupName) }, []) function updateEventsCount(event) { let newValue = Number(event.target.value) if (newValue < 1) newValue = 1 setAttributes({ eventsCount: newValue }) reloadEventList(newValue, attributes.groupName) } function updateGroupName(event) { const newValue = event.target.value setAttributes({ groupName: newValue }) reloadEventList(attributes.eventsCount, newValue) } return [ ')}> ,
{__('The events could not be loaded!', '')}
{__('The group could not be found!', '')}
{__('Loading...', '')}
, ] }