mypastebin/static/js/favorites.js
2025-05-29 22:40:58 +02:00

45 lines
1.6 KiB
JavaScript

document.addEventListener("DOMContentLoaded", function () {
const favoriteButton = document.getElementById("favorite-button");
if (!favoriteButton) return;
// Ajuste para visibilidad en tema oscuro
favoriteButton.classList.add("text-light");
favoriteButton.addEventListener("click", function () {
const pasteId = document.body.dataset.pasteId;
if (!pasteId) {
console.error("Error: pasteId no definido en el <body>.");
return;
}
const isFavorited = favoriteButton.classList.contains("text-danger");
const action = isFavorited ? "unfavorite" : "favorite";
fetch(`/paste/${pasteId}/${action}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
credentials: "include"
})
.then(response => response.json())
.then(data => {
if (data.message.includes("Added")) {
favoriteButton.classList.add("text-danger");
favoriteButton.classList.remove("text-secondary");
favoriteButton.setAttribute("title", "Remove from Favorites");
} else {
favoriteButton.classList.add("text-secondary");
favoriteButton.classList.remove("text-danger");
favoriteButton.setAttribute("title", "Add to Favorites");
}
showToast(data.message, "bg-success");
})
.catch(error => {
console.error("Error updating favorites:", error);
showToast("Error updating favorites.", "bg-danger");
});
});
});