| 1 |
jQuery(document).ready(function ($) { |
| 2 |
const {id,url,code_setting} = data; |
| 3 |
|
| 4 |
// Convert TeextArea for custom css field on admin settings |
| 5 |
wp.codeEditor.initialize($(`#woocommerce_${id}_custom_style`), code_setting); |
| 6 |
|
| 7 |
/** |
| 8 |
* Make Validation before submit settings via send request |
| 9 |
* to prepare checkout to make sure the access token and entity id is valid |
| 10 |
* @if return success response @then => resume submitting @else print error msg and preventDefault |
| 11 |
* |
| 12 |
* @returns {boolean} if valid or not |
| 13 |
* |
| 14 |
*/ |
| 15 |
const from = $("#mainform"); |
| 16 |
|
| 17 |
from.on('submit', function () { |
| 18 |
|
| 19 |
$('.input-error-msg').remove(); |
| 20 |
|
| 21 |
let valid = false |
| 22 |
|
| 23 |
const accesstoken = $(this).find(`input[name='woocommerce_${id}_accesstoken']`); |
| 24 |
const entityId = $(this).find(`input[name='woocommerce_${id}_entityId']`); |
| 25 |
|
| 26 |
const dataToSend = { |
| 27 |
entityId: entityId.val(), |
| 28 |
}; |
| 29 |
|
| 30 |
$.ajax({ |
| 31 |
type: 'POST', |
| 32 |
async: false, |
| 33 |
headers: { |
| 34 |
Authorization: `Bearer ${accesstoken.val()}` |
| 35 |
}, |
| 36 |
url: url, |
| 37 |
data: dataToSend, |
| 38 |
dataType: "json", |
| 39 |
success: function () { |
| 40 |
valid = true |
| 41 |
}, |
| 42 |
error: function (resultData) { |
| 43 |
valid = false; |
| 44 |
accesstoken.addClass('input-error').after(`<p class='input-error-msg'>${resultData.responseJSON.result.description}</p>`); |
| 45 |
entityId.addClass('input-error').after(`<p class='input-error-msg'>${resultData.responseJSON.result.description}</p>`); |
| 46 |
$('html, body').animate({ |
| 47 |
scrollTop: accesstoken.offset().top - 200 |
| 48 |
}, 200); |
| 49 |
}, |
| 50 |
|
| 51 |
}); |
| 52 |
return valid; |
| 53 |
}) |
| 54 |
|
| 55 |
}); |