mirror of
https://github.com/dwaxweiler/connector-mobilizon
synced 2025-06-05 21:59:25 +02:00
wip
This commit is contained in:
@ -13,6 +13,7 @@
|
|||||||
require_once __DIR__ . '/includes/exceptions/GeneralException.php';
|
require_once __DIR__ . '/includes/exceptions/GeneralException.php';
|
||||||
require_once __DIR__ . '/includes/exceptions/GroupNotFoundException.php';
|
require_once __DIR__ . '/includes/exceptions/GroupNotFoundException.php';
|
||||||
require_once __DIR__ . '/includes/Constants.php';
|
require_once __DIR__ . '/includes/Constants.php';
|
||||||
|
require_once __DIR__ . '/includes/Api.php';
|
||||||
require_once __DIR__ . '/includes/EventsCache.php';
|
require_once __DIR__ . '/includes/EventsCache.php';
|
||||||
require_once __DIR__ . '/includes/Settings.php';
|
require_once __DIR__ . '/includes/Settings.php';
|
||||||
require_once __DIR__ . '/includes/DateTimeWrapper.php';
|
require_once __DIR__ . '/includes/DateTimeWrapper.php';
|
||||||
@ -30,6 +31,7 @@ if (!defined('ABSPATH')) {
|
|||||||
final class Mobilizon_Connector {
|
final class Mobilizon_Connector {
|
||||||
|
|
||||||
private function __construct() {
|
private function __construct() {
|
||||||
|
add_action('init', [$this, 'register_api']);
|
||||||
add_action('init', [$this, 'register_blocks']);
|
add_action('init', [$this, 'register_blocks']);
|
||||||
add_action('init', [$this, 'register_settings'], 1); // required for register_blocks
|
add_action('init', [$this, 'register_settings'], 1); // required for register_blocks
|
||||||
add_action('init', [$this, 'register_shortcut']);
|
add_action('init', [$this, 'register_shortcut']);
|
||||||
@ -60,6 +62,10 @@ final class Mobilizon_Connector {
|
|||||||
wp_add_inline_script($scriptName, 'var MOBILIZON_CONNECTOR = ' . json_encode($settings), 'before');
|
wp_add_inline_script($scriptName, 'var MOBILIZON_CONNECTOR = ' . json_encode($settings), 'before');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function register_api() {
|
||||||
|
MobilizonConnector\Api::init();
|
||||||
|
}
|
||||||
|
|
||||||
public function register_blocks() {
|
public function register_blocks() {
|
||||||
$scriptName = MobilizonConnector\EventsListBlock::initAndReturnScriptName();
|
$scriptName = MobilizonConnector\EventsListBlock::initAndReturnScriptName();
|
||||||
$this->load_settings_globally_before_script($scriptName);
|
$this->load_settings_globally_before_script($scriptName);
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
/* eslint-disable @wordpress/i18n-ellipsis */
|
/* eslint-disable @wordpress/i18n-ellipsis */
|
||||||
import { loadEventList } from '../../events-loader.js'
|
import { loadEventList } from '../../events-loader.js'
|
||||||
|
import {
|
||||||
|
showLoadingIndicator,
|
||||||
|
hideLoadingIndicator
|
||||||
|
} from '../../events-displayer.js'
|
||||||
|
|
||||||
const { InspectorControls, useBlockProps } = wp.blockEditor
|
const { InspectorControls, useBlockProps } = wp.blockEditor
|
||||||
const { PanelBody } = wp.components
|
const { PanelBody } = wp.components
|
||||||
@ -23,7 +27,25 @@ export default ({ attributes, setAttributes }) => {
|
|||||||
timer = setTimeout(() => {
|
timer = setTimeout(() => {
|
||||||
const container = document.getElementById(blockProps.id)
|
const container = document.getElementById(blockProps.id)
|
||||||
if (container) {
|
if (container) {
|
||||||
loadEventList(container)
|
// loadEventList(container)
|
||||||
|
// TODO use API instead
|
||||||
|
|
||||||
|
// TODO not using newest values yet
|
||||||
|
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((data) => {
|
||||||
|
console.log(data) // TODO handle
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
hideLoadingIndicator(container)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}, 500)
|
}, 500)
|
||||||
}
|
}
|
||||||
@ -37,6 +59,7 @@ export default ({ attributes, setAttributes }) => {
|
|||||||
reloadEventList()
|
reloadEventList()
|
||||||
}
|
}
|
||||||
function updateGroupName(event) {
|
function updateGroupName(event) {
|
||||||
|
// TODO not triggered on pasting only
|
||||||
setAttributes({ groupName: event.target.value })
|
setAttributes({ groupName: event.target.value })
|
||||||
reloadEventList()
|
reloadEventList()
|
||||||
}
|
}
|
||||||
|
@ -84,7 +84,7 @@ export function showLoadingIndicator(container) {
|
|||||||
indicator.style.display = 'block'
|
indicator.style.display = 'block'
|
||||||
}
|
}
|
||||||
|
|
||||||
function hideLoadingIndicator(container) {
|
export function hideLoadingIndicator(container) {
|
||||||
const indicator = container.querySelector('.loading-indicator')
|
const indicator = container.querySelector('.loading-indicator')
|
||||||
indicator.style.display = 'none'
|
indicator.style.display = 'none'
|
||||||
}
|
}
|
||||||
|
52
source/includes/Api.php
Normal file
52
source/includes/Api.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
namespace MobilizonConnector;
|
||||||
|
|
||||||
|
class Api {
|
||||||
|
public static function init() {
|
||||||
|
add_action('rest_api_init', 'MobilizonConnector\Api::init_api');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function init_api() {
|
||||||
|
register_rest_route(
|
||||||
|
NAME . '/v1',
|
||||||
|
'/events',
|
||||||
|
[
|
||||||
|
'methods' => 'GET',
|
||||||
|
'callback' => 'MobilizonConnector\Api::get_events',
|
||||||
|
'args' => [
|
||||||
|
'eventsCount' => [
|
||||||
|
'required' => true,
|
||||||
|
'validate_callback' => function($param, $request, $key) {
|
||||||
|
return is_numeric($param) && $param > 0;
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'groupName' => [
|
||||||
|
'validate_callback' => function($param, $request, $key) {
|
||||||
|
return !is_numeric($param);
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function get_events($request) {
|
||||||
|
$eventsCount = $request['eventsCount'];
|
||||||
|
$groupName = isset($request['groupName']) ? $request['groupName'] : '';
|
||||||
|
|
||||||
|
$url = Settings::getUrl();
|
||||||
|
|
||||||
|
try {
|
||||||
|
if ($groupName) {
|
||||||
|
$events = GraphQlClient::get_upcoming_events_by_group_name($url, (int) $eventsCount, $groupName);
|
||||||
|
} else {
|
||||||
|
$events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount);
|
||||||
|
}
|
||||||
|
return $events;
|
||||||
|
} catch (GeneralException $e) {
|
||||||
|
return 'The events could not be loaded!';
|
||||||
|
} catch (GroupNotFoundException $e) {
|
||||||
|
return sprintf('The group "%s" could not be found!', $groupName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user