import { state } from './state.js'; import * as api from './api.js'; import * as ui from './ui.js'; export async function fetchPatients() { try { const patients = await api.fetchPatientsList(); if (patients.length > 0) { state.patientId = patients[0].patient_id; await showDashboard(state.patientId); } else { // This should never happen now since the backend auto-creates a profile, but just in case: ui.showAlert("Profile Error", "No patient profile found. Please contact support."); } } catch (err) { console.error("Failed to load patient profile:", err); } } /** * Fetches patient dashboard data (past sessions, domains, etc.) and renders the dashboard screen. * * @param {string} pId - The patient ID to load dashboard for. */ export async function showDashboard(pId) { try { const data = await api.loadDashboard(pId); document.getElementById("dashboardPatientName").textContent = data.patient.name; document.getElementById("dashboardPatientAge").textContent = data.patient.age ? `Age: ${data.patient.age}` : "Age: --"; const list = document.getElementById("dashboardSessionsList"); list.innerHTML = ""; const dashStartBtn = document.getElementById("dashboardStartBtn"); dashStartBtn.textContent = "NEW SESSION"; if (data.sessions && data.sessions.length > 0) { data.sessions.forEach(sess => { const d = new Date(sess.created_at).toLocaleString(); const summary = sess.rolling_summary || "No summary available."; list.innerHTML += `
${d}
${summary}
`; }); } else { list.innerHTML = `

No previous sessions found.

`; } const domainsDiv = document.getElementById("dashboardDomains"); if (domainsDiv && data.profile) { domainsDiv.innerHTML = `

Clinical Profile

`; const renderDomain = (title, items) => { if (!items || items.length === 0) return; const html = `
${title}
`; domainsDiv.innerHTML += html; }; renderDomain("Emotional Themes", data.profile.emotional_themes); renderDomain("Thinking Patterns", data.profile.thinking_patterns); renderDomain("Stressors", data.profile.stressors); renderDomain("Protective Factors", data.profile.protective_factors); if (data.profile.risk_assessment) { domainsDiv.innerHTML += `
Risk Assessment

${data.profile.risk_assessment}

`; } } ui.switchScreen("dashboardScreen"); } catch (err) { console.error("Dashboard error", err); alert("Could not load patient dashboard."); } }