74 lines
2.2 KiB
JavaScript
74 lines
2.2 KiB
JavaScript
document.addEventListener("DOMContentLoaded", () => {
|
|
const atletaId = sessionStorage.getItem("userId");
|
|
|
|
if (!atletaId) {
|
|
document.getElementById("rutinas-list").innerHTML =
|
|
"<p>No se encontró tu sesión. Inicia sesión nuevamente.</p>";
|
|
return;
|
|
}
|
|
|
|
fetch(`/api/rutinas/atleta/${atletaId}`)
|
|
.then(res => {
|
|
if (!res.ok) {
|
|
throw new Error("Error al obtener rutinas");
|
|
}
|
|
return res.json();
|
|
})
|
|
.then(rutinas => {
|
|
const contenedor = document.getElementById("rutinas-list");
|
|
if (rutinas.length === 0) {
|
|
contenedor.innerHTML = "<p>No tienes rutinas asignadas todavía.</p>";
|
|
return;
|
|
}
|
|
rutinas.forEach(rutina => {
|
|
const card = document.createElement("div");
|
|
card.className = "card my-3";
|
|
card.innerHTML = `
|
|
<div class="card-body">
|
|
<h5 class="card-title">${rutina.title}</h5>
|
|
<p class="card-text">${rutina.nombreCompetencia || "Sin descripción"}</p>
|
|
<button class="btn btn-success" onclick="verRutina('${rutina._id}')">Ver rutina</button>
|
|
</div>
|
|
`;
|
|
contenedor.appendChild(card);
|
|
});
|
|
})
|
|
.catch(err => {
|
|
console.error(err);
|
|
document.getElementById("rutinas-list").innerHTML =
|
|
"<p>Error al cargar tus rutinas.</p>";
|
|
});
|
|
});
|
|
|
|
function verRutina(id) {
|
|
window.location.href = `simulador.html?routineId=${id}`;
|
|
}
|
|
|
|
function logout() {
|
|
sessionStorage.clear();
|
|
alert("Sesión cerrada");
|
|
window.location.href = "../index.html";
|
|
}
|
|
|
|
// Mostrar nombre del usuario logueado en el header
|
|
window.addEventListener('DOMContentLoaded', async () => {
|
|
const userId = sessionStorage.getItem("userId");
|
|
|
|
if (!userId) {
|
|
alert("Sesión expirada, inicia sesión de nuevo.");
|
|
window.location.href = "index.html";
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const res = await fetch(`/api/users/${userId}`);
|
|
const user = await res.json();
|
|
|
|
if (user?.name) {
|
|
document.getElementById("nombreUsuarioHeader").textContent = user.name;
|
|
document.getElementById("nombreUsuarioDropdown").textContent = "Coach " + user.name;
|
|
}
|
|
} catch (err) {
|
|
console.error("❌ Error al obtener datos del usuario:", err);
|
|
}
|
|
}); |