diff --git a/assets/src/js/admin/main.js b/assets/src/js/admin/main.js index 0a524314..ce133e77 100644 --- a/assets/src/js/admin/main.js +++ b/assets/src/js/admin/main.js @@ -72,6 +72,11 @@ document.addEventListener('DOMContentLoaded', () => { this.stepElems[i].addEventListener('click', this.saveOptionOnNext); } } + + /** + * Run once on pageload. + */ + this.showMessages(); }, /** @@ -121,63 +126,7 @@ document.addEventListener('DOMContentLoaded', () => { form.append('is_list', button.dataset.list); form.append('_nonce', plausible.nonce); - let result = plausible.ajax(form, null); - - result.then(function (response) { - if (response.success === false) { - plausible.toggleOption(e, false); - - return; - } - - let is_wizard = document.getElementById('plausible-analytics-wizard'); - - if (is_wizard !== null) { - return; - } - - if (response.additional !== undefined) { - plausible.renderAdditionalMessages(response.additional, e.target); - } else { - plausible.removeAdditionalMessage(e.target); - } - }); - }, - - /** - * Renders a HTML box containing additional information about the enabled option. - * - * @param html - * @param target - */ - renderAdditionalMessages: function (html, target) { - let container = target.closest('.plausible-analytics-group'); - - container.innerHTML += html; - }, - - /** - * Removes the additional information box when the option is disabled. - * - * @param target - */ - removeAdditionalMessage: function (target) { - let container = target.closest('.plausible-analytics-group'); - let additionalMessage; - - if (container.children.length > 0) { - for (let i = 0; i < container.children.length; i++) { - if (container.children[i].classList.contains('plausible-analytics-hook')) { - additionalMessage = container.children[i]; - - break; - } - } - } - - if (additionalMessage !== undefined) { - container.removeChild(additionalMessage); - } + plausible.ajax(form); }, /** @@ -203,7 +152,7 @@ document.addEventListener('DOMContentLoaded', () => { button.children[0].classList.remove('hidden'); button.setAttribute('disabled', 'disabled'); - plausible.ajax(form, button, null); + plausible.ajax(form, button); }, /** @@ -228,7 +177,7 @@ document.addEventListener('DOMContentLoaded', () => { data.append('options', JSON.stringify(options)); data.append('_nonce', plausible.nonce); - plausible.ajax(data, null, false); + plausible.ajax(data); } }, @@ -296,7 +245,7 @@ document.addEventListener('DOMContentLoaded', () => { data.append('action', 'plausible_analytics_show_wizard'); data.append('_nonce', e.target.dataset.nonce); - plausible.ajax(data, null); + plausible.ajax(data); }, /** @@ -364,14 +313,15 @@ document.addEventListener('DOMContentLoaded', () => { }, /** - * Do AJAX request and (optionally) show a notice or (optionally) reload the page. + * Do AJAX request. * * @param data * @param button + * @param showMessages * * @return object */ - ajax: function (data, button = null) { + ajax: function (data, button = null, showMessages = true) { return fetch( ajaxurl, { @@ -390,14 +340,8 @@ document.addEventListener('DOMContentLoaded', () => { return false; }).then(response => { - if (response.success === true) { - if (response.data !== undefined && response.data.message !== undefined) { - plausible.notice(response.data.message); - } - } else { - if (response.data !== undefined && response.data.message !== undefined) { - plausible.notice(response.data.message, true); - } + if (showMessages === true) { + plausible.showMessages(); } let event = new CustomEvent('plausibleAjaxDone', {detail: response}); @@ -408,19 +352,66 @@ document.addEventListener('DOMContentLoaded', () => { }); }, + /** + * Show messages on screen. + */ + showMessages: function () { + let messages = plausible.fetchMessages(); + + messages.then(function (messages) { + if (messages.error !== false) { + plausible.showMessage(messages.error, 'error'); + } else if (messages.notice !== false) { + plausible.showMessage(messages.notice, 'notice'); + } else if (messages.success !== false) { + plausible.showMessage(messages.success, 'success'); + } + + if (messages.additional.length === 0) { + return; + } + + if (messages.additional.id !== undefined && messages.additional.message) { + plausible.showAdditionalMessage(messages.additional.message, messages.additional.id); + } else if (messages.additional.id !== undefined && messages.additional.message === '') { + plausible.removeAdditionalMessage(messages.additional.id); + } + }); + }, + + /** + * Fetch the messages for display. + */ + fetchMessages: function () { + let data = new FormData(); + data.append('action', 'plausible_analytics_messages'); + + let result = plausible.ajax(data, null, false); + + return result.then(function (response) { + return response; + }); + }, + /** * Displays a notice or error message. * * @param message - * @param isError + * @param type error|warning|success Defaults to success. */ - notice: function (message, isError = false) { - if (isError === true) { + showMessage: function (message, type = 'success') { + if (type === 'error') { document.getElementById('icon-error').classList.remove('hidden'); - document.getElementById('icon-success').classList += ' hidden'; + document.getElementById('icon-success').classList.add('hidden'); + document.getElementById('icon-notice').classList.add('hidden'); + } else if (type === 'notice') { + document.getElementById('icon-notice').classList.remove('hidden'); + document.getElementById('icon-error').classList.add('hidden'); + document.getElementById('icon-success').classList.add('hidden'); } else { document.getElementById('icon-success').classList.remove('hidden'); - document.getElementById('icon-error').classList += ' hidden'; + document.getElementById('icon-error').classList.add('hidden'); + document.getElementById('icon-notice').classList.add('hidden'); } let notice = document.getElementById('plausible-analytics-notice'); @@ -433,7 +424,7 @@ document.addEventListener('DOMContentLoaded', () => { notice.classList.replace('opacity-0', 'opacity-100'); }, 200) - if (isError === false) { + if (type !== 'error') { setTimeout(function () { notice.classList.replace('opacity-100', 'opacity-0'); setTimeout(function () { @@ -441,6 +432,43 @@ document.addEventListener('DOMContentLoaded', () => { }, 200) }, 2000); } + }, + /** + * Renders a HTML box containing additional information about the enabled option. + * + * @param html + * @param target + */ + showAdditionalMessage: function (html, target) { + let targetElem = document.querySelector(`[name='${target}']`); + let container = targetElem.closest('.plausible-analytics-group'); + + container.innerHTML += html; + }, + + /** + * Removes the additional information box when the option is disabled. + * + * @param target + */ + removeAdditionalMessage: function (target) { + let targetElem = document.querySelector(`[name="${target}"]`); + let container = targetElem.closest('.plausible-analytics-group'); + let additionalMessage; + + if (container.children.length > 0) { + for (let i = 0; i < container.children.length; i++) { + if (container.children[i].classList.contains('plausible-analytics-hook')) { + additionalMessage = container.children[i]; + + break; + } + } + } + + if (additionalMessage !== undefined) { + container.removeChild(additionalMessage); + } } } diff --git a/src/Admin/Messages.php b/src/Admin/Messages.php index c9cc9c4b..40bc52e8 100644 --- a/src/Admin/Messages.php +++ b/src/Admin/Messages.php @@ -1,6 +1,7 @@ $message ], $expiration ); } } diff --git a/src/Admin/Settings/API.php b/src/Admin/Settings/API.php index 126a8be5..b9c23199 100644 --- a/src/Admin/Settings/API.php +++ b/src/Admin/Settings/API.php @@ -377,23 +377,17 @@ class="plausible-analytics-wizard-completed-step flex hidden items-start mb-6"> * @return void */ private function render_notices_field() { - /** - * If this var contains a value, the notice box will be shown on next pageloads until the transient is expired. - */ - $show_error = get_transient( 'plausible_analytics_error' ); - $show_notice = get_transient( 'plausible_analytics_notice' ); ?>
- -
+ +