Create the JS to handle clear db functionality

This commit is contained in:
Narek Zakarian 2022-12-08 19:24:22 +04:00
parent 990799d20b
commit 374ce5d5f9
No known key found for this signature in database
GPG key ID: 07AFD7E7A9C164A7

View file

@ -0,0 +1,45 @@
document.addEventListener(
'DOMContentLoaded',
() => {
const config = PayPalCommerceGatewayClearDb;
if (!typeof (config)) {
return;
}
const clearDbConfig = config.clearDb;
jQuery(document).on('click', clearDbConfig.button, function () {
const isConfirmed = confirm(clearDbConfig.ConfirmationMessage);
if (!isConfirmed) {
return;
}
const clearButton = document.querySelector(clearDbConfig.button);
clearButton.setAttribute('disabled', 'disabled');
fetch(clearDbConfig.endpoint, {
method: 'POST',
credentials: 'same-origin',
body: JSON.stringify({
nonce: clearDbConfig.nonce,
})
}).then(function (res) {
return res.json();
}).then(function (data) {
const resultMessage = document.querySelector(clearDbConfig.messageSelector);
if (!data.success) {
clearDbConfig.failureMessage.insertAfter(clearButton);
setTimeout(()=> resultMessage.remove(),3000);
clearButton.removeAttribute('disabled');
console.error(data);
throw Error(data.data.message);
}
clearDbConfig.successMessage.insertAfter(clearButton);
setTimeout(()=> resultMessage.remove(),3000);
clearButton.removeAttribute('disabled');
});
})
},
);