2025-05-29 22:40:58 +02:00

52 lines
2.3 KiB
JavaScript

document.addEventListener("DOMContentLoaded", () => {
console.log("Cargando: actions.js");
// Manejo de la eliminación de pastes
document.querySelectorAll('.delete-paste').forEach(button => {
button.addEventListener('click', function () {
const pasteId = this.dataset.id;
const row = document.getElementById(`paste-${pasteId}`);
fetch(`/user/paste/${pasteId}/delete`, { method: 'POST' })
.then(response => response.json())
.then(data => {
if (data.message) {
row.remove();
showToast(data.message);
updateCharts(); // Llamada para refrescar estadísticas
} else {
showToast(data.error || 'Error occurred.', 'danger');
}
})
.catch(error => console.error('Error:', error));
});
});
// Función para actualizar estadísticas
const username = document.body.dataset.username;
function updateCharts() {
fetch(`/user/${username}/stats?format=json`)
.then(response => response.json())
.then(stats => {
console.log("Respuesta AJAX recibida:", stats);
// ✅ Actualizar métricas con valores predeterminados
document.getElementById('total-pastes').innerText = stats.totalPastes ?? 0;
document.getElementById('total-text-pastes').innerText = stats.totalTextPastes ?? 0;
document.getElementById('total-file-pastes').innerText = stats.totalFilePastes ?? 0;
document.getElementById('total-media-pastes').innerText = stats.totalMediaPastes ?? 0;
// ✅ Actualizar gráfico
if (window.languageTypeChart) {
window.languageTypeChart.data.labels = stats.languageLabels ?? [];
window.languageTypeChart.data.datasets[0].data = stats.textCounts ?? [];
window.languageTypeChart.data.datasets[1].data = stats.fileCounts ?? [];
window.languageTypeChart.data.datasets[2].data = stats.mediaCounts ?? [];
window.languageTypeChart.update();
}
})
.catch(error => console.error('Error actualizando métricas y gráficos:', error));
}
});