diff --git a/.eslintrc.json b/.eslintrc.json index c6aaedd..1446ca1 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -3,6 +3,9 @@ "browser": true, "es2020": true }, + "globals": { + "SETTINGS": "readonly" + }, "parser": "@babel/eslint-parser", "extends": [ "eslint:recommended", diff --git a/source/connector-mobilizon.php b/source/connector-mobilizon.php index eb998c6..135e766 100644 --- a/source/connector-mobilizon.php +++ b/source/connector-mobilizon.php @@ -24,7 +24,7 @@ final class Mobilizon_Connector { private function __construct() { 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('widgets_init', [$this, 'register_widget']); add_action('wp_enqueue_scripts', [$this, 'register_scripts']); @@ -44,16 +44,28 @@ final class Mobilizon_Connector { 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() { - 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-components', 'wp-editor', 'wp-i18n' ]); 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() { @@ -61,7 +73,9 @@ final class Mobilizon_Connector { } 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() { diff --git a/source/front/blocks/events-list/edit.js b/source/front/blocks/events-list/edit.js index 88a54b7..a8afae0 100644 --- a/source/front/blocks/events-list/edit.js +++ b/source/front/blocks/events-list/edit.js @@ -1,15 +1,28 @@ +import { loadEventLists } from '../../events-loader.js' + const { InspectorControls } = wp.blockEditor +const { useEffect } = wp.element const { PanelBody } = wp.components const { __ } = wp.i18n +const NAME = '' + export default ({ attributes, setAttributes }) => { + useEffect(() => { + reloadEventLists() + }, []) + function reloadEventLists() { + loadEventLists() + } function updateEventsCount(event) { let newValue = Number(event.target.value) if (newValue < 1) newValue = 1 setAttributes({ eventsCount: newValue }) + reloadEventLists() } function updateGroupName(event) { setAttributes({ groupName: event.target.value }) + reloadEventLists() } return [ @@ -42,12 +55,17 @@ export default ({ attributes, setAttributes }) => { /> , -