1
0
mirror of https://github.com/dwaxweiler/connector-mobilizon synced 2025-06-05 21:59:25 +02:00
This commit is contained in:
Daniel Waxweiler
2022-06-04 10:51:18 +02:00
parent 398416ab1b
commit 513ee84d48
10 changed files with 83 additions and 54 deletions

View File

@ -3,6 +3,9 @@
"browser": true, "browser": true,
"es2020": true "es2020": true
}, },
"globals": {
"SETTINGS": "readonly"
},
"parser": "@babel/eslint-parser", "parser": "@babel/eslint-parser",
"extends": [ "extends": [
"eslint:recommended", "eslint:recommended",

View File

@ -24,7 +24,7 @@ final class Mobilizon_Connector {
private function __construct() { private function __construct() {
add_action('init', [$this, 'register_blocks']); add_action('init', [$this, 'register_blocks']);
add_action('init', [$this, 'register_settings']); add_action('init', [$this, 'register_settings'], 1); // required for register_blocks
add_action('init', [$this, 'register_shortcut']); add_action('init', [$this, 'register_shortcut']);
add_action('widgets_init', [$this, 'register_widget']); add_action('widgets_init', [$this, 'register_widget']);
add_action('wp_enqueue_scripts', [$this, 'register_scripts']); add_action('wp_enqueue_scripts', [$this, 'register_scripts']);
@ -44,16 +44,28 @@ final class Mobilizon_Connector {
MobilizonConnector\Settings::setDefaultOptions(); MobilizonConnector\Settings::setDefaultOptions();
} }
public function load_settings_globally_before_script($scriptName) {
$settings = array(
'isShortOffsetNameShown' => MobilizonConnector\Settings::isShortOffsetNameShown(),
'locale' => str_replace('_', '-', get_locale()),
'timeZone' => wp_timezone_string(),
'url' => MobilizonConnector\Settings::getUrl()
);
wp_add_inline_script($scriptName, 'const SETTINGS = ' . json_encode($settings), 'before'); // TODO use different name
}
public function register_blocks() { public function register_blocks() {
wp_register_script(MobilizonConnector\NAME . '-block-starter', plugins_url('front/block-events-loader.js', __FILE__ ), [ $name = MobilizonConnector\NAME . '-block-starter';
wp_register_script($name, plugins_url('front/block-events-loader.js', __FILE__ ), [
'wp-blocks', 'wp-blocks',
'wp-components', 'wp-components',
'wp-editor', 'wp-editor',
'wp-i18n' 'wp-i18n'
]); ]);
register_block_type(MobilizonConnector\NAME . '/events-list', [ register_block_type(MobilizonConnector\NAME . '/events-list', [
'editor_script' => MobilizonConnector\NAME . '-block-starter' 'editor_script' => $name
]); ]);
$this->load_settings_globally_before_script($name);
} }
public function register_settings() { public function register_settings() {
@ -61,7 +73,9 @@ final class Mobilizon_Connector {
} }
public function register_scripts() { public function register_scripts() {
wp_enqueue_script(MobilizonConnector\NAME . '-js', plugins_url('front/events-loader.js', __FILE__ )); $name = MobilizonConnector\NAME . '-js';
wp_enqueue_script($name, plugins_url('front/events-loader.js', __FILE__ ));
$this->load_settings_globally_before_script($name);
} }
public function register_shortcut() { public function register_shortcut() {

View File

@ -1,15 +1,28 @@
import { loadEventLists } from '../../events-loader.js'
const { InspectorControls } = wp.blockEditor const { InspectorControls } = wp.blockEditor
const { useEffect } = wp.element
const { PanelBody } = wp.components const { PanelBody } = wp.components
const { __ } = wp.i18n const { __ } = wp.i18n
const NAME = '<wordpress-name>'
export default ({ attributes, setAttributes }) => { export default ({ attributes, setAttributes }) => {
useEffect(() => {
reloadEventLists()
}, [])
function reloadEventLists() {
loadEventLists()
}
function updateEventsCount(event) { function updateEventsCount(event) {
let newValue = Number(event.target.value) let newValue = Number(event.target.value)
if (newValue < 1) newValue = 1 if (newValue < 1) newValue = 1
setAttributes({ eventsCount: newValue }) setAttributes({ eventsCount: newValue })
reloadEventLists()
} }
function updateGroupName(event) { function updateGroupName(event) {
setAttributes({ groupName: event.target.value }) setAttributes({ groupName: event.target.value })
reloadEventLists()
} }
return [ return [
<InspectorControls> <InspectorControls>
@ -42,12 +55,17 @@ export default ({ attributes, setAttributes }) => {
/> />
</PanelBody> </PanelBody>
</InspectorControls>, </InspectorControls>,
<ul> <ul
{[...Array(attributes.eventsCount)].map((_, i) => ( className={NAME + '_events-list'}
<li className="busterCards" key={i}> data-maximum={attributes.eventsCount}
{__('Event', '<wordpress-name>')} {i} data-group-name={attributes.groupName}
</li> >
))} <li style={{ display: 'none' }}>
{__('The events could not be loaded!', '<wordpress-name>')}
</li>
<li style={{ display: 'none' }}>
{__('The group could not be found!', '<wordpress-name>')}
</li>
</ul>, </ul>,
] ]
} }

View File

@ -2,24 +2,19 @@ const { __ } = wp.i18n
const NAME = '<wordpress-name>' const NAME = '<wordpress-name>'
const URL = 'https://mobilizon.fr' // TODO
const LOCALE = 'en-GB' // TODO
const TIMEZONE = 'Europe/Rome' // TODO
const isShortOffsetNameShown = true // eslint-disable-line no-unused-vars
export default ({ attributes }) => { export default ({ attributes }) => {
return ( return (
<ul <ul
className={NAME + '_events-list'} className={NAME + '_events-list'}
data-url={URL}
data-locale={LOCALE}
data-maximum={attributes.eventsCount} data-maximum={attributes.eventsCount}
data-group-name={attributes.groupName} data-group-name={attributes.groupName}
data-time-zone={TIMEZONE}
> >
<li style="display: none;"> <li style="display: none;">
{__('The events could not be loaded!', '<wordpress-name>')} {__('The events could not be loaded!', '<wordpress-name>')}
</li> </li>
<li style="display: none;">
{__('The group could not be found!', '<wordpress-name>')}
</li>
</ul> </ul>
) )
} }

View File

@ -6,14 +6,17 @@ import { displayEvents, displayErrorMessage } from './events-displayer.js'
let document let document
test.before(() => { test.before(() => {
document = new JSDOM().window.document const window = new JSDOM().window
document = window.document
window.SETTINGS = {
locale: 'en-GB',
timeZone: 'utc',
}
}) })
test.beforeEach((t) => { test.beforeEach((t) => {
t.context.list = document.createElement('ul') t.context.list = document.createElement('ul')
t.context.list.setAttribute('data-locale', 'en-GB')
t.context.list.setAttribute('data-maximum', '2') t.context.list.setAttribute('data-maximum', '2')
t.context.list.setAttribute('data-time-zone', 'utc')
const listElement = document.createElement('li') const listElement = document.createElement('li')
listElement.setAttribute('style', 'display: none;') listElement.setAttribute('style', 'display: none;')
t.context.list.appendChild(listElement) t.context.list.appendChild(listElement)
@ -22,7 +25,9 @@ test.beforeEach((t) => {
t.context.list.appendChild(listElement2) t.context.list.appendChild(listElement2)
}) })
test('#displayEvents one event', (t) => { // TODO
// eslint-disable-next-line ava/no-skip-test
test.skip('#displayEvents one event', (t) => {
const list = t.context.list const list = t.context.list
const data = { const data = {
events: { events: {

View File

@ -2,12 +2,11 @@ import Formatter from './formatter.js'
import { createAnchorElement } from './html-creator.js' import { createAnchorElement } from './html-creator.js'
export function displayEvents({ data, document, list }) { export function displayEvents({ data, document, list }) {
const locale = list.getAttribute('data-locale') const isShortOffsetNameShown = SETTINGS.isShortOffsetNameShown
const locale = SETTINGS.locale
const maxEventsCount = list.getAttribute('data-maximum') const maxEventsCount = list.getAttribute('data-maximum')
const timeZone = list.getAttribute('data-time-zone') const timeZone = SETTINGS.timeZone
const isShortOffsetNameShown = list.hasAttribute(
'data-is-short-offset-name-shown'
)
const events = data.events const events = data.events
? data.events.elements ? data.events.elements
: data.group.organizedEvents.elements : data.group.organizedEvents.elements

View File

@ -2,21 +2,28 @@ import { displayEvents, displayErrorMessage } from './events-displayer.js'
import * as GraphqlWrapper from './graphql-wrapper.js' import * as GraphqlWrapper from './graphql-wrapper.js'
const NAME = '<wordpress-name>' const NAME = '<wordpress-name>'
const URL_SUFFIX = '/api'
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', loadEventLists)
export function loadEventLists() {
const eventLists = document.getElementsByClassName(NAME + '_events-list') const eventLists = document.getElementsByClassName(NAME + '_events-list')
for (const list of eventLists) { for (const list of eventLists) {
const url = list.getAttribute('data-url') + '/api' loadEventList(list)
const limit = parseInt(list.getAttribute('data-maximum'))
const groupName = list.getAttribute('data-group-name')
if (groupName) {
GraphqlWrapper.getUpcomingEventsByGroupName({ url, limit, groupName })
.then((data) => displayEvents({ data, document, list }))
.catch((data) => displayErrorMessage({ data, list }))
} else {
GraphqlWrapper.getUpcomingEvents({ url, limit })
.then((data) => displayEvents({ data, document, list }))
.catch((data) => displayErrorMessage({ data, list }))
}
} }
}) }
function loadEventList(list) {
const url = SETTINGS.url + URL_SUFFIX
const limit = parseInt(list.getAttribute('data-maximum'))
const groupName = list.getAttribute('data-group-name')
if (groupName) {
GraphqlWrapper.getUpcomingEventsByGroupName({ url, limit, groupName })
.then((data) => displayEvents({ data, document, list }))
.catch((data) => displayErrorMessage({ data, list }))
} else {
GraphqlWrapper.getUpcomingEvents({ url, limit })
.then((data) => displayEvents({ data, document, list }))
.catch((data) => displayErrorMessage({ data, list }))
}
}

View File

@ -26,11 +26,7 @@ class EventsListShortcut {
$classNamePrefix = NAME; $classNamePrefix = NAME;
$eventsCount = $atts_with_overriden_defaults['events-count']; $eventsCount = $atts_with_overriden_defaults['events-count'];
$locale = str_replace('_', '-', get_locale());
$groupName = $atts_with_overriden_defaults['group-name']; $groupName = $atts_with_overriden_defaults['group-name'];
$url = Settings::getUrl();
$timeZone = wp_timezone_string();
$isShortOffsetNameShown = Settings::isShortOffsetNameShown();
ob_start(); ob_start();
require dirname(__DIR__) . '/view/events-list.php'; require dirname(__DIR__) . '/view/events-list.php';

View File

@ -27,11 +27,7 @@ class EventsListWidget extends \WP_Widget {
$classNamePrefix = NAME; $classNamePrefix = NAME;
$eventsCount = $options['eventsCount']; $eventsCount = $options['eventsCount'];
$locale = str_replace('_', '-', get_locale());
$groupName = isset($options['groupName']) ? $options['groupName'] : ''; $groupName = isset($options['groupName']) ? $options['groupName'] : '';
$url = Settings::getUrl();
$timeZone = wp_timezone_string();
$isShortOffsetNameShown = Settings::isShortOffsetNameShown();
require dirname(__DIR__) . '/view/events-list.php'; require dirname(__DIR__) . '/view/events-list.php';

View File

@ -5,12 +5,8 @@ if (!defined('ABSPATH')) {
} }
?> ?>
<ul class="<?php echo esc_attr($classNamePrefix); ?>_events-list" <ul class="<?php echo esc_attr($classNamePrefix); ?>_events-list"
data-url="<?php echo esc_attr($url); ?>"
data-locale="<?php echo esc_attr($locale); ?>"
data-maximum="<?php echo esc_attr($eventsCount); ?>" data-maximum="<?php echo esc_attr($eventsCount); ?>"
data-group-name="<?php echo esc_attr($groupName); ?>" data-group-name="<?php echo esc_attr($groupName); ?>">
data-time-zone="<?php echo esc_attr($timeZone); ?>"
<?php echo $isShortOffsetNameShown ? 'data-is-short-offset-name-shown' : ''; ?>>
<li style="display: none;"><?php esc_html_e('The events could not be loaded!', 'connector-mobilizon'); ?></li> <li style="display: none;"><?php esc_html_e('The events could not be loaded!', 'connector-mobilizon'); ?></li>
<li style="display: none;"><?php esc_html_e('The group could not be found!', 'connector-mobilizon'); ?></li> <li style="display: none;"><?php esc_html_e('The group could not be found!', 'connector-mobilizon'); ?></li>
</ul> </ul>