connector-wordpress/source/front/blocks/events-list/edit.js

114 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-06-03 23:48:19 +02:00
/* eslint-disable @wordpress/i18n-ellipsis */
2024-04-05 23:26:11 +02:00
import {
clearEventsList,
displayErrorMessage,
displayEvents,
hideErrorMessages,
showLoadingIndicator,
} from '../../events-displayer.js'
2022-06-03 23:48:19 +02:00
const { InspectorControls, useBlockProps } = wp.blockEditor
2024-04-05 23:26:11 +02:00
const { Panel, PanelBody } = wp.components
2022-06-03 23:48:19 +02:00
const { useEffect } = wp.element
2021-11-01 20:55:37 +01:00
const { __ } = wp.i18n
2022-06-03 23:48:19 +02:00
const NAME = '<wordpress-name>'
2021-11-01 20:55:37 +01:00
export default ({ attributes, setAttributes }) => {
2024-04-05 23:26:11 +02:00
let timer
2022-06-03 23:48:19 +02:00
const blockProps = useBlockProps({
className: NAME + '_events-list',
})
2024-04-05 23:26:11 +02:00
function reloadEventList(eventsCount, groupName) {
2022-06-03 23:48:19 +02:00
if (timer) {
clearTimeout(timer)
}
2024-04-05 23:26:11 +02:00
timer = setTimeout(async () => {
2022-06-03 23:48:19 +02:00
const container = document.getElementById(blockProps.id)
if (container) {
2024-04-05 23:26:11 +02:00
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 })
})
2022-06-03 23:48:19 +02:00
}
}, 500)
}
useEffect(() => {
2024-04-05 23:26:11 +02:00
reloadEventList(attributes.eventsCount, attributes.groupName)
2022-06-03 23:48:19 +02:00
}, [])
2021-11-01 20:55:37 +01:00
function updateEventsCount(event) {
let newValue = Number(event.target.value)
if (newValue < 1) newValue = 1
setAttributes({ eventsCount: newValue })
2024-04-05 23:26:11 +02:00
reloadEventList(newValue, attributes.groupName)
2021-11-01 20:55:37 +01:00
}
function updateGroupName(event) {
2024-04-05 23:26:11 +02:00
const newValue = event.target.value
setAttributes({ groupName: newValue })
reloadEventList(attributes.eventsCount, newValue)
2021-11-01 20:55:37 +01:00
}
2021-12-14 15:33:07 +01:00
return [
2021-11-01 20:55:37 +01:00
<InspectorControls>
2024-04-05 23:26:11 +02:00
<Panel>
<PanelBody title={__('Events List Settings', '<wordpress-name>')}>
<label
className="components-base-control__label"
htmlFor={NAME + '_events-count'}
>
{__('Number of events to show', '<wordpress-name>')}
</label>
<input
className="components-text-control__input"
type="number"
value={attributes.eventsCount}
onChange={updateEventsCount}
id={NAME + '_events-count'}
/>
<label
className="components-base-control__label"
htmlFor={NAME + '_group-name'}
>
{__('Group name (optional)', '<wordpress-name>')}
</label>
<input
className="components-text-control__input"
type="text"
value={attributes.groupName}
onChange={updateGroupName}
id={NAME + '_group-name'}
/>
</PanelBody>
</Panel>
2021-11-01 20:55:37 +01:00
</InspectorControls>,
2022-06-03 23:48:19 +02:00
<div {...blockProps}>
<div className="general-error" style={{ display: 'none' }}>
{__('The events could not be loaded!', '<wordpress-name>')}
</div>
<div className="group-not-found" style={{ display: 'none' }}>
{__('The group could not be found!', '<wordpress-name>')}
</div>
<div className="loading-indicator" style={{ display: 'none' }}>
{__('Loading...', '<wordpress-name>')}
</div>
<ul style={{ 'list-style-type': 'none', 'padding-left': 0 }}></ul>
</div>,
2021-12-14 15:33:07 +01:00
]
2021-11-01 20:55:37 +01:00
}