Compare commits

...

3 Commits

Author SHA1 Message Date
Daniel Waxweiler 208d2de4c1 simplify handling 2024-04-03 23:03:58 +02:00
Daniel Waxweiler 53231ee0fe add permission callback 2024-04-03 22:49:12 +02:00
Daniel Waxweiler 5988457253 return status code in error cases too 2024-04-03 22:43:49 +02:00
4 changed files with 17 additions and 14 deletions

View File

@ -17,10 +17,9 @@ const NAME = '<wordpress-name>'
let timer
export default ({ attributes, setAttributes }) => {
const { eventsCount, groupName } = attributes
const blockProps = useBlockProps({
className: NAME + '_events-list',
'data-maximum': attributes.eventsCount,
'data-group-name': attributes.groupName, // TODO still necessary?
})
function reloadEventList() {
if (timer) {
@ -33,8 +32,6 @@ export default ({ attributes, setAttributes }) => {
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}`
@ -43,7 +40,12 @@ export default ({ attributes, setAttributes }) => {
.then((response) => response.text()) // TODO also handle response.ok being false
.then((data) => {
const events = JSON.parse(data)
displayEvents({ events, document, container })
displayEvents({
events,
document,
container,
maxEventsCount: eventsCount,
})
})
.catch((data) => {
displayErrorMessage({ data, container })
@ -55,12 +57,14 @@ export default ({ attributes, setAttributes }) => {
reloadEventList()
}, [])
function updateEventsCount(event) {
// console.log('new value: ', event.target.value) // TODO
let newValue = Number(event.target.value)
if (newValue < 1) newValue = 1
setAttributes({ eventsCount: newValue })
reloadEventList()
}
function updateGroupName(event) {
// console.log('new value: ', event.target.value) // TODO
// TODO not triggered on pasting only
setAttributes({ groupName: event.target.value })
reloadEventList()
@ -77,7 +81,7 @@ export default ({ attributes, setAttributes }) => {
<input
className="components-text-control__input"
type="number"
value={attributes.eventsCount}
value={eventsCount}
onChange={updateEventsCount}
id={NAME + '_events-count'}
/>
@ -90,7 +94,7 @@ export default ({ attributes, setAttributes }) => {
<input
className="components-text-control__input"
type="text"
value={attributes.groupName}
value={groupName}
onChange={updateGroupName}
id={NAME + '_group-name'}
/>

View File

@ -18,7 +18,6 @@ test.before(() => {
test.beforeEach((t) => {
t.context.container = document.createElement('div')
t.context.container.setAttribute('data-maximum', '2')
const errorMessageGeneral = document.createElement('div')
errorMessageGeneral.setAttribute('class', 'general-error')
@ -53,7 +52,7 @@ test('#displayEvents one event', (t) => {
},
]
const container = t.context.container
displayEvents({ events, document, container })
displayEvents({ events, document, container, maxEventsCount: 2 })
const list = container.querySelector('ul')
t.is(list.children[0].childNodes[0].tagName, 'A')
t.is(list.children[0].childNodes[0].getAttribute('href'), 'b')

View File

@ -6,13 +6,12 @@ export function clearEventsList(container) {
list.replaceChildren()
}
export function displayEvents({ events, document, container }) {
export function displayEvents({ events, document, container, maxEventsCount }) {
hideLoadingIndicator(container)
const isShortOffsetNameShown =
window.MOBILIZON_CONNECTOR.isShortOffsetNameShown
const locale = window.MOBILIZON_CONNECTOR.locale
const maxEventsCount = container.getAttribute('data-maximum')
const timeZone = window.MOBILIZON_CONNECTOR.timeZone
const eventsCount = Math.min(maxEventsCount, events.length)

View File

@ -25,7 +25,8 @@ class Api {
return !is_numeric($param);
}
]
]
],
'permission_callback' => '__return_true',
]
);
}
@ -44,9 +45,9 @@ class Api {
}
return $events;
} catch (GeneralException $e) {
return 'The events could not be loaded!';
return new \WP_Error('events_not_loading', 'The events could not be loaded!', array('status' => 500));
} catch (GroupNotFoundException $e) {
return sprintf('The group "%s" could not be found!', $groupName);
return new \WP_Error('group_not_found', sprintf('The group "%s" could not be found!', $groupName), array('status' => 404));
}
}
}