41 lines
1.3 KiB
Plaintext
41 lines
1.3 KiB
Plaintext
document.addEventListener('DOMContentLoaded', () => {
|
|
console.log("favorites.js ejecutado correctamente");
|
|
|
|
const favoriteButton = document.getElementById('favorite-button');
|
|
if (!favoriteButton) {
|
|
console.warn("Botón de favoritos no encontrado. ¿El usuario está autenticado?");
|
|
return;
|
|
}
|
|
|
|
// Obtener el pasteId desde el atributo del <body>
|
|
const pasteId = document.body.dataset.pasteId;
|
|
if (!pasteId) {
|
|
console.error("Error: pasteId no está definido en el <body>.");
|
|
return;
|
|
}
|
|
|
|
favoriteButton.addEventListener('click', function () {
|
|
fetch(`/paste/${pasteId}/favorite`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': 'Bearer ' + (document.body.dataset.authToken || '')
|
|
}
|
|
})
|
|
.then(response => {
|
|
if (!response.ok) {
|
|
throw new Error('Failed to toggle favorite');
|
|
}
|
|
return response.json();
|
|
})
|
|
.then(data => {
|
|
const message = data.message || 'Action completed!';
|
|
showToast(message, 'bg-success');
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
showToast('Error adding to favorites.', 'bg-danger');
|
|
});
|
|
});
|
|
});
|
|
|