$(document).ready(() => {
if (!window.canManage()) return;
setTimeout(() => window.canManage(), 100);
const pathParts = window.location.pathname.split("/");
const authorId = parseInt(pathParts[pathParts.length - 2]);
if (!authorId || isNaN(authorId)) {
Utils.showToast("Некорректный ID автора", "error");
setTimeout(() => (window.location.href = "/authors"), 1500);
return;
}
let originalAuthor = null;
let authorBooks = [];
const $form = $("#edit-author-form");
const $loader = $("#loader");
const $dangerZone = $("#danger-zone");
const $nameInput = $("#author-name");
const $submitBtn = $("#submit-btn");
const $submitText = $("#submit-text");
const $loadingSpinner = $("#loading-spinner");
const $deleteModal = $("#delete-modal");
const $successModal = $("#success-modal");
Promise.all([
Api.get(`/api/authors/${authorId}`),
Api.get(`/api/authors/${authorId}/books/`),
])
.then(([author, booksData]) => {
originalAuthor = author;
authorBooks = booksData.books || booksData || [];
document.title = `Редактирование: ${author.name} | LiB`;
populateForm(author);
renderAuthorBooks(authorBooks);
$loader.addClass("hidden");
$form.removeClass("hidden");
$dangerZone.removeClass("hidden");
$("#cancel-btn").attr("href", `/author/${authorId}`);
})
.catch((error) => {
console.error(error);
Utils.showToast("Автор не найден", "error");
setTimeout(() => (window.location.href = "/authors"), 1500);
});
function populateForm(author) {
$nameInput.val(author.name);
updateCounter();
}
function updateCounter() {
$("#name-counter").text(`${$nameInput.val().length}/255`);
}
$nameInput.on("input", updateCounter);
function renderAuthorBooks(books) {
const $container = $("#author-books-container");
$container.empty();
$("#books-count").text(books.length > 0 ? `(${books.length})` : "");
if (books.length === 0) {
$container.html(`
`);
return;
}
books.forEach((book) => {
$container.append(`
${Utils.escapeHtml(book.title)}
`);
});
}
$form.on("submit", async function (e) {
e.preventDefault();
const name = $nameInput.val().trim();
if (!name) {
Utils.showToast("Введите имя автора", "error");
return;
}
if (name === originalAuthor.name) {
Utils.showToast("Нет изменений для сохранения", "info");
return;
}
setLoading(true);
try {
const updatedAuthor = await Api.put(`/api/authors/${authorId}`, { name });
originalAuthor = updatedAuthor;
showSuccessModal(updatedAuthor);
} catch (error) {
console.error("Ошибка обновления:", error);
let errorMsg = "Произошла ошибка при обновлении автора";
if (error.responseJSON && error.responseJSON.detail) {
errorMsg = error.responseJSON.detail;
} else if (error.status === 401) {
errorMsg = "Вы не авторизованы";
} else if (error.status === 403) {
errorMsg = "У вас недостаточно прав";
} else if (error.status === 404) {
errorMsg = "Автор не найден";
} else if (error.status === 409) {
errorMsg = "Автор с таким именем уже существует";
}
Utils.showToast(errorMsg, "error");
} finally {
setLoading(false);
}
});
function setLoading(isLoading) {
$submitBtn.prop("disabled", isLoading);
if (isLoading) {
$submitText.text("Сохранение...");
$loadingSpinner.removeClass("hidden");
} else {
$submitText.text("Сохранить изменения");
$loadingSpinner.addClass("hidden");
}
}
function showSuccessModal(author) {
$("#success-author-name").text(author.name);
$("#success-link-btn").attr("href", `/author/${author.id}`);
$successModal.removeClass("hidden");
}
$("#success-close-btn").on("click", function () {
$successModal.addClass("hidden");
});
$successModal.on("click", function (e) {
if (e.target === this) {
$successModal.addClass("hidden");
}
});
$("#delete-btn").on("click", function () {
$("#modal-author-name").text(originalAuthor.name);
if (authorBooks.length > 0) {
$("#modal-books-warning").removeClass("hidden");
} else {
$("#modal-books-warning").addClass("hidden");
}
$deleteModal.removeClass("hidden");
});
$("#cancel-delete-btn").on("click", function () {
$deleteModal.addClass("hidden");
});
$deleteModal.on("click", function (e) {
if (e.target === this) {
$deleteModal.addClass("hidden");
}
});
$("#confirm-delete-btn").on("click", async function () {
const $btn = $(this);
const $spinner = $("#delete-spinner");
$btn.prop("disabled", true);
$spinner.removeClass("hidden");
try {
await Api.delete(`/api/authors/${authorId}`);
Utils.showToast("Автор успешно удалён", "success");
setTimeout(() => (window.location.href = "/authors"), 1000);
} catch (error) {
console.error("Ошибка удаления:", error);
let errorMsg = "Произошла ошибка при удалении автора";
if (error.responseJSON && error.responseJSON.detail) {
errorMsg = error.responseJSON.detail;
} else if (error.status === 401) {
errorMsg = "Вы не авторизованы";
} else if (error.status === 403) {
errorMsg = "У вас недостаточно прав";
}
Utils.showToast(errorMsg, "error");
$btn.prop("disabled", false);
$spinner.addClass("hidden");
$deleteModal.addClass("hidden");
}
});
$(document).on("keydown", function (e) {
if (e.key === "Escape") {
if (!$deleteModal.hasClass("hidden")) {
$deleteModal.addClass("hidden");
} else if (!$successModal.hasClass("hidden")) {
$successModal.addClass("hidden");
}
}
});
});