fix(user): rebuild badge modal ajax flow
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* Server-rendered user medal actions.
|
||||
*
|
||||
* Mirrors the Zibll admin-ajax lifecycle: mutate, close the dialog, then
|
||||
* reload the page so every server-rendered medal surface is in sync.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
(function (window, document) {
|
||||
'use strict';
|
||||
|
||||
var actions = {
|
||||
wear: 'aksrt_user_medal_wear',
|
||||
grant: 'aksrt_user_medal_manually_add',
|
||||
revoke: 'aksrt_user_medal_manually_remove'
|
||||
};
|
||||
|
||||
function notice(message, type) {
|
||||
if (typeof window.notyf === 'function') window.notyf(message, type);
|
||||
}
|
||||
|
||||
document.addEventListener('click', function (event) {
|
||||
var button = event.target.closest('[data-aksrt-badge-action]');
|
||||
if (!button || button.classList.contains('is-loading')) return;
|
||||
|
||||
var type = button.getAttribute('data-aksrt-badge-action') || '';
|
||||
var action = actions[type];
|
||||
var slug = button.getAttribute('data-slug') || '';
|
||||
var nonce = (window.aksrtData && window.aksrtData.nonce) || '';
|
||||
if (!action || !slug || !nonce) return;
|
||||
|
||||
event.preventDefault();
|
||||
button.classList.add('is-loading');
|
||||
button.setAttribute('aria-busy', 'true');
|
||||
|
||||
var form = new URLSearchParams();
|
||||
form.set('action', action);
|
||||
form.set('nonce', nonce);
|
||||
form.set('slug', slug);
|
||||
if (type !== 'wear') form.set('id', button.getAttribute('data-user-id') || '0');
|
||||
|
||||
window.fetch((window.aksrtData && window.aksrtData.ajaxUrl) || '/wp-admin/admin-ajax.php', {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
|
||||
body: form.toString()
|
||||
}).then(function (response) {
|
||||
return response.json();
|
||||
}).then(function (json) {
|
||||
if (!json || !json.success) throw new Error((json && json.data && json.data.message) || '操作失败');
|
||||
notice((json.data && json.data.message) || '操作成功', 'success');
|
||||
window.AksrtModalRuntime.close('badge-action');
|
||||
window.setTimeout(function () { window.location.reload(); }, 120);
|
||||
}).catch(function (error) {
|
||||
button.classList.remove('is-loading');
|
||||
button.removeAttribute('aria-busy');
|
||||
notice(error.message || '网络错误', 'danger');
|
||||
});
|
||||
});
|
||||
})(window, document);
|
||||
Reference in New Issue
Block a user