Dmitriy | Инициализация.
This commit is contained in:
69
assets/js/options-page.js
Normal file
69
assets/js/options-page.js
Normal file
@@ -0,0 +1,69 @@
|
||||
jQuery(document).ready(function ($) {
|
||||
const $form = $(".price-updates-options__form");
|
||||
const $loaderWrapper = $(".price-updates-loader-wrapper");
|
||||
|
||||
const $success = $(".price-updates-options__success-message");
|
||||
const $error = $(".price-updates-options__error-message");
|
||||
|
||||
function hideMessages() {
|
||||
$success.addClass("hidden");
|
||||
$error.addClass("hidden")
|
||||
}
|
||||
|
||||
// [Событие] Сохранение настроек
|
||||
$form.on("submit", function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
$loaderWrapper.removeClass("hidden");
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: priceUpdatesOptionsSettings.ajaxUrl + "/update",
|
||||
data: $(this).serializeArray(),
|
||||
success: function (response) {
|
||||
hideMessages();
|
||||
|
||||
$success.removeClass("hidden");
|
||||
$success.text("Настройки обновлены.");
|
||||
},
|
||||
error: function (error) {
|
||||
console.error(error)
|
||||
|
||||
hideMessages();
|
||||
|
||||
$error.removeClass("hidden");
|
||||
$error.text("Ошибка при обновлении настроек.");
|
||||
},
|
||||
complete: function () {
|
||||
$loaderWrapper.addClass("hidden");
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
$loaderWrapper.removeClass("hidden");
|
||||
|
||||
// [Ajax] Подгружаем настройки
|
||||
$.ajax({
|
||||
method: "GET",
|
||||
url: priceUpdatesOptionsSettings.ajaxUrl + "/get",
|
||||
success: function (response) {
|
||||
if (!response) return;
|
||||
|
||||
for (const [key, value] of Object.entries(response)) {
|
||||
const $input = $form.find(`input[name="${key}"]`);
|
||||
$input.val(JSON.stringify(value).slice(1, -1));
|
||||
}
|
||||
},
|
||||
error: function (error) {
|
||||
console.error(error);
|
||||
|
||||
hideMessages();
|
||||
|
||||
$error.removeClass("hidden");
|
||||
$error.text("Ошибка при загрузке настроек.");
|
||||
},
|
||||
complete: function () {
|
||||
$loaderWrapper.addClass("hidden");
|
||||
}
|
||||
})
|
||||
});
|
||||
Reference in New Issue
Block a user