Dmitriy | Инициализация.
This commit is contained in:
205
assets/js/price-updates-page.js
Normal file
205
assets/js/price-updates-page.js
Normal file
@@ -0,0 +1,205 @@
|
||||
jQuery(document).ready(function ($) {
|
||||
const $variantons = $('input[name="price-updates-variant"]');
|
||||
const $contents = $("form[data-price-updates-variant]");
|
||||
|
||||
const $count = $(".price-updates-count");
|
||||
const $countSuccess = $(".price-updates-count-success");
|
||||
const $countError = $(".price-updates-count-error");
|
||||
|
||||
const $tableWrapper = $(".price-updates-response");
|
||||
const $error = $(".price-updates-error");
|
||||
const $loaderWrapper = $(".price-updates-loader-wrapper");
|
||||
|
||||
// [Событие] Смена варианита обновления цен
|
||||
$variantons.on("change", function () {
|
||||
const $varianton = $(this);
|
||||
|
||||
$tableWrapper.find("tbody").html("");
|
||||
$tableWrapper.addClass("hidden");
|
||||
|
||||
updatePriceUpdatesCountSuccess(0);
|
||||
updatePriceUpdatesCountError(0);
|
||||
|
||||
$count.addClass("hidden");
|
||||
$countSuccess.addClass("hidden");
|
||||
$countError.addClass("hidden");
|
||||
|
||||
$contents.each(function() {
|
||||
const $content = $(this);
|
||||
|
||||
if ($varianton.val() != $content.data("price-updates-variant")) {
|
||||
$content.addClass("hidden");
|
||||
} else {
|
||||
$content.removeClass("hidden");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Инициализация вариантов обновления цен
|
||||
$variantons.each(function() {
|
||||
const $varianton = $(this);
|
||||
|
||||
$contents.each(function() {
|
||||
const $content = $(this);
|
||||
|
||||
if (!$varianton.is(":checked") && $varianton.val() == $content.data("price-updates-variant")) {
|
||||
$content.addClass("hidden");
|
||||
} else {
|
||||
$content.removeClass("hidden");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function getCount(response) {
|
||||
if (!response?.length) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let success = 0;
|
||||
let error = 0;
|
||||
|
||||
for (const item of response) {
|
||||
if (!item?.isError) {
|
||||
success += 1;
|
||||
} else {
|
||||
error += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return { success, error };
|
||||
}
|
||||
|
||||
function updatePriceUpdatesCountSuccess(count) {
|
||||
if (typeof count !== "number") return;
|
||||
|
||||
$count.removeClass("hidden");
|
||||
|
||||
$countSuccess.removeClass("hidden");
|
||||
$countSuccess.html(`Измененно элементов: <span>${count}</span>`);
|
||||
}
|
||||
|
||||
function updatePriceUpdatesCountError(count) {
|
||||
if (typeof count !== "number") return;
|
||||
|
||||
$count.removeClass("hidden");
|
||||
|
||||
$countError.removeClass("hidden");
|
||||
$countError.html(`Неудалось изменить элементов: <span>${count}</span>`);
|
||||
}
|
||||
|
||||
function getTableRow(data) {
|
||||
const row = document.createElement("tr");
|
||||
|
||||
const tempData = { ...data };
|
||||
|
||||
delete tempData.url;
|
||||
delete tempData.currency;
|
||||
|
||||
for (const [key, value] of Object.entries(tempData)) {
|
||||
const ceil = document.createElement("td");
|
||||
|
||||
if (key === "sku") {
|
||||
const link = document.createElement("a");
|
||||
|
||||
link.href = data.url;
|
||||
link.textContent = value;
|
||||
|
||||
ceil.appendChild(link);
|
||||
} else if (key === "regular" || key === "sale") {
|
||||
ceil.textContent = value["old"] != value["new"] ? `${value["old"]} ${data.currency} -> ${value["new"]} ${data.currency}` : `${value["new"]} ${data.currency}`;
|
||||
}
|
||||
|
||||
row.appendChild(ceil);
|
||||
}
|
||||
|
||||
return $(row);
|
||||
}
|
||||
|
||||
function getTableRowError(data) {
|
||||
const row = document.createElement("tr");
|
||||
|
||||
const tempData = {
|
||||
sku: data?.sku,
|
||||
message: data?.message
|
||||
};
|
||||
|
||||
for (const [key, value] of Object.entries(tempData)) {
|
||||
const ceil = document.createElement("td");
|
||||
|
||||
ceil.textContent = value;
|
||||
|
||||
if (key === "message") {
|
||||
ceil.colSpan = 2;
|
||||
}
|
||||
|
||||
row.appendChild(ceil);
|
||||
}
|
||||
|
||||
return $(row);
|
||||
}
|
||||
|
||||
// [Событие] Отправка формы
|
||||
$contents.on("submit", function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
$loaderWrapper.removeClass("hidden");
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: priceUpdatesSettings.ajaxUrl + "/update",
|
||||
processData: false,
|
||||
contentType: false,
|
||||
data: new FormData(this),
|
||||
success: function (response) {
|
||||
$error.addClass("hidden");
|
||||
|
||||
$tableWrapper.find("tbody").html("");
|
||||
|
||||
if (!response?.length) {
|
||||
$tableWrapper.addClass("hidden");
|
||||
|
||||
updatePriceUpdatesCountSuccess(0);
|
||||
updatePriceUpdatesCountError(0);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const count = getCount(response);
|
||||
|
||||
updatePriceUpdatesCountSuccess(count.success);
|
||||
updatePriceUpdatesCountError(count.error);
|
||||
|
||||
$tableWrapper.removeClass("hidden");
|
||||
|
||||
for (const item of response) {
|
||||
let $row = null;
|
||||
|
||||
if (!item?.isError) {
|
||||
$row = getTableRow(item);
|
||||
} else {
|
||||
$row = getTableRowError(item);
|
||||
}
|
||||
|
||||
$tableWrapper.find("tbody").append($row);
|
||||
}
|
||||
},
|
||||
error: function (error) {
|
||||
$tableWrapper.find("tbody").html("");
|
||||
$tableWrapper.addClass("hidden");
|
||||
|
||||
updatePriceUpdatesCountSuccess(0);
|
||||
updatePriceUpdatesCountError(0);
|
||||
|
||||
$count.addClass("hidden");
|
||||
$countSuccess.addClass("hidden");
|
||||
$countError.addClass("hidden");
|
||||
|
||||
$error.removeClass("hidden");
|
||||
$error.text(error?.responseJSON?.message || "Неизвестная ошибка");
|
||||
},
|
||||
complete: function () {
|
||||
$loaderWrapper.addClass("hidden");
|
||||
}
|
||||
})
|
||||
})
|
||||
});
|
||||
Reference in New Issue
Block a user