Skip to content

Commit

Permalink
fixup! Instead of relying on the button class name, switch to detect …
Browse files Browse the repository at this point in the history
…post-status changes in JS
  • Loading branch information
Rodrigo Gomez Palacio committed Jan 16, 2025
1 parent 76d2a1a commit 9f63b08
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions v3/onesignal-metabox/onesignal-metabox.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,43 @@ window.addEventListener("DOMContentLoaded", () => {
children.forEach((child) => (child.disabled = disabled));
}

setDisplay(optionsWrap, sendPost.checked);
setDisplay(customiseWrap, customisePost.checked);
setDisabled(customiseWrapChild, !customisePost.checked);

sendPost.addEventListener("change", () => {
function updateUI() {
setDisplay(optionsWrap, sendPost.checked);
});

customisePost.addEventListener("change", () => {
setDisplay(customiseWrap, customisePost.checked);
setDisabled(customiseWrapChild, !customisePost.checked);
});
}

// init UI state
updateUI();

// make sure WordPress editor and API are available
if (typeof wp === 'undefined' || !wp.data || !wp.data.select) {
console.warn('wp.data is not available.');
sendPost.addEventListener("change", updateUI);
customisePost.addEventListener("change", updateUI);

// Make sure WordPress editor and API are available
if (typeof wp === "undefined" || !wp.data || !wp.data.select) {
console.warn("wp.data is not available.");
return;
}

const editorStore = wp.data.select('core/editor');

// track initial state of checkbox
const osUpdateCheckbox = document.querySelector('#os_update');
const wasCheckedInitially = osUpdateCheckbox ? osUpdateCheckbox.checked : false;
const editorStore = wp.data.select("core/editor");

// track previous post status to detect changes
let previousStatus = editorStore.getCurrentPostAttribute('status');
// Track previous post status to detect changes
let previousStatus = editorStore.getCurrentPostAttribute("status");

// subscribe to state changes
wp.data.subscribe(() => {
const currentStatus = editorStore.getCurrentPostAttribute('status');
const currentStatus = editorStore.getCurrentPostAttribute("status");

// check if the post status changed to "publish"
if (previousStatus !== currentStatus && currentStatus === 'publish') {
previousStatus = currentStatus;
// Check if the post status changed to "publish"
if (previousStatus !== currentStatus && currentStatus === "publish") {
previousStatus = currentStatus;

if (wasCheckedInitially) {
// uncheck the os_update checkbox
if (osUpdateCheckbox && osUpdateCheckbox.checked) {
osUpdateCheckbox.checked = false;
}
}
// Uncheck the checkbox and update the UI
if (sendPost.checked) {
sendPost.checked = false;
updateUI(); // Ensure the UI reflects the checkbox state
}
} else {
previousStatus = currentStatus;
previousStatus = currentStatus;
}
});
});

0 comments on commit 9f63b08

Please sign in to comment.