Skip to content

Commit

Permalink
Add event communication trigger back.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauteri committed Sep 10, 2024
1 parent 4348ff7 commit 19d29ba
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build/editor.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('moment', 'react-jsx-runtime', 'wp-blocks', 'wp-data', 'wp-date', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => 'bc15603abbcb9b06567f');
<?php return array('dependencies' => array('moment', 'react-jsx-runtime', 'wp-blocks', 'wp-data', 'wp-date', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '1e28ca063435d44e0985');
2 changes: 1 addition & 1 deletion build/editor.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* WordPress dependencies.
*/
import domReady from '@wordpress/dom-ready';
import { dispatch, select } from '@wordpress/data';
import { hasEventPastNotice } from './helpers/event';
import { dispatch, select, subscribe } from '@wordpress/data';
import { hasEventPastNotice, triggerEventCommunication } from './helpers/event';
import { getBlockType, unregisterBlockType } from '@wordpress/blocks';
import './stores';

Expand Down Expand Up @@ -53,7 +53,8 @@ domReady(() => {
);
}

// Display a notice for past events using the 'hasEventPastNotice' function.
subscribe(triggerEventCommunication);

hasEventPastNotice();
});

Expand Down
30 changes: 27 additions & 3 deletions src/helpers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ export function hasEventPastNotice() {
}
}

/**
* Flag to prevent multiple event communication notices.
*
* @type {boolean}
*/
let isEventCommunicationNoticeCreated = false;

/**
* Trigger communication notice for event updates.
*
Expand All @@ -94,13 +101,25 @@ export function hasEventPastNotice() {
export function triggerEventCommunication() {
const id = 'gatherpress_event_communication';
const notices = dispatch('core/notices');
const isSavingPost = select('core/editor').isSavingPost();
const isAutosavingPost = select('core/editor').isAutosavingPost();

notices.removeNotice(id);

// Only proceed if a save is in progress and it's not an autosave.
if (
'publish' === select('core/editor').getEditedPostAttribute('status') &&
!hasEventPast()
isEventPostType() &&
isSavingPost &&
!isAutosavingPost &&
!hasEventPast() &&
!isEventCommunicationNoticeCreated
) {
// Mark notice as created.
isEventCommunicationNoticeCreated = true;

// Remove any previous notices with the same ID.
notices.removeNotice(id);

// Create a new notice with an action.
notices.createNotice(
'success',
__('Send an event update to members via email?', 'gatherpress'),
Expand All @@ -120,4 +139,9 @@ export function triggerEventCommunication() {
}
);
}

// Reset the flag after the save operation completes.
if (!isSavingPost) {
isEventCommunicationNoticeCreated = false;
}
}

0 comments on commit 19d29ba

Please sign in to comment.